You are on page 1of 12

EXPERIMENT NO: 5.

Aim: To transverse a Binary Search tree recursively and non recursively.

Theory:
A binary search tree is a tree where each node has a left and right child. Either child, or both children, may be missing. binary tree data structure which has the following properties:[1]

The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys greater than the node's key. The left and right subtree each must also be a binary search tree. There must be no duplicate nodes.

The major advantage of binary search trees over other data structures is that the related sorting algorithms and search algorithms such as in-order traversal can be very efficient.

The Binary Search tree is Implemented using linked lists. Using nodes that contain a data member , a reference to the left sub tree and a reference to the right sub tree . The Structure of the node is as follows: struct node { int info; struct node*left; struct node* right; }

Traversal in Binary Search Tree:


Tree Traversal means visiting each node of the given tree exactly once. The following algorithms are described for a binary tree. 1. In-order. 2. Pre-order. 3. Post-order.

Algorithm:(Recursive)
A] In-Order: 1. Traverse the left Sub-tree In Order. 2. Visit the Root. 3. Traverse the right Sub-tree In Order. B] Pre-Order:. 1. Visit the Root. 2. Traverse the left Sub-tree Pre Order. 3. Traverse the right Sub-tree Pre Order.

C] Post-Order: 1. Traverse the left Sub-tree Post Order. 2. Traverse the right Sub-tree Post Order. 3. Visit the Root.

Algorithm:(Non-Recursive)
A]In-Order:
1. 2. 3. 4. 5. 6. 7. 8. Create an empty stack S. Initialize current node as root Push the current node to S and set current = current->left until current is NULL If current is NULL and stack is not empty then a) Pop the top item from stack. b) Print the popped item, set current = current->right c) Go to step 3. If current is NULL and stack is empty then we are done.

B] Pre-Order:.
1. Create an empty stack S. 2. Initialize current node as root 3. Do following while s is not empty. a) Pop an item from stack and print it. b) Push right child of popped item to stack c) Push left child of popped item to stack.

C] Post-Order:
1. Push root to first stack. 2. Loop while first stack is not empty 2.1 Pop a node from first stack and push it to second stack 2.2 Push left and right children of the popped node to first stack 3. Print contents of second stack

Program Code:A]Recursive.
#include<stdio.h> #include<stdlib.h> struct node { int info; struct node* right; struct node* left; }; struct node* root; struct node* maketree(int x) { struct node *p; p=(struct node*)malloc(sizeof(struct node*)); p->info=x; p->left=NULL; p->right=NULL; return p; } void setleft(struct node* p,int x) { if(p==NULL||p->left!=NULL) printf("invalid insertion"); else

p->left=maketree(x); } void setright(struct node* p,int x) { if(p==NULL||p->right!=NULL) printf("invalid insertion"); else p->right=maketree(x); } void postorder(struct node* root) { struct node *temp=root; if(temp->left!=NULL) inorder(temp->left); if(temp->right!=NULL) inorder(temp->right); printf("\n %d",temp->info);

} void inorder(struct node* root) { struct node *temp=root; if(temp->left!=NULL) inorder(temp->left); printf("\n %d",temp->info); if(temp->right!=NULL) inorder(temp->right);

} void preorder(struct node* root) { struct node *temp=root; printf("\n %d",temp->info); if(temp->left!=NULL) inorder(temp->left); if(temp->right!=NULL) inorder(temp->right); } int main() { struct node* p; struct node* q; int i; int arr[10],count=0,element; printf("Enter the elements"); for(i=0;i<10;i++) { scanf("%d",&element); arr[i]=element; printf("%d",arr[i]); } root=maketree(arr[0]); while(count<10) {

int number=arr[count+1]; p=root; q=root; while(q!=NULL) { p=q; if(number<p->info) q=p->left; else q=p->right; } if(number<p->info) setleft(p,number); else setright(p,number); count++; } printf(" \n inorder.\n"); inorder(root); printf("\n preorder.\n"); preorder(root); printf("\n postorder.\n"); postorder(root);

return 0; }

Program code:[Non recursion]:


#include<stdio.h> #include<stdlib.h> struct node { int info; struct node *left; struct node *right; };

struct stack { int top; struct node* arr[50]; }s;

int isEmpty() { if(s.top==-1) return 1; else return 0; }

void push(struct node* T)

{ s.arr[s.top] = T; s.top++; }

struct node* pop() { if(isEmpty()) printf("Stack Empty"); else return s.arr[s.top--]; }

struct node* makeTree(int value) { struct node *p; p=(struct node*) malloc(sizeof(struct node)); p->info=value; p->left=NULL; p->right=NULL; return p; }

void setLeft(struct node* n,int x) { if(n==NULL || n->left!=NULL) printf("invalid insertion. ");

else n->left=makeTree(x);

} void setRight(struct node* n,int x) { if(n==NULL || n->right!=NULL) printf("invalid insertion. "); else n->right=makeTree(x);

} void InOrder(struct node* root) { struct node* T = root; while(T!=NULL) { push(T); T=T->left; } while(1) { if(isEmpty()) break; else { T=pop();

printf("\n%d", T->info); T=T->right; while(T!=NULL) { push(T); T=T->left; } } } } int main() { struct node* p; struct node* q; int i; int arr[10],count=0,element; printf("Enter the elements"); for(i=0;i<10;i++) { scanf("%d",&element); arr[i]=element; printf("%d",arr[i]); } root=maketree(arr[0]); while(count<10) { int number=arr[count+1];

p=root; q=root; while(q!=NULL) { p=q; if(number<p->info) q=p->left; else q=p->right; } if(number<p->info) setleft(p,number); else setright(p,number); count++; } s.top=-1; printf("Traversing inorder without recursion"); InOrder(); return 0; }

Conclusion:
Thus we have traversed the Binary Search Tree recursively using recursion functions and non recursively using stack.

You might also like