Binary Tree Traversal.

Slides:



Advertisements
Similar presentations
CS Fall 2012, Lab 08 Haohan Zhu. Boston University Slideshow Title Goes Here CS Fall 2012, Lab /17/2015 Tree - Data Structure  Basic.
Advertisements

1 Tree Traversal Section 9.3 Longin Jan Latecki Temple University Based on slides by Paul Tymann, Andrew Watkins, and J. van Helden.
1 Nell Dale Chapter 9 Trees Plus Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
1 A Two-Level Binary Expression ‘-’ ‘8’ ‘5’ treePtr INORDER TRAVERSAL : has value 3 PREORDER TRAVERSAL: POSTORDER TRAVERSAL: 8 5 -
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
1 Nell Dale Chapter 9 Trees Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
Tree Traversal. Traversal Algorithms preorder inorder postorder.
4/17/2017 Section 9.3 Tree Traversal ch9.3.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
Binary Tree Properties & Representation. Minimum Number Of Nodes Minimum number of nodes in a binary tree whose height is h. At least one node at each.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
1 CS308 Data Structures An application of binary trees: Binary Expression Trees.
Tree Traversal. Traversal Algorithms preorder inorder postorder.
Binary Trees. Node structure Data A data field and two pointers, left and right.
Chapter Chapter Summary Introduction to Trees Applications of Trees (not currently included in overheads) Tree Traversal Spanning Trees Minimum.
1 Chapter 18 Trees Objective To learn general trees and recursion binary trees and recursion tree traversal.
1 Nell Dale Chapter 9 Trees Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
(c) University of Washington20d-1 CSC 143 Java Applications of Trees.
12-CRS-0106 REVISED 8 FEB 2013 CSG2A3 ALGORITMA dan STRUKTUR DATA.
CS Data Structures Chapter 5 Trees. Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals 
Trees.ppt1 Introduction Many data structures are linear –unique first component –unique last component –other components have unique predecessor and successor.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Data Structures : Project 5 Data Structures Project 5 – Expression Trees and Code Generation.
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.
Tree Data Structures.
Starting at Binary Trees
1 Storing Hierarchical Information Lists, Stacks, and Queues represent linear sequences Data often contain hierarchical relationships that cannot be expressed.
CSC 172 DATA STRUCTURES. LISTS We have seen lists: public class Node { Object data; Node next; } 
Binary Trees In computer science, a binary tree is a tree data structure in which each node has at most two children, which are referred to as the left.
12-CRS-0106 REVISED 8 FEB 2013 CSG2A3 ALGORITMA dan STRUKTUR DATA.
© University of Auckland Trees – (cont.) CS 220 Data Structures & Algorithms Dr. Ian Watson.
Binary Search Trees (BST)
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.
Traversing a tree means visiting each node in a specified order. There are different ways to traverse a tree. Tree Traversing Depth first search Pre-orderIn-orderPost-order.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
AL-HUSEEN BIN TALAL UNIVERSITY College of Engineering Department of Computer Engineering Algorithms and Data Structures Binary Tree Course No.:
2/11/ IT 179 Recursive Definition of Tree Structures 1.Empty is a tree; the root is null 2.A node points to a finite number of the roots of some.
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.
Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST) Binary 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.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
The Tree ADT.
Data Structure By Amee Trivedi.
Chapter 12 – Data Structures
Traversal From CSCE 3110 Data Structures & Algorithm Analysis
Non Linear Data Structure
Binary Tree ADT: Properties
Recursive Objects (Part 4)
Section 8.1 Trees.
abstract containers sequence/linear (1 to 1) hierarchical (1 to many)
Chapter 20: Binary Trees.
Chapter 21: Binary Trees.
CS212: Data Structures and Algorithms
Binary Tree Traversal Methods
Binary Tree Traversal Methods
Binary Tree Traversal Methods
Binary Tree Traversals
Section 9.3 by Andrew Watkins
2018, Fall Pusan National University Ki-Joune Li
Binary Trees, Binary Search Trees
Stack.
Binary Tree Properties & Representation
Tree.
Queue Applications Lecture 31 Tue, Apr 11, 2006.
Chapter 20: Binary Trees.
Binary Tree Traversal Methods
C++ Plus Data Structures
Binary Trees, Binary Search Trees
Presentation transcript:

Binary Tree Traversal

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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;

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 7 4 + 2 * 1 - - * 1 + 2 7 4

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

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

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

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

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

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

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

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