Data Structure and Algorithm (CS 302) : A. K. Siromoni
Data Structure and Algorithm (CS 302) : A. K. Siromoni
( CS 302)
A. K. Siromoni
Lecture Objective 1 : Explain the difference between linear and non-linear data
structure
How can we use the above Abstract Data Type ( ADT ) for storage and other operations
Solution 1.
Solution 2.
Binary Tree
Depth 3 = Height,
node = 2depth
Numbers to be inserted : 22 56 38 11 17 25 37
22 Step 1 root = createnode();
22 root
Step 2 putdata(root)
node
Step 3 q=p=root
56 Step 4 node = createnode() q = p = root = & 22
56
Step 5 putdata(node)
Step 6 p=q
Step 7 if data_node less than data_p then q =left of p
Step 8 if data_node greater than data_p then q = right of p
Step 9 if q not equals to NULL goto Step 6
Numbers to be inserted : 22 56 38 11 17 25 37
Step 10 if q equals to NULL and data_node less than data_p then Set node left of p
Step 11 if q equals to NULL and data_node greater than data_p then Set node right of p
Step 12 Ask user if more data to be entered if yes go to Step 3
22
56
Step 3 q=p=root
38 Step 4 node = createnode() q = p = root = & 22
38
Step 5 putdata(node)
Binary Search Tree (B S T )
left_subtree (keys) ≤ node (key) ≤ right_subtree (keys)
Numbers to be inserted : 22 56 38 11 17 25 37
Step 6 p=q
Step 7 if data_node less than data_p then q =left of p
Step 8 if data_node greater than data_p then q = right of p
Step 9 if q not equals to NULL goto Step 6
p = q= & 22 38 < 22 : q = left of p ( & 22 ) else q = right of p p = &22 q= &56
p = q= &56 38 < 56 : q = left of p ( & 56 ) else q = right of p p = &56 q= NULL
Step 10 if q equals to NULL and data_node less than data_p then Set node left of p
Step 11 if q equals to NULL and data_node greater than data_p then Set node right of p
Step 12 Ask user if more data to be entered if yes go to Step 3
22
56
38
Binary Search Tree (B S T )
Creation of BST
Algorithm
Data Type : Structure BST ( integer Data,
Address of left child of BST node,
Address of right child of BST node )
Storage :
Step 1 root = createnode();
Step 2 putdata(root)
Step 3 q=p=root
Step 4 node = createnode()
Step 5 putdata(node)
Step 6 p=q
Step 7 if data_node less than data_p then q =left of p
Step 8 if data_node greater than data_p then q = right of p
Step 9 if q not equals to NULL goto Step 6
Step 10 if q equals to NULL and data_node less than data_p then Set node left of p
Step 11 if q equals to NULL and data_node greater than data_p then Set node right of p
Step 12 Ask user if more data to be entered if yes go to Step 3
Binary Search Tree (B S T )
Creation of BST
Partial Implementation in C
Result : 9 12 14 17 19 23 50 54 67 72 76
Binary Search Tree (B S T )
Traversal of BST
Preorder Traversal : Root , Left , Right
Result : 50 17 12 9 14 23 19 72 54 67 76
Binary Search Tree (B S T )
Traversal of BST
Postorder Traversal : Left , Right , Root
Result : 9 14 12 19 23 17 67 54 76 72 50
A
A Binary Search Tree (B S T )
B A
Creation of BST by Traversal B
I] In Order : E D F B A C H G
D
B P] Pre Order : A B D E F C G H
D E F
B C
D G
E F H
In Order : E D F B A C H G
Pre Order : A B D E F C G H
Binary Search Tree (B S T )
Traversal of BST
Recursive Implementation of Traversal in C Language
/* Inorder Traversal : Left , Root , /* Postorder Traversal : Left , Right ,
Right */ Root */
void inorder ( struct bst * root) Void postorder ( struct bst * root)
{ if( root ! = NULL ) { { if( root ! = NULL ) {
inorder( root -> left ) ; postorder ( root -> left ) ;
printf(“ %d “, root -> data ); postorder( root -> right ) ;
inorder (root -> right ); } } printf(“ %d “, root -> data );
} }
3 https://en.wikipedia.org/wiki/Binary_expression_tree
4 https://www.youtube.com/watch?v=nVcGsgsOlGU
5. https://www.youtube.com/watch?v=wcIRPqTR3Kc