Computer Science 2 Data Structures and Algorithms V22.0102 section 2 Introduction to Trees Professor: Evan Korth New York University.

Slides:



Advertisements
Similar presentations
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Advertisements

Binary Trees, Binary Search Trees COMP171 Fall 2006.
Trees, Binary Trees, and Binary Search Trees COMP171.
CS2420: Lecture 13 Vladimir Kulyukin Computer Science Department Utah State University.
Chapter 4: Trees General Tree Concepts Binary Trees Lydia Sinapova, Simpson College Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
1 Chapter 7 Trees. 2 What is a Tree In computer science, a tree is an abstract model of a hierarchical structure A tree consists of nodes with a parent-child.
Binary Trees. Linear data structures Here are some of the data structures we have studied so far: –Arrays –Singly-linked lists and doubly-linked lists.
Trees CMSC 433 Chapter 8.1 Nelson Padua-Perez Bill Pugh.
CSC 2300 Data Structures & Algorithms February 6, 2007 Chapter 4. Trees.
Binary Trees. 2 Linear data structures Here are some of the data structures we have studied so far: –Arrays –Singly-linked lists and doubly-linked lists.
CS 146: Data Structures and Algorithms June 18 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
1 Chapter 18 Trees Objective To learn general trees and recursion binary trees and recursion tree traversal.
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
Lecture Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion to process trees.
CS Data Structures Chapter 5 Trees. Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals 
Saturday, 04 Apr 2010 University of Palestine Computer Science II Trees.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
CMSC 341 Introduction to Trees. 8/3/2007 UMBC CMSC 341 TreeIntro 2 Tree ADT Tree definition  A tree is a set of nodes which may be empty  If not empty,
DATA STRUCTURES AND ALGORITHMS Lecture Notes 5 Prepared by İnanç TAHRALI.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Binary Trees. 2 Parts of a binary tree A binary tree is composed of zero or more nodes In Java, a reference to a binary tree may be null Each node contains:
Data Structures and Algorithm Analysis Trees Lecturer: Jing Liu Homepage:
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
Trees, Binary Trees, and Binary Search Trees COMP171.
TREES. What is a tree ? An Abstract Data Type which emulates a tree structure with a set of linked nodes The nodes within a tree are organized in a hierarchical.
Trees Isaac Sheff. Nodes Building blocks of trees “Parent” node may have “Child” nodes Can be both parent and child Can’t be its own ancestor Can’t have.
Discrete Structures Trees (Ch. 11)
CE 221 Data Structures and Algorithms Chapter 4: Trees (Binary) Text: Read Weiss, §4.1 – 4.2 1Izmir University of Economics.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 6.
Trees By P.Naga Srinivasu M.tech,(MBA). Basic Tree Concepts A tree consists of finite set of elements, called nodes, and a finite set of directed lines.
CMSC 341 Introduction to Trees. 2/21/20062 Tree ADT Tree definition –A tree is a set of nodes which may be empty –If not empty, then there is a distinguished.
DATA STRUCTURE BS(IT)3rd. Tree An Introduction By Yasir Mustafa Roll No. BS(IT) Bahauddin Zakariya University, Multan.
Data Structures and Algorithms Introduction to Binary and Binary Search Trees Prepared by: S. Kondakci.
24 January Trees CSE 2011 Winter Trees Linear access time of linked lists is prohibitive  Does there exist any simple data structure for.
Binary Trees. 2 Parts of a binary tree A binary tree is composed of zero or more nodes In Java, a reference to a binary tree may be null Each node contains:
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
TREES General trees Binary trees Binary search trees AVL trees Balanced and Threaded trees.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
Chapter 6 – Trees. Notice that in a tree, there is exactly one path from the root to each node.
1 Trees : Part 1 Reading: Section 4.1 Theory and Terminology Preorder, Postorder and Levelorder Traversals.
Fundamentals of Algorithms MCS - 2 Lecture # 17. Binary Search Trees.
1 CMSC 341 Introduction to Trees Textbook sections:
What is a Tree? Formally, we define a tree T as a set of nodes storing elements such that the nodes have a parent-child relationship, that satisfies the.
Trees. Trees: – A trunk from the roots – Divides into branches – Ends in leaves.
Binary Trees.
Trees Saurav Karmakar
CS 201 Data Structures and Algorithms
Lecture 1 (UNIT -4) TREE SUNIL KUMAR CIT-UPES.
Binary Trees.
CMSC 341 Introduction to Trees 8/3/2007 CMSC 341 Tree Intro.
CMSC 341 Introduction to Trees.
Data Structures & Algorithm Design
Binary Trees, Binary Search Trees
TREES General trees Binary trees Binary search trees AVL trees
CS223 Advanced Data Structures and Algorithms
Binary Trees.
Trees.
Binary Trees.
Binary Trees.
Prepared by: S. Kondakci
CE 221 Data Structures and Algorithms
Binary Trees, Binary Search Trees
CE 221 Data Structures and Algorithms
Trees.
Binary Trees.
CMSC 341 Introduction to Trees CMSC 341 Tree Intro.
Binary Trees.
Binary Trees, Binary Search Trees
Presentation transcript:

Computer Science 2 Data Structures and Algorithms V section 2 Introduction to Trees Professor: Evan Korth New York University

Road Map Introduction to trees –Terminology Binary trees Tree traversal Reading: 4.1 – 4.2

Tree Tree defined recursively A tree is a collection of nodes. The collection can be empty; otherwise, a tree consists of a distinguished node r, called the root, and zero or more non-empty (sub) trees T 1, T 2, …, T k each of whose roots are connected by a directed edge from r. A tree is a collection of N nodes, one of which is the root and N-1 edges. Source:Mark Allen Weiss

Tree terminology The root of each subtree is said to be a child of r and r is said to be the parent of each subtree root. Leaves: nodes with no children (also known as external nodes) Internal Nodes: nodes with children Siblings: nodes with the same parent Source:Mark Allen Weiss - edited by Evan Korth

Tree terminology (continued) A path from node n 1 to n k is defined as a sequence of nodes n 1, n 2, …, n k such that n i is the parent of n i+1 for 1<= i <= k. The length of this path is the number of edges on the path namely k-1. The length of the path from a node to itself is 0. There is exactly one path from the root to each node. Source:Mark Allen Weiss

Tree terminology (continued) Depth (of node): the length of the unique path from the root to a node. Depth (of tree): The depth of a tree is equal to the depth of its deepest leaf. Height (of node): the length of the longest path from a node to a leaf. –All leaves have a height of 0 –The height of the root is equal to the depth of the tree Source:Mark Allen Weiss

Tree Example In class: On blackboard

Binary trees A binary tree is a tree in which no node can have more than two children. Each node has an element, a reference to a left child and a reference to a right child.

Picture of a binary tree a bc d e ghi l f jk Source: David Matuszek

Tree traversals A binary tree is defined recursively: it consists of a root, a left subtree, and a right subtree To traverse (or walk) the binary tree is to visit each node in the binary tree exactly once Tree traversals are naturally recursive Since a binary tree has three “parts,” there are six possible ways to traverse the binary tree: –root, left, right –left, root, right –left, right, root –root, right, left –right, root, left –right, left, root Source: David Matuszek

Preorder traversal In preorder, the root is visited first Here’s a preorder traversal to print out all the elements in the binary tree: public void preorderPrint(BinaryTree bt) { 1 if (bt == null) return; 2 System.out.println(bt.value); 3 preorderPrint(bt.leftChild); 4 preorderPrint(bt.rightChild); } Source: David Matuszek

Inorder traversal In inorder, the root is visited in the middle Here’s an inorder traversal to print out all the elements in the binary tree: public void inorderPrint(BinaryTree bt) { if (bt == null) return; inorderPrint(bt.leftChild); System.out.println(bt.value); inorderPrint(bt.rightChild); } Source: David Matuszek

Postorder traversal In postorder, the root is visited last Here’s a postorder traversal to print out all the elements in the binary tree: public void postorderPrint(BinaryTree bt) { if (bt == null) return; postorderPrint(bt.leftChild); postorderPrint(bt.rightChild); System.out.println(bt.value); } Source: David Matuszek

Tree traversals using “flags” The order in which the nodes are visited during a tree traversal can be easily determined by imagining there is a “flag” attached to each node, as follows: To traverse the tree, collect the flags: preorderinorderpostorder A BC DEFG A BC DEFG A BC DEFG A B D E C F G D B E A F C G D E B F G C A Source: David Matuszek

Other traversals The other traversals are the reverse of these three standard ones –That is, the right subtree is traversed before the left subtree is traversed Reverse preorder: root, right subtree, left subtree Reverse inorder: right subtree, root, left subtree Reverse postorder: right subtree, left subtree, root Source: David Matuszek

Arithmetic Expression Tree Binary tree for an arithmetic expression –internal nodes: operators –leaves: operands Example: arithmetic expression tree for the expression ((2  (5 - 1)) + (3  2))   

Print Arithmetic Expressions inorder traversal: –print “(“ before traversing left subtree –print operand or operator when visiting node –print “)“ after traversing right subtree void printTree(t) //binary operands only if (t.left != null) print("("); printTree (t.left); print(t.element ); if (t.right != null) printTree (t.right); print (")");    ((2  (5 - 1)) + (3  2))

Evaluate Arithmetic Expressions postorder traversal –Recursively evaluate subtrees –Apply the operator after subtrees are evaluated int evaluate (t) //binary operators only if (t.left == null) //external node return t.element; else //internal node x = evaluate (t.left); y = evaluate (t.right); let o be the operator t.element z = apply o to x and y return z;   