Binary Search Tree
Binary Search Tree
fahmid@cse.uiu.ac.bd 1
Binary Search Tree
A binary search tree (BST) is a node-based binary tree data structure which has the
following properties:
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 or equal to the
node's key.
Both the left and right subtrees must also be binary search trees.
Binary Search Tree
The efficiency of all of the operations is O(h) = O(log n), only if the tree is reasonably
height-balanced
Each node contains at least the following fields
key: an identifying field
Found!!
Operation on BST: Search
Givena key k and a pointer to the root, Tree-Search returns a pointer to a node with key k
or NIL:
Tree-Search(x, k)
//return if found
if(x == NIL or k==key[x])
return x;
//else recursively search in subtree
if(k < key[x])
return Tree-Search(left[x], k);
else return Tree-Search(right[x], k);
Operation on BST: Search
Tree-Search(x, k)
while (x != NIL and k != key[x]){
if (k < x->value)
x = x->left;
else
x = x->right;
}
return x;
Operation on BST: Min & Max
Operation on BST: Successor
Two cases:
Case 1: x has a right subtree:
Two cases:
Case 2: x has no right subtree:
Intuition: As long as you move to the left up the tree, you’re visiting smaller
nodes.
Operation on BST: Successor (Code)
Operation on BST: Predeccessor
Two cases:
similar to Successor algorithm
Operation on BST: Insert
Inserts an element z to the tree so that the binary search tree property continues to hold
The basic algorithm
Like the search procedure as discussed before
Use a “trailing pointer” to keep track of where you came from (like inserting into
singly linked list)
Operation on BST: Insert
Operation on BST:
Insert Compare with root
shoumik@cse.uiu.ac.bd
Operation on BST: Insert
External node
Operation on BST: Insert
External node
Operation on BST: Insert
Operation on BST: Insert
Operation on BST: Insert
Operation on BST: Insert
• Draw a Binary Search Tree (showing each step) after inserting the
following keys:
• 22, 15, 25, 10, 35, 46, 37, 8, 50
Operation on BST: Delete
Delete node x
3 cases:
• x has no children:
• Remove x
• x has one child:
• Splice out x
• x has two children:
• Find its inorder successor y
• Replace x with y
• Delete y
Operation on BST: Delete
Operation on BST: Delete
Operation on BST: Delete
Operation on BST: Delete
Operation on BST: Delete
Operation on BST: Delete
Found!
Operation on BST: Delete
Splice out z
Operation on BST: Delete
Operation on BST: Delete
Operation on BST: Delete
Operation on BST: Delete
Operation on BST: Simulation
• Draw the BST after doing each step of the following statements:
• Insert 85
• Delete 28
• Delete 76
• Insert 55
• Delete 44
Sorting with BST
BSTSort(A)
for i=1 to n
TreeInsert(A[i]);
InorderTreeWalk(root);
Average case?
BST: Remarks
O (lg n)
Balanced BST
General framework:
First a binary search tree
shoumik@cse.uiu.ac.bd
Codes for BST
#include<stdio.h> #include<stdlib.h>
typedef struct BSTnode{
int key;
struct BSTnode *left;
struct BSTnode *right;
} BSTnode;
BSTnode *create( ){
BSTnode *temp;
printf("\nEnter key: ");
temp = (BSTnode*)malloc(sizeof(BSTnode));
scanf("%d", &temp->key);
return temp;
}
Codes for BST
void insert(BSTnode *root, BSTnode *temp) BSTnode *minValueNode(BSTnode *node){
{ if(root != NULL) {
inorder(root->left);
printf("%d ", root-
>key); inorder(root-
>right);
}
shoumik@cse.uiu.ac.bd
}
Codes for BST
int main() {
char ch;
int item;
BSTnode
*root =
NULL,
*temp;
do {
temp = create();
if(root ==
NULL) getchar(); scanf(“%c”, &ch);
root =
temp; else
insert(ro
ot, temp);
printf("\n Do
you want to
enter
more(y/n)? ");
printf("\nDelete what? "); scanf("%d",
} &item); root = deleteNode(root, item);
while(ch=='y'|
printf("\nInorder traversal of the modified tree \t");
ch=='Y');
inorder(root);
} printf("\n Inorder traversal of the
printf("\nDo you want to delete more(y/n)? getchar(); scanf(“%c”, &ch);
tree:\t"); inorder(root);
");
do {
} while(ch=='y'|ch=='Y');
if(root == NULL){return 0; }
48
12/2re9/t2u1rn 0; }
Thank You
49