0% found this document useful (0 votes)
75 views

Data Structure and Algorithm (CS 302) : A. K. Siromoni

The document discusses binary search trees, explaining that they are a non-linear data structure where each node's left subtree contains values less than the node's value and the right subtree contains values greater than the node's value. It provides pseudocode for inserting values into a binary search tree and describes recursive and non-recursive approaches to traversing a binary search tree using inorder, preorder, and postorder traversal.

Uploaded by

Abhigyan Prakash
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views

Data Structure and Algorithm (CS 302) : A. K. Siromoni

The document discusses binary search trees, explaining that they are a non-linear data structure where each node's left subtree contains values less than the node's value and the right subtree contains values greater than the node's value. It provides pseudocode for inserting values into a binary search tree and describes recursive and non-recursive approaches to traversing a binary search tree using inorder, preorder, and postorder traversal.

Uploaded by

Abhigyan Prakash
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Data Structure And Algorithm

( CS 302)

A. K. Siromoni

Department of Information Technology


Lecture 3

Lecture Objective 1 : Explain the difference between linear and non-linear data
structure

Lecture Objective 2 : Illustrate different features of Binary Tree

Lecture Objective 3 : Implement Binary Search Tree in C Language


Last Lecture
• Linked List and its applications

Now we will try to move from Linear Data


Structure to Non Linear Data Structure

struct data_type { int data;


struct data_type *a;
struct data_type *b; };

How can we use the above Abstract Data Type ( ADT ) for storage and other operations
Solution 1.

Doubly Linked List

• Linear Data Structure

Solution 2.

Binary Tree

• Non Linear Data Structure


Tree

A Tree can have any number of child . A Multiway Tree of order n is a


general tree in which each node has n or fewer subtrees and contains one
fewer keys than it has subtrees.

A Binary Tree is a Multiway Tree of order 2


Binary Tree
Depth and Height of a Tree
Depth : Length of the path from root to current node.
Depth of root is 0
Height : Length of the longest path from root to any node

Depth 0 , node = 2depth

Depth 1, node = 2depth

Depth 2, node = 2depth

Depth 3 = Height,
node = 2depth

Maximum Nodes in a Full Binary Tree : 2 Height + 1 – 1 , Edges : No of Nodes -1


Binary Tree
A full binary tree (sometimes proper binary tree or 2-tree) is
a tree in which every node other than the leaves has two children.
A complete binary tree is a binary tree in which every level,
except possibly the last, is completely filled, and all nodes are as
far left as possible.
Binary Search Tree (B S T )
left_subtree (keys) ≤ node (key) ≤ right_subtree (keys)
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
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

p = q= & 22 56 < 22 : q = left of p ( & 22 ) else q = right of p p = &22 q= NULL


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

struct bst { int data; struct bst * left, * right ; } ;

main() { struct bst * root, *node;


root = createnode();
putdata(root);
while(getans()){
q=p=root ;
node = createnode() ;
putdata(node) ;
/* Searching for Proper Position of Node */
while(q!=NULL) {
p=q ;
if( node ->data < p -> data ) q = p -> left ;
else q = p -> right ; }/* Searching for Proper Position for node is Complete */
if (node -> data < p -> data ) p -> left = node;
else p - > right = node; } /* All Data Inserted and BST is formed */ }
Binary Search Tree (B S T )
Traversal of BST
Inorder Traversal : Left , Root , Right

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

P] A B D E F C G H : A is root , But B Left or Right Child ?

I] E D F B A C H G: B stands left to root, so definitely Left Child

P] A B D E F C G H : D is the next one , Will be Left or Right Child

I] E D F B A C H G: D stands left to B, so must be a left child

P] A B D E F C G H : E and F are the next two

I] E D F B A C H G: E stands left to D, so must be a left child

I] E D F B A C H G: F stands right to D, so must be a right child


Binary Search Tree (B S T )
Creation of BST by Traversal

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

/* Preorder Traversal : Root , Left


Right */
void preorder ( struct bst * root)
{ if( root ! = NULL ) {
printf(“ %d “, root -> data );
preorder ( root -> left );
preorder (root -> right ); } }
Binary Search Tree (B S T )
Traversal of BST
Non Recursive Implementation of Inorder Traversal in C
Language

struct stack { int top;


struct bst * stk[100] ; /* Assuming 100 nodes are in tree */
} s;

void inorder(struct bst * root) { struct bst *p;


s.top = -1 ;
p = root;
do{
while(p != NULL){
push(s , p);
p = p -> left; }
if(notempty(s)){ p = pop(s);
printf(“ %d “, p->data);
p = p -> right;
} } while( notempty(s) !! ( p != NULL)); }
Binary Search Tree (B S T )
In Order Traversal of BST
p = root;
do{
while(p != NULL){
push(s , p);
p = p -> left; }
if(notempty(s)){ p = pop(s);
printf(“ %d “,
p->data);
9 12 17 50 : Push 19 : pop p = p -> right;
9 : pop P = NULL } } while(
P = NULL 23 : pop notempty(s) !! ( p != NULL)); }
12 : pop P = NULL
P= & 14 50 : pop
14 17 50 : Push P = & 72 P = NULL
14 : pop 54 72 : Push 72 : pop
P = Null 54 : pop P = & 76
17 : pop P = & 67 76 : Push
P = & 23 67 72 : Push 76 : pop
19 23 50 : Push 67 : pop P= NULL and Stack Empty
Assignment 4
1. Write a non recursive program which will show the numbers in a
BST in descending order.

2. Create an expression tree from a given infix expression


a = b * c + d / eg – f
( eg may be considered at e ^ g )

3. Write a C program which will accept a number and will search


the number in a binary search tree . If available it will tell in which
depth it is available otherwise it will prompt message “ Number not
found “ .
( Every Program should be properly documented )

• Covers LO1, LO2 and LO3


Self Learning
For Assignment 4

1. Data Structure Using C & C++ : Langsum, Augenstein , Tanenbaum ,


Second Edition, Prentice Hall of India

2. Data Structures : Seymour Lipschutz , Indian Adapted Edition


2006, Tata McGaw Hill Education Pvt Ltd

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

You might also like