binary search tree traversal

13
Binary Search Tree Traversal Irvanizam Zamanhuri, M.Sc Computer Science Study Program Syiah Kuala University http://www.informatika.unsyiah.ac.id/irvanizam Email: [email protected] DIK-013 Data Structure Diploma 3 Years in Informatics Management

Upload: others

Post on 03-Feb-2022

20 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Binary Search Tree Traversal

Binary Search Tree Traversal

Irvanizam Zamanhuri, M.ScComputer Science Study ProgramSyiah Kuala Universityhttp://www.informatika.unsyiah.ac.id/irvanizamEmail: [email protected]

DIK-013 Data StructureDiploma 3 Years in Informatics Management

Page 2: Binary Search Tree Traversal

Traversal (1/2) Compared to linear data structures like linked lists and

one dimensional arrays, which have a canonical method of traversal, tree structures can be traversed in many different ways.

Starting at the root of a binary tree. There are three main steps that can be performed and

the order in which they are performed defines the traversal type.

These steps (in no particular order) are: - performing an action on the current node

(referred to as "visiting" the node), - traversing to the left child node, and - traversing to the right child node. 

Page 3: Binary Search Tree Traversal

Traversal (2/2) Thus the process is most easily described

through recursion. The names given for particular style of traversal

came from the position of root element with regard to the left and right nodes

Imagine that the left and right nodes are constant in space, then - the root node could be placed to the left of the left

node (pre-order), - between the left and right node (in-order), or - to the right of the right node (post-order).

Page 4: Binary Search Tree Traversal

Pre-Order Traversal To traverse a non-empty binary tree in preorder,

perform the following operations recursively at each node, starting with the root node:

- Visit the root.

- Traverse the left subtree.

- Traverse the right subtree.

Page 5: Binary Search Tree Traversal

Pre-Order

resulting visit order :

F - B - A - D - C - G - I - H (root,left,right)

PREORDER ( BSTNodePTR pointer) if pointer != NULL VISIT (pointer) PREORDER( LChild(pointer)) PREORDER( Rchild(pointer))

Page 6: Binary Search Tree Traversal

Implementation Pre-Order in C

void preOrder(IntBSTree * pBST){ if(pBST!=NULL) { printf("%d->",pBST->data); preOrder(pBST->left); preOrder(pBST->right); } printf("\n");}

Page 7: Binary Search Tree Traversal

In-Order Traversal (symmetric) To traverse a non-empty binary tree

in inorder (symmetric), perform the following operations recursively at each node:

- Traverse the left subtree.

- Visit the root.

- Traverse the right subtree.

Page 8: Binary Search Tree Traversal

In-Order

resulting visit order :

A - B - C - D - E - F - G - H - I (left, root, right); note how this produces a sorted sequence

INORDER ( BSTNodePTR pointer) if pointer != NULL INORDER( LChild(pointer)) VISIT (pointer) INORDER( Rchild(pointer))

Page 9: Binary Search Tree Traversal

Implementation In-Order in C

void inOrder(IntBSTree * pBST){ if(pBST!=NULL) { inOrder(pBST->left); printf("%d->",pBST->data); inOrder(pBST->right); } printf("\n");}

Page 10: Binary Search Tree Traversal

Post-Order Traversal To traverse a non-empty binary tree in postorder,

perform the following operations recursively at each node:

- Traverse the left subtree.

- Traverse the right subtree.

- Visit the root.

Page 11: Binary Search Tree Traversal

Post-Order

resulting visit order :

A - C - E - D - B - H - I - G - F (left, right, root)

POSTRDER ( BSTNodePTR pointer) if pointer != NULL POSTORDER( LChild(pointer)) POSTORDER( Rchild(pointer)) VISIT (pointer)

Page 12: Binary Search Tree Traversal

Implementation Post-Order in C

void postOrder(IntBSTree * pBST){ if(pBST!=NULL) { postOrder(pBST->left); postOrder(pBST->right); printf("%d->",pBST->data); } printf("\n");}

Page 13: Binary Search Tree Traversal

Reference http://www.informatika.unsyiah.ac.id/tfa/ds/bst.pdf http://www.cplusplus.com/doc/tutorial/ http://en.wikipedia.org/wiki/Tree_traversal http://www.scribd.com/doc/13597362/C-program-for-

Binary-Search-Tree-Traversal