Presentation is loading. Please wait.

Presentation is loading. Please wait.

Traversal From CSCE 3110 Data Structures & Algorithm Analysis

Similar presentations


Presentation on theme: "Traversal From CSCE 3110 Data Structures & Algorithm Analysis"— Presentation transcript:

1 Traversal From CSCE 3110 Data Structures & Algorithm Analysis
Rada Mihalcea Trees. Binary Trees. Reading: Chap.4 ( ) Weiss

2 A Tree Node Every tree node: object – useful information
children – pointers to its children nodes O O O O O

3 Tree Traversal Two main methods: Recursive definition PREorder:
Postorder Recursive definition PREorder: visit the root traverse in preorder the children (subtrees) POSTorder traverse in postorder the children (subtrees)

4 Preorder preorder traversal Algorithm preOrder(v) “visit” node v
for each child w of v do recursively perform preOrder(w)

5 Postorder postorder traversal du (disk usage) command in Unix
Algorithm postOrder(v) for each child w of v do recursively perform postOrder(w) “visit” node v du (disk usage) command in Unix

6 Binary Trees A special class of trees: max degree for each node is 2
Recursive definition: A binary tree is a finite set of nodes that is either empty or consists of a root and two disjoint binary trees called the left subtree and the right subtree. Any tree can be transformed into binary tree. by left child-right sibling representation

7 Binary tree example data key left right data left right
In a binary tree Struct node { int key; leftchild *node; rightchild *node; }

8 Preorder Implementation
Dummy example: void preorder(node * t) { node * ptr; if (t!=NULL) { cout<<t->key; preorder(t->leftchild); preorder(t->rightchild); } return; Struct node { int key; leftchild *node; rightchild *node; } void preorder(node * t) { node * ptr; if (t!=NULL) { process(t->key); preorder(t->leftchild); preorder(t->rightchild); return;

9 Postorder Implementation
Struct node { int key; leftchild *node; rightchild *node; } void postorder(node * t) { node * ptr; if (t!=NULL) { postorder(t->leftchild); postorder(t->rightchild); cout<<t->key; return;

10 Inorder Implementation
Struct node { int key; leftchild *node; rightchild *node; } void inorder(node * t) { node * ptr; if (t!=NULL) { inorder(t->leftchild); cout<<t->key; inorder(t->rightchild); return;

11 Arithmetic Expression Using BT
inorder traversal A / B * C * D + E infix expression preorder traversal + * * / A B C D E prefix expression postorder traversal A B / C * D * E + postfix expression level order traversal + * E * D / C A B + * E * D / C A B

12 Inorder Traversal (recursive version)
void inorder(node *ptr) /* inorder tree traversal */ { if (ptr!=NULL) { inorder(ptr->leftchild); cout<< ptr->key; indorder(ptr->rightchild); } A / B * C * D + E

13 Preorder Traversal (recursive version)
void preorder(node * ptr) /* preorder tree traversal */ { if (ptr!=NULL) { cout<<ptr->key; preorder(ptr->leftchild); predorder(ptr->rightchild); } + * * / A B C D E

14 Postorder Traversal (recursive version)
void postorder(node *ptr) /* postorder tree traversal */ { if (ptr!=NULL) { postorder(ptr->leftchild); postdorder(ptr->rightchild); cout<<ptr->key; } A B / C * D * E +


Download ppt "Traversal From CSCE 3110 Data Structures & Algorithm Analysis"

Similar presentations


Ads by Google