0% found this document useful (0 votes)
12 views29 pages

UNIT 2 Part 2

The document provides an overview of Binary Search Trees (BST), detailing their properties, operations such as searching, insertion, and deletion, as well as algorithms for these operations. It explains the structure of BSTs, the significance of node placement, and the time complexities associated with various operations. Additionally, it includes algorithms for constructing a BST from given inorder and preorder traversals.

Uploaded by

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

UNIT 2 Part 2

The document provides an overview of Binary Search Trees (BST), detailing their properties, operations such as searching, insertion, and deletion, as well as algorithms for these operations. It explains the structure of BSTs, the significance of node placement, and the time complexities associated with various operations. Additionally, it includes algorithms for constructing a BST from given inorder and preorder traversals.

Uploaded by

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

UNIT II PART 2

Binary search tree


BINARY SEARCH TREES (BST)
• A data structure for efficient
searching, insertion and deletion
• Binary search tree property
• For every node X
• All the keys in its left
subtree are smaller than
the key value in X
• All the keys in its right
subtree are larger than the
key value in X
BINARY SEARCH TREES (BST)
🞆 All elements in
bst are stored in
such a manner 7
that 0
🞆 Left subtree bst
holds values less
than root element 9
5
value. 2
0
🞆 Right subtree
holds values
3 6 7 9
greater than root
element. 2 0 8 7

🞆 And left and 2 4 5 6 7 8 9 9


right subtrees are0 5 5 8 5 0 4 9
recursively Binary
Search Tree
BST ADT
BINARY SEARCH TREES

A binary search tree Not a binary search tree


BINARY SEARCH TREES
The same set of keys may have different BSTs

🞆 Average depth of a node is O(log N)


🞆 Maximum depth of a node is O(N)
SEARCHING BST
🞆 If you are searching for 15 then you are done
🞆 If we are searching for key<15, then we
should search in the left subtree
🞆 If we are searching for key>15, then we
should search in the right subtree
SEARCHING (FIND)
• Find X: return a pointer to the node that has
key X, or NULL if there is no such node
• Time complexity: ???
🞆 Algorithm
INORDER TRAVERSAL OF BST
🞆 Inorder traversal of BST prints out all the
keys in sorted order

Inorder: 2, 3, 4, 6, 7, 9, 13, 15, 17, 18, 20


FINDMIN/ FINDMAX
🞆 Goal: return the node containing the smallest
(largest) key in the tree
🞆 Algorithm: Start at the root and go left (right)
as long as there is a left (right) child. The
stopping point is the smallest (largest)
element
🞆 Time complexity = O(height of the tree)
INSERTION
• Proceed down the tree to find correct position
to insert
• If X is found, do nothing (or update
something)
• Otherwise, insert X at the last spot on the
path traversed

• Time complexity???
DELETION
🞆 When we delete a node, we need to consider
how we take care of the children of the
deleted node.

⚫ This has to be done such that the property of the


search tree is maintained.
DELETION UNDER DIFFERENT CASES
🞆 Case 1: the node is a leaf
⚫ Delete it immediately
🞆 Case 2: the node has one child
⚫ Adjust a pointer from the parent to bypass that
node
DELETION CASE 3
• Case 3: the node has 2 children
– Replace the key of that node with the minimum
element at the right subtree
– Delete that minimum element
• Has either no child or only right child because if it has
a left child, that left child would be smaller and would
have been chosen. So invoke case 1 or 2.
ALGORITHM
🞆 Algorithm deleteBST(root,dltkey)
if(empty tree)
Return false
If(dlekey<root)
return deleteBST(left subtree,dltkey)
Else If(dlekey>root)
return deleteBST(right subtree,dltkey)
Else
if(no left subtree)
make right subtree the root
return true
if(no right subtree)
make left subtree the root
return true
else
Save root in deletenode
Set smallest to smallestBST(right subtree)
Move data in smallest to deletenode
Return deleteBST(right subtree deletenode
,key of smallest)
ALGORITHM(NON RECURSIVE)
delnode=search(key)
Parent=delnode parent;
if((delNode->left == NULL) && (delNode->right == NULL))
// node to be deleted has no children
{if(parent->left == delNode)
{ parent->left = NULL;
free(delNode); delNode = NULL; }
else if(parent->right == delNode)
{ parent->right = NULL;
free(delNode); delNode = NULL; }
else if((delNode->left!=NULL) && (delNode->right == NULL))
//node to be deleted has exactly one child
{ if(parent->left == delNode)
{ parent->left = delNode->left;
free(delNode); delNode = NULL; }
else if(parent->right == delNode)
{ parent->right = delNode->left;
free(delNode); delNode = NULL; }
}
else if((delNode->left==NULL) && (delNode->right!=
NULL)) //node to be deleted has exactly one child
{ if(parent->left == delNode)
{ parent->left = delNode-> right;
delNode = NULL; }
else if(parent->right == delNode)
{ parent->right = delNode-> right;
delNode = NULL; }
}
else if((delNode->right!=NULL)&&(delNode->left !=
NULL)) //if has two children
{ temp = delNode;
trav = delNode->right;
if((trav->right == NULL)&&(trav->left == NULL))
{ delNode->key = trav->key; free trav;
delNode->right =NULL;
}
else {
ptr = trav;
while(trav->left!=NULL)
{ ptr = trav; //smallest node in right subtree
trav=trav->left;
}
delNode->key = trav->key;
ptr->left=trav->right;
trav = NULL;
}
}
ALGORITHM(RECURSIVE)
delnode(node *root, key)
if root==NULL return root;
if key< root->data
root->left =delnode( root->left,key)
else if key< root->data
root->left =delnode( root->left,key)
else
{ if (root->left==NULL)
temp==root->right; free root; return temp;
else if (root->right==NULL)
temp==root->left; free root; return temp;
}
else
temp= root->right;
while (temp->left !=NULL) temp=temp->left;
root->key =temp->key
root->right=delnode(root->right,temp->key)
CONSTRUCT TREE FROM GIVEN
INORDER AND PREORDER
TRAVERSALS
🞆 Inorder: 2, 3, 4, 6, 7, 9, 13, 15, 17, 18, 20
🞆 Preorder: 15,6,3,2,4,7,13,9,18,17,20
ALGORITHM
1) Get an element from Preorder. Increment a
Preorder Index Variable.
2) Create a new tree node with the element.
3) Search the element’s index in Inorder. Let
the index be inIndex.
4) Call constructTree for elements before
inIndex and make the built tree as left subtree
of current element.
5) Call buildTree for elements after inIndex
and make the built tree as right subtree of
current element. .
6) return tNode.
CONSTRUCT BST
Preorder : 100 , 20 , 10 , 30 , 200 , 150 , 300
Inorder: 10 , 20 , 30 , 100 , 150 , 200 , 300
Preorder : 100 , 20 , 10 , 30 , 200 , 150 , 300
Inorder: 10 , 20 , 30 , 100 , 150 , 200 , 300
PSEDOCODE
Constructbst(inorder, preorder, instart, inend)
{
static preindex=0;inindex=0;
node *t =createnode(preorder[preindex++])
if( instart==inend)
return t;
for i=instart to inend
if (t->data==inorder[i]) inindex=i
constructbst(inorder, preorder, instart, inindex-1)
constructbst(inorder, preorder, inindex+1, inend)
}

You might also like