Binary Search Tree - Data Structures
Binary Search Tree - Data Structures
Asma Sattar
[email protected]
Topics
• Introduction of BST
• Insertion in BST
• Search in BST
• Deletion in BST
• Finding smallest element in BST
• Find Largest Element in BST
• The basic idea behind this data structure is to have such a storing
repository that provides the efficient way of data sorting, searching
and retrieving.
Binary Search Tree
• For every node, X, in the tree,
• the values of all the keys in its left subtree are smaller than the key value of X,
• the values of all the keys in its right subtree are larger than the key value of X.
• Example
• X>Y
• X<Z
Y Z
Binary Search Tree Examples (on board)
13, 3, 4, 12, 14, 10, 5, 1, 8, 2, 7, 9, 11, 6, 18
Traversal:
Ascending Order??
Descending Order
Binary Search Trees Examples
• Examples
5
10 10
2 45
5 30 5 45
30
2 25 45 2 25 30
10
25
Binary Not a binary
search trees search tree
Binary Search Tree Operations
There are many operations one can perform on a binary search tree.
We will briefly cover all of these operations with their general algorithms
Iimplementation and examples.
Creating a Binary (Search) Tree
The root pointer will point to the first node in the tree, or to NULL
(if the tree is empty).
root
10 5
10 > 2, left 5 > 2, left
5 30 5 > 2, left 2 45 2 = 2, found
2 = 2, found
2 25 45 30
10
25
Example Binary Searches
• Find (root, 25 )
10 5
10 < 25, right 5 < 25, right
5 30 30 > 25, left 2 45 45 > 25, left
25 = 25, found 30 > 25, left
2 25 45 30
10 < 25, right
10 25 = 25, found
25
Recursive Search of Binary Tree
• First, a new node is allocated, and its value member is initialized with
the new value.
Binary Search Tree – Insertion
• Algorithm
1. Perform search for value X
2. Search will end at node Y (if X not in tree)
3. If X < Y, insert new leaf X as new left subtree for Y
4. If X > Y, insert new leaf X as new right subtree for Y
• Observations
• Insertions may unbalance tree
Example Insertion
• Insert ( 20 )
10 10 < 20, right
30 > 20, left
5 30
25 > 20, left
2 25 45 Insert 20 on left
20
Insertion
Note, we assume that our binary tree will store no duplicate values.
void IntBinaryTree::insertNode(int num)
{
TreeNode *newNode,// Pointer to a new node
*nodePtr;// Pointer to traverse the tree
// Create a new node
newNode = new TreeNode;
newNode->value = num;
newNode->left = newNode->right = NULL;
else{
nodePtr = root;
Insertion
while (nodePtr != NULL)
{
if (num < nodePtr->value){
if (nodePtr->left!=Null)
nodePtr = nodePtr->left;
else{
nodePtr->left = newNode;
Break;}
}
else if (num > nodePtr->value){
if (nodePtr->right !=Null)
nodePtr = nodePtr->right;
else{
nodePtr->right = newNode;
Break;}
}
else{
cout << "Duplicate value found in tree.\n";
Break;}
}
}
}
Program
• Leaf node
removes a specified item from the BST and adjusts the tree
uses a binary search to locate the target item:
starting at the root it probes down the tree till it finds the
target or reaches a leaf node (target not in the tree)
removal of a node must not leave a ‘gap’ in the tree,
Removal in BST - Pseudocode
method remove (key)
I if the tree is empty return false
II Attempt to locate the node containing the target using the binary search
algorithm
if the target is not found return false
else the target is found, so remove its node:
Case 3:
if the node has a left and a right subtree
- replace the node's value with the max value in the left subtree=Pred
- delete the max node in the left subtree
Removal in BST: Example
parent
7
5 9
Del node
4 6 8 10
7
Removing 4
replace the link in the parent
with null 5 9
6 8 10
Removal in BST: Example
parent
parent
7 7
Del node
5 9 cursor 5 9
6 8 10 6 8 10
Removal in BST: Example
Case 2 : removing a node with 1 EMPTY SUBTREE
Removing 5
parent
parent
7 7
Del node
5 9 5 9
4 8 10 4 8 10
Removal in BST: Example
Del node
7 6
5 9 5 9
4 6 8 10 4 8 10
36
Pseudocode
DeleteKey(x, node *root)
While (curr!=Null)
If (curr-> key==x)
break the loop
Else if (x > curr-> key)
save=curr
curr= curr-> right
Else if (x < curr-> key)
save=curr
curr= curr-> left
If (curr->data == key)
{
Case :1
If (curr->left== Null && curr->right== Null)
{ if (curr==root)
root=Null
if (save->left=current)
save->left=Null
if (save->right=current)
save->right=Null
delete current ;
}
• Delete 7
• Delete 3
• Delete 8
• Delete 14