Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS212: Data Structures and Algorithms

Similar presentations


Presentation on theme: "CS212: Data Structures and Algorithms"— Presentation transcript:

1 CS212: Data Structures and Algorithms
Trees

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

3 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

4 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.

5 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.

6 Binary Trees Some Binary Trees One node Two nodes Three nodes

7 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.

8 Binary Tree Implementation
Linked Implementation

9 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; } };

10 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…

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

12 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

13 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

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

15 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

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

17 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

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

19 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 / +

20 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

21 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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

36 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); }

37 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.

38 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

39 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 ;

40 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.


Download ppt "CS212: Data Structures and Algorithms"

Similar presentations


Ads by Google