Dinamik Linked List - Informatika

9 downloads 185 Views 457KB Size Report
http://www.informatika.unsyiah.ac.id/irvanizam. Email: irvanizam.zamanhuri@ informatika.unsyiah.ac.id. DIK-013 Data Structure. Diploma 3 Years in Informatics  ...
Binary Search Tree Traversal DIK-013 Data Structure Diploma 3 Years in Informatics Management

Irvanizam Zamanhuri, M.Sc Computer Science Study Program Syiah Kuala University http://www.informatika.unsyiah.ac.id/irvanizam Email: [email protected]

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 :

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

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

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 :

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

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

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

POSTRDER ( BSTNodePTR pointer) if pointer != NULL POSTORDER( LChild(pointer)) POSTORDER( Rchild(pointer)) VISIT (pointer) (left, right, root)

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-forBinary-Search-Tree-Traversal