Presentation is loading. Please wait.

Presentation is loading. Please wait.

Binary Tree Traversal.

Similar presentations


Presentation on theme: "Binary Tree Traversal."— Presentation transcript:

1 Binary Tree Traversal

2 Traversal of Binary Tree
The process of visiting each node in a tree Why the traversal necessary? Checking whether insertions/deletions work well. Searching a specific node. How to visit all nodes once? A B D E C F G H I J K

3 Traversal of Binary Tree
Traversal methods Inorder traversal: LCR Visiting a left subtree, a root node, and a right subtree Preorder traversal: CLR Visiting the root node node before subtrees Postorder traversal: LRC Visiting subtrees before visiting the root node Level order traversal C L R

4 Inorder Traversal (LCR)
Algorithm: LCR Step 1: Visiting a left subtree Step 2: Visiting the root node Step 3: Visiting a right subtree 6 2 A C L R 9 4 B C 2 5 7 11 D E F G 1 H I 3 8 J K 10 1 3

5 Inorder Traversal (LCR)
Root A B C D E F G H I J K Left subtree Right subtree B D E H I C F G J K A

6 Inorder Traversal (LCR)
B D E H I D B E A H I A Right subtree H D I B E A Left subtree C F G J K F G C K J F J C K G Left subtree Right subtree

7 Inorder Traversal (LCR)
Algorithm void Inorder(BTreeNode* root) { if (root != NULL) Inorder(root->left_child); printf("%d ", root->item); Inorder(root->right_child); } 6 4 9 2 5 7 11 3 1 A B D E C F G H I J K 10 8 H D I B E A F J C K G

8 Preorder Traversal (CLR)
Algorithm: CLR Step 1: Visiting the root node Step 2: Visiting a left subtree Step 3: Visiting a right subtree 1 2 7 3 6 8 10 5 4 A B D E C F G H I J K 11 9 1 C L R 2 3

9 Preorder Traversal (CLR)
Root A B C D E F G H I J K Left subtree Right subtree B D E H I C F G J K A

10 Preorder Traversal (CLR)
H I B D E H I A B E A Right subtree A B D H I E Left subtree C F G J K F J G K C C F J G K Left subtree Right subtree

11 Preorder Traversal (CLR)
Algorithm: CLR void Preorder(BTreeNode* root) { if (root != NULL) printf("%d ", root->item); Preorder(root->left_child); Preorder(root->right_child); } 1 2 7 3 6 8 10 5 4 A B D E C F G H I J K 11 9 A B D H I E C F J G K

12 Postorder Traversal (LRC)
Algorithm: LRC Step 1: Visiting a left subtree Step 2: Visiting a right subtree Step 3: Visiting the root node 11 5 10 3 4 7 9 2 1 A B D E C F G H I J K 8 6 3 C L R 1 2

13 Postorder Traversal (LRC)
Root A B C D E F G H I J K Left subtree Right subtree B D E H I C F G J K A

14 Postorder Traversal (LRC)
H I B D E H I E B Right subtree H I D E B Left subtree C F G J K F J G K C A A J F K G C A Left subtree Right subtree

15 Postorder Traversal (LRC)
Algorithm: LRC void Postorder(BTreeNode* root) { if (root != NULL) Postorder(root->left_child); Postorder(root->right_child); printf("%d ", root->item); } 11 5 10 3 4 7 9 2 1 A B D E C F G H I J K 8 6 H I D E B J F K G C A

16 Inorder Postorder Preorder 11 5 10 3 4 7 9 2 1 A B D E C F G H I J K 8
6 4 9 2 5 7 11 3 1 A B D E C F G H I J K 10 8 11 5 10 3 4 7 9 2 1 A B D E C F G H I J K 8 6 Postorder Preorder 1 2 7 3 6 8 10 5 4 A B D E C F G H I J K 11 9

17 Level Order Traversal Algorithm
For each level, visit nodes from the left to right direction. 1 Level 0 A 3 2 Level 1 B C 4 5 6 7 Level 2 D E F G 8 H I 9 10 J K 11 Level 3 A B C D E F G H I J K

18 Level Order Traversal Algorithm
Traverse a tree by using a queue (FIFO) When dequeuing a node, enqueue its children from the left to the right direction. 1 2 3 4 5 6 7 9 8 A B D E C F G H I J K 11 10 EnQueue A A EnQueue B, C B C EnQueue D, E D E EnQueue F, G F G EnQueue H, I H I EnQueue - EnQueue J J EnQueue K K

19 Level Order Traversal void Levelorder(BTreeNode* root) { Queue queue;
if (root == NULL) return; InitQueue(&queue); EnQueue(&queue, root); while (!IsEmpty(&queue)) root = Peek(&queue); DeQueue(&queue); printf("%d ", root->item); if (root->left_child != NULL) EnQueue(&queue, root->left_child); if (root->right_child != NULL) EnQueue(&queue, root->right_child); }

20 Calculating Directory Size
How to accumulate directory size? Each file has different size, and each directory has 10K. 50K 150K 200K 30K 10K + 230K 10K + 200K 10K + 450K

21 Calculating Directory Size
// Make use of postorder traversal. int CalDirectorySize(BTreeNode *root) { int left_size, right_size; if (root == NULL) return 0; else { left_size = CalDirectorySize(root->left_child); right_size = CalDirectorySize(root->right_child); return (root->item + left_size + right_size); } 10 50 150 200 30

22 Binary Expression Tree
Infix notation: X + Y Operators are written in-between their operands. Need extra information to make the order of evaluation of the operators clear. Example: (7 + 4) * 2 – 1 The expression tree is easier to understand than infix notation. - * 1 + 2 7 4 (7 + 4) * 2 - 1

23 Binary Expression Tree
Definition Representing an expression as a tree. Non-leaf node: operator, leaf node: operand + * c a b - + * a b c d + a b a+b a*b+c a+b-c*d Infix Prefix +ab +*abc -+ab*cd Postfix ab+ ab*c+ ab+cd*-

24 Calculating Expression Tree
Calculate and update the node. - - * 1 11 2 * 1 + 2 7 4 Calculating 7 + 4

25 Calculating Expression Tree
Calculate 11 * 2 and update the node. Calculate and update the node. - - 22 1 * 1 11 2 Calculating 11 * 2 - 22 1 21 Calculating

26 Calculating Expression Tree
int CalculateExpTree(BTreeNode * root) { int op1, op2; if (root == NULL) return 0; if (root->left_child == NULL && root->right_child == NULL) return root->item; op1 = CalculateExpTree(root->left_child); op2 = CalculateExpTree(root->right_child); switch (root->item) case '+': return op1 + op2; case '-': return op1 - op2; case '*': return op1 * op2; case '/': return op1 / op2; } return 0;

27 Building Expression Tree
Overall procedure Infix notation  postfix notation Use a stack (See Lecture Note 3). Postfix notation  expression tree Build a tree incrementally using a stack. Infix notation Postfix notation Expression tree (7 + 4) * 2 - 1 * 1 - - * 1 + 2 7 4

28 Building Expression Tree
Push nodes from operands until finding a operator. Pop two nodes and push an expression tree. * 1 - 4 7 * 1 - + T = T 7 4

29 Building Expression Tree
Push nodes from operands until finding a operator. Pop two nodes and push an expression tree. * 1 - 2 + T = T 7 4 T’ = * + 2 7 4 * 1 - T’

30 Building Expression Tree
Push nodes from operands until finding a operator. Pop two nodes and push an expression tree. * * 1 - 1 T’ = + 2 T’ 7 4 T’’ = - * 1 + 2 7 4 * 1 - T’’

31 Building Expression Tree
BTreeNode * MakeExpTree(char* exp, int len) { Stack stack; BTreeNode * node, *right_node, *left_node; InitStack(&stack); for (int i = 0; i < len; i++) { if ('0' <= exp[i] && exp[i] <= '9') node = CreateNode(exp[i]); else { right_node = Peek(&stack), Pop(&stack); left_node = Peek(&stack), Pop(&stack); CreateRightSubtree(node, right_node); CreateLeftSubtree(node, left_node); } Push(&stack, node); return Peek(&stack);

32 Threaded Binary Tree Variation of binary tree
Modify a binary tree to cheaply find its inorder successor. Have special threading links (dashed arrows). It can be fast traversal. A B C D E F G H I J K

33 Threaded Binary Tree Inorder traversal
Traverse a tree until finding the leftmost node. Solid arrows mean left-side movements. Traverse a tree by a threaded link or a right-side child pointer. Dashed arrows mean right-side movements. 6 A 9 4 B C 5 2 7 11 D E F G H I J K 1 3 8 10

34 Node in Threaded Binary Tree
Node representation in threaded binary tree If no right child node exists, the right_child pointer of the node refers to its inorder successor. This node indicates a threaded node. typedef int BData; typedef struct _bTreeNode { BData item; struct _bTreeNode * left_child; struct _bTreeNode * right_child; bool isTheaded; } BTreeNode; item left_child right_child

35 Inorder in Threaded Binary Tree
BTreeNode* leftMost(BTreeNode* node) { if (node == NULL) return NULL; while (node->left_child != NULL) node = node->left_child; // Go to the leftmost node. return node; } void inorder(BTreeNode* node) BTreeNode* cur = leftmost(node); while (cur != NULL) { printf("%d ", cur->item); // If the node is a thread node, go to its inorder successor. if (cur->isTheaded) cur = cur->right_child; else // Go to the leftmost child in a right subtree. cur = leftmost(cur->right_child);


Download ppt "Binary Tree Traversal."

Similar presentations


Ads by Google