binary search tree traversal

Post on 03-Feb-2022

20 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Binary Search Tree Traversal

Irvanizam Zamanhuri, M.ScComputer Science Study ProgramSyiah Kuala Universityhttp://www.informatika.unsyiah.ac.id/irvanizamEmail: irvanizam.zamanhuri@informatika.unsyiah.ac.id

DIK-013 Data StructureDiploma 3 Years in Informatics Management

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. 

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

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.

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

Implementation Pre-Order in C

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

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.

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

Implementation In-Order in C

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

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.

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)

Implementation Post-Order in C

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

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

top related