CS212: Data Structures and Algorithms

Slides:



Advertisements
Similar presentations
Expression Trees What is an Expression tree? Expression tree implementation Why expression trees? Evaluating an expression tree (pseudo code) Prefix, Infix,
Advertisements

1 Tree Traversal Section 9.3 Longin Jan Latecki Temple University Based on slides by Paul Tymann, Andrew Watkins, and J. van Helden.
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
Trees Chapter 8.
Fall 2007CS 2251 Trees Chapter 8. Fall 2007CS 2252 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information.
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
1 Introduction to Binary Trees. 2 Background All data structures examined so far are linear data structures. Each element in a linear data structure has.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
Transforming Infix to Postfix
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.
Trees. Tree Terminology Chapter 8: Trees 2 A tree consists of a collection of elements or nodes, with each node linked to its successors The node at the.
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.
Ceng-112 Data Structures I 1 Chapter 7 Introduction to Trees.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
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.
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. 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.
CE 221 Data Structures and Algorithms Chapter 4: Trees (Binary) Text: Read Weiss, §4.1 – 4.2 1Izmir University of Economics.
Review 1 Queue Operations on Queues A Dequeue Operation An Enqueue Operation Array Implementation Link list Implementation Examples.
1 Storing Hierarchical Information Lists, Stacks, and Queues represent linear sequences Data often contain hierarchical relationships that cannot be expressed.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
Tree Traversals, TreeSort 20 February Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C.
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.
1 Chapter 4 Trees Basic concept How tree are used to implement the file system How tree can be used to evaluate arithmetic expressions How to use trees.
Rooted Tree a b d ef i j g h c k root parent node (self) child descendent leaf (no children) e, i, k, g, h are leaves internal node (not a leaf) sibling.
AL-HUSEEN BIN TALAL UNIVERSITY College of Engineering Department of Computer Engineering Algorithms and Data Structures Binary Tree Course No.:
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.
DS.T.1 Trees Chapter 4 Overview Tree Concepts Traversals Binary Trees Binary Search Trees AVL Trees Splay Trees B-Trees.
Trees Saurav Karmakar
Data Structure By Amee Trivedi.
CS 201 Data Structures and Algorithms
CSCE 210 Data Structures and Algorithms
Lecture 1 (UNIT -4) TREE SUNIL KUMAR CIT-UPES.
Trees.
DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING IN C++
Paul Tymann and Andrew Watkins
Week 6 - Wednesday CS221.
Lecture Trees Chapter 9 of textbook 1. Concepts of trees
Data Structures & Algorithm Design
Data Structure Interview
CSC 172– Data Structures and Algorithms
i206: Lecture 13: Recursion, continued Trees
TREE DATA STRUCTURE Data Structure 21-Sep-18 Tree
Binary Trees, Binary Search Trees
TREES General trees Binary trees Binary search trees AVL trees
COSC2100: Data Structures and Algorithms
Introduction to Trees IT12112 Lecture 05.
Trees.
Paul Tymann, Andrew Watkins,
Data Structures – Week #5
CE 221 Data Structures and Algorithms
Data Structures and Algorithm Analysis Trees
Binary Trees, Binary Search Trees
CE 221 Data Structures and Algorithms
Trees.
Paul Tymann, Andrew Watkins,
Binary Tree Traversal.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Binary Search Tree.
Presentation transcript:

CS212: Data Structures and Algorithms Trees

Outline Trees (Basic Concepts) Binary Trees Binary Tree Implementation Binary Tree Traversals

Trees (Basic Concepts) A tree is a collection of n nodes, one of which is the root, and n -1 edges. n -1 edges, because each edge connects some node to its parent, and every node except the root has one parent Each node may have an arbitrary number of children, possibly zero. Nodes with no children are known as leaves. Nodes with the same parent are siblings A Path from node n1 to nk is defined as a sequence of nodes n1, n2, …,nk such that ni is the parent of ni + 1 for 1  i < k. The length of this path is the number of edges on the path, namely k – 1

Trees (Basic Concepts) There is a path of length zero from every node to itself. In a tree there is exactly one path from the root to each node. The level of any node is the length of its path from the root. The degree of a node is its number of subtrees.

Binary Trees A binary tree is either empty, or it consists of a node called the root together with two binary trees called the left sub-tree and right sub-tree of the root A binary tree consists of a root and one or two children named as left child and right child. A binary tree is a set of elements that is either empty or is partitioned into three disjoint subsets. The first subset contains a single element called the root of the tree. The other two subsets are themselves binary trees, called the left and right sub-trees of the original tree. A left or right sub-tree can be empty. Each element of the binary tree is called the node of the tree.

Binary Trees Some Binary Trees One node Two nodes Three nodes

Binary Trees Strictly Binary Tree If every non-leaf node in a binary tree has non- empty left and right sub-trees A strict binary tree with n leaves always contain 2n-1 nodes. Complete Binary Tree A strictly binary tree all of whose leaves are at level d If a binary tree contains m nodes at level l it contains at most 2m nodes at level l+1. A complete binary tree contains 2l nodes at level l.

Binary Tree Implementation Linked Implementation

Binary Tree Implementation class BTNode { public:   int info; BTNode *left, *right;   BTNode() { left = right = 0; } BTNode(int el, BTNode *lef = 0, BTNode *ret = 0) { info = el; left = lef; right = ret; } };

Binary Tree Implementation class BT { public:   BT( ) { root = 0; } ~BT( ) { clear( ); root = 0; } void clear( ) { clear(root); root = 0; } //Delete all nodes of a tree bool isEmpty( ) { return root == 0 }//checks tree is empty or not void preorder( ) { preorder(root); }//preorder traversal of tree void inorder( ) { inorder(root); }//inorder traversal of tree void postorder( ) { postorder(root); }//postorder traveral of tree void breadthFirst(); //breadth first traversal //…………………… Continues on next slide…

Binary Tree Implementation private: BTNode *root; void clear(BTNode *); void preorder(BTNode *); void inorder(BTNode *); void postorder(BTNode *); //………………………….. };

Binary Tree Traversals Tree Traversal is the process of visiting each node in the tree exactly one time Tree traversals are of two types Depth First Traversal Breadth First Traversal The three Depth First Traversal techniques are Preorder tree traversal Inorder tree traversal Postorder tree traversal

Binary Tree Traversals Preorder Tree Traversal Visit the root Traverse the left sub-tree in preorder Traverse the right sub-tree in preorder Example: A B C E D ABCDE

Binary Tree Traversals void BT::preorder(BTNode *p) { if(p != 0) cout<<p->info; preorder(p->left); preorder(p->right); }

Binary Tree Traversals Inorder Tree Traversal: Traverse the left sub-tree in inorder Visit the root Traverse the right sub-tree in inorder Example: A B C E D ADCEB

Binary Tree Traversals void BT::inorder(BTNode *p) { if(p != 0) inorder(p->left); cout<<p->info; inorder(p->right); }

Binary Tree Traversals Postorder Tree Traversal: Traverse the left sub-tree in postorder Traverse the right sub-tree in postorder Visit the root Example: A B C E D DECBA

Binary Tree Traversals void BT::postorder(BTNode *p) { if(p != 0) postorder(p->left); postorder(p->right); cout<<p->info; }

Binary Tree Traversals Traverse the following tree: + - / * ^ E F A B C D Inorder: A * B – C ^ D + E / F Preorder: + – * A B ^ C D / E F Postorder: A B * C D ^ - E F / +

Binary Tree Traversals Traverse the following tree. Pre-order Traversal? Post-order Traversal? In-order Traversal? 14 15 4 9 3 18 20 16 17 7 5

Binary Tree Traversals Breadth First Tree Traversal Implementation of this kind of traversal is straightforward when a queue is used Consider a top down left-to-right, breadth first traversal After a node is visited, its children, if any are placed at the end (rear) of a queue, and the node at the beginning (front) of the queue is visited

Breadth First Traversal Example C D B E F G I H

Breadth First Traversal Example C D B E F G I C Queue B A

Breadth First Traversal Example C D B E F G I Dqueue C B AC

Breadth First Traversal Example C D B E F G I B Enqueu D D AC

Breadth First Traversal Example C D B E F G I Dqueue B D ACB

Breadth First Traversal Example C D B E F G I D Enqueue E, H E H ACB

Breadth First Traversal Example C D B E F G I Dqueue D E H ACBD

Breadth First Traversal Example C D B E F G I Dqueue E H ACBDE

Breadth First Traversal Example C D B E F G I Enqueue F, G H F G ACBDE

Breadth First Traversal Example C D B E F G I Dqueue H F G ACBDEH

Breadth First Traversal Example C D B E F G I I Enqueue I F G ACBDEH

Breadth First Traversal Example C D B E F G I I Dqueue F G ACBDEHF

Breadth First Traversal Example C D B E F G I I Dqueue G ACBDEHFG

Breadth First Traversal Example Dqueue I C B D E H F G I ACBDEHFGI

Binary Tree Traversals void BT::breadthFirst() { Queue queue; BTNode *p = root; if(p != 0) { queue.enqueue(p); while(!queue.empty()) { p = queue.dequeue(); cout<<p->key; if(p -> left != 0) queue.enqueue(p->left); if(p -> right != 0) queue.enqueue(p->right); }

Binary Tree Application Two applications / types Binary Search Trees A binary search tree is a binary tree in which all elements left to a given node are less than all elements present on its right side. Binary search tree due to its special data arrangement is good for fast insertion and lookup (searching) Binary Expression Trees A binary expression tree is a type of binary tree which models an arithmetic, relational or logical expression like (( x + y ) - z ). It has the following characteristic: a) leaves are the variables or constants in the expression b) non-leaf nodes are the operators in the expression We will now discuss Binary Expression Trees in detail. Binary Search Trees will be covered in another lecture.

Binary Expression Trees A binary expression tree is used to remove ambiguity in expressions. For example 1- 2*3+4 can be evaluated in multiple ways. Expression tree solved this ambiguity in expressions so that one expression can only be solved in one way. It establishes correct precedence of operations without using parenthesis by using hierarchy. Terms deeper in the tree get evaluated first. A binary expression tree is very helpful in evaluation of arithematic/ logical/relational expressions performing advanced mathematical operations (like differentiation) on the expression generating compiler code to calculate given expression value at running time of the program

How to Build binary expression tree from postfix expression Algorithm to convert a valid postfix expression, (which should only have binary operators), to an expression tree: while(not end of input expression) { if (the current symbol in the input expression is an operand) i) create a binary tree node for the operand ; ii) push the pointer / address of the newly created node onto the stack ; iii) move to next character in input and go to start of while loop ; } else if(the current symbol in the input expression is a binary operator) i) create a binary tree node for the operator ; ii) pop from the stack reference / address of the top most entry and make it right subtree of the operator node ; iii) pop from the stack reference / address of the next top most entry and make it the left subtree of the operator node ; iv) push the reference to the operator node back onto the stack ; v) move to next character in input and go to start of while loop ;

Expression Tree Traversal Once a post fix expression has been converted into an expression tree. The next step is to perform operations on this expression tree. One important operation on the binary expression tree is traversal Pre-order, postorder and inorder traversal over the binary expression tree will yield the arithmetic expression in prefix, postfix and infix form respectively.