Binary Search Tree
Binary Search Tree
≤2 0
≤2 1
≤2 2
≤2 3
What is the total number of nodes N
of a full tree with height h?
h −1
N = 2 + 2 + ... + 2
0 1
= 2 −1
h
l=0 l=1 l=h-1
2 −1 = N
h
⇒ 2 = N +1
h
⇒ h = log( N + 1) → O (log N )
Why is h important?
The Tree operations like insert, delete, retrieve etc.
are typically expressed in terms of the height of the
tree h.
So, it can be stated that the tree height h determines
running time!
What is the max height of a tree
with N nodes?
N (same as a linked list)
What is the min height of a tree with
N nodes?
log(N+1)
Binary Search
Suppose DATA is an array which is sorted in increasing
numerical order and we want to find the location LOC of a given
ITEM in DATA. Then there is an extremely efficient searching
algorithm called Binary Search.
Binary Search
Consider that you want to find the location of some word in a
dictionary.
I guess you are not fool enough to search it in a linear way or in
other words apply linear search. That is no one search page by
page from the start to end of the book.
Rather, I guess you will open or divide the dictionary in the
middle to determine which half contains the word.
Then consider new probable half and open or divide that half in
the middle to determine which half contains the word. This
process goes on.
Eventually you will find the location of the word and thus you
are reducing the number of possible locations for the word in the
dictionary.
How to search a binary tree?
1. Start at the root
2. Search the tree level by level,
until you find the element you
are searching for or you reach
a leaf.
No O(N)
Binary Search Trees
Binary Search Tree Property:
The value stored at
a node is greater than
the value stored at its
left child and less than
the value stored at its
right child
Binary Search Trees
22 99
11 44 88
Binary Tree Search Algorithm
x=root Example: k =4 and x=root
66 Call TREE-SEARCH(root,4)
Now x.key=6 then k<x.key
So call TREE-SEARCH(x.left,k)
22 99
11 44 88
Binary Tree Search Algorithm
Example: k =4 and x=root.left
66 Call TREE-SEARCH(x,4)
x=root.left Now x.key=2 then k>x.key
TREE-SEARCH(x.right,k)
22 99
11 44 88
Binary Tree Search Algorithm
Example: k =4 and x=root.left.right
66 Call TREE-SEARCH(x,4)
x=root.left.right Now x.key=4 then k=x.key
Search terminates
22 99
11 44 88
Binary Tree Search Algorithm
Example: k =4 and x=root.left.right
Now x.key=4 then k=x.key
66 Search terminates and
x=root.left.right x=root.left.right is the desired
node or location
22 99
11 44 88
Animation of How Works in a
Sorted Data Array
BST - Pseudo code
if the tree is empty
return NULL
else if the key value in the node is greater than the target
return the result of searching the left subtree
else if the key value in the node is smaller than the target
return the result of searching the right subtree
Search in a BST: C code
Ptnode search(ptnode root,
int key)
{
/* return a pointer to the node that
contains key. If there is no such
node, return NULL */
TREE-MINIMUM(x)
1. while x.left ≠ NIL
2. x = x.left
3. return x
Maximum Key or Element
We can always find an element in a binary search tree
whose key is maximum by following the right children from
the root until we encounter a NIL.
Otherwise if a node x has no right subtree then the value
x.key contained in root x is the maximum key or element.
The procedure for finding the maximum key:
TREE-MAXIMUM(x)
1. while x.right ≠ NIL
2. x = x.right
3. return x
Insert a value into the BST
To insert a new value v into a binary search tree T, we use
the procedure TREE-INSERT.
The procedure takes a node z for which z.key=v , z.left=NIL
and z.right=NIL.
It modifies T and some of the attributes of z in such a way
that it inserts z into an appropriate position in the tree
Insert a value into the BST
Suppose v is the value we want to insert and z is the node (New
or NIL) we are supposed to find to insert the value v.
z.p denotes the parent of z.
X is a pointer that traces a simple path downward the tree and y
is the trailing pointer as the parent of x. T.root denote the root
of the tree.
Now the intention is to find a new or NIL node that will satisfy
the BST property after placing the value v. The procedure first
consider x as the root of the tree thus the parent of the root
y=NIL.
In steps 3 to 7 the procedure causes the two pointer y and x to
move down the tree, going left or right depending on the
comparison of z.key with x.key, until x becomes NIL.
Insert a value into the BST
Now this NIL occupies the position z, where we wish to place the
input item.
At this time we need y the parent of the desired node. This is why
at step four we always storing the parent of current node x while
moving downward. At the end of step 7 (in step 8) we make this
node the parent of z (z.p).
From steps 9 to 11:
Now if tree is empty (y==NIL) then create a root node with the
new key(T.root=z)
If the value v is less than the value of the parent(z.key<y.key)
then make it as the left-child of the parent(y.left=z)
If the value v is greater than the value of the parent(z.key>y.key)
then make it as the right-child of the parent(y.right=z)
Insert a value into the BST
TREE-INSERT(T , z)
1. y=NIL
2. x= T.root
3. While x ≠ NIL
4. y=x
5. if z.key<x.key
6. x=x.left
7. else x=x.right
8. z.p=y
9. if y== NIL
10. T.root = z
11.elseif z.key < y.key
12. y.left=z
13.else y.right = z
BST Insertion Algorithm
We are supposed to insert
66 an item value 5 and find an
appropriate node z for it
22 99
11 44 88
BST Insertion Algorithm
x=T.root
Y=NIL and x=T.root
66
22 99
11 44 88
BST Insertion Algorithm
x=T.root
Now x≠NIL and z.key<x.key
66 That is [5<6]
22 99
11 44 88
BST Insertion Algorithm
y Y=T.root and x=T.root.left
66 And z.key>x.key[5>2]
x
22 99
11 44 88
BST Insertion Algorithm
Y=T.root.left and
y 66 x=T.root.left.right
x And z.key>x.key[5>4]
22 99
11 44 88
BST Insertion Algorithm
Y=T.root.left.right and x=NIL
66 Then z.p=T.root.left.right
y
22 99
11 44 88
BST Insertion Algorithm
Y=T.root.left.right [y ≠ NIL]
66 And z.key>y.key [5>4]
So y.right=z that is
y T.root.left.right.right=z
22 99
11 44 88
z 5
Insertion in BST – Pseudo code
if tree is empty
create a root node with the new key
else
compare key with the top node
if key = node key
replace the node with the new value
else if key > node key
compare key with the right subtree:
if subtree is empty create a leaf node
else add key in right subtree
else key < node key
compare key with the left subtree:
if the subtree is empty create a leaf node
else add key to the left subtree
Insertion into a BST: C code
void insert (ptnode *node, int key)
{
ptnode ptr,
temp = search(*node, key);
if (temp || !(*node)) {
ptr = (ptnode) malloc(sizeof(tnode));
if (IS_FULL(ptr)) {
fprintf(stderr, “The memory is full\n”);
exit(1);
}
ptr->key = key;
ptr->left = ptr->right = NULL;
if (*node)
if (key<temp->key) temp->left=ptr;
else temp->right = ptr;
else *node = ptr;
}
}
Delete a value from the BST
Removing a node from a BST is a bit more complex, since we do
not want to create any "holes" in the tree. The intention is to
remove the specified item from the BST and adjusts the tree
The binary search algorithm is used 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)
If the node has one child then the child is spliced to the parent
of the node. If the node has two children then its successor has
no left child; copy the successor into the node and delete the
successor instead TREE-DELETE (T, z) removes the node
pointed to by z from the tree T. IT returns a pointer to the node
removed so that the node can be put on a free-node list
The overall strategy for deleting a node or item from a binary
search tree can be described through some cases.
Delete a value from the BST
Experimenting the cases:
Removing 4
Parent
77 77
Z
55 99 55 99
44 66 88 10
10 66 88 10
10
Delete a value from the BST
Case 2: removing a node with 2 SUBTREES
- replace the node's value with the max value in the left subtree
- delete the max node in the left subtree
Z Removing 7
77 66
55 99 55 99
44 66 88 10
10 44 88 10
10
Delete a value from the BST
Case 3: if the node has no left child
- link the parent of the node to the right (non-empty) subtree
Parent
Parent
Removing 5
Z
77 77
55 99 66 99
66 88 10
10 88 10
10
Delete a value from the BST
Case 4: if the node has no right child
- link the parent of the node to the left (non-empty) subtree
Parent
Parent
Removing 5
Z
77 77
55 99 99
44
44 88 10
10 88 10
10
BST Deletion Algorithm
• TREE-DELETE (T, z)
1. if left [z] = NIL .OR. right[z] = NIL
2. then y ← z
3. else y ← TREE-SUCCESSOR (z)
4. if left [y] ≠ NIL
5. then x ← left[y]
6. else x ← right [y]
7. if x ≠ NIL
8. then p[x] ← p[y]
9. if p[y] = NIL
10. then root [T] ← x
11. else if y = left [p[y]]
12. then left [p[y]] ← x
13. else right [p[y]] ← x
14. if y ≠ z
15. then key [z] ← key [y]
16. if y has other field, copy them, too
17. return y
Analysis of BST Operations
The complexity of operations search, insert and
remove in BST is O(h) , where h is the height.
When the tree is balanced then it is O(log n). The
updating operations cause the tree to become
unbalanced.
The tree can degenerate to a linear shape and the
operations will become O (n)
Applications for BST
Binary Search Tree: Used in many search applications where data is
constantly entering/leaving, such as the map and set objects in many
languages' libraries.
Binary Space Partition: Used in almost every 3D video game to
determine what objects need to be rendered.
Binary Tries: Used in almost every high-bandwidth router for storing
router-tables.
Heaps: Used in implementing efficient priority-queues. Also used in
heap-sort.
Huffman Coding Tree:- used in compression algorithms, such as
those used by the .jpeg and .mp3 file-formats.
GGM Trees - Used in cryptographic applications to generate a tree of
pseudo-random numbers.
Syntax Tree: Constructed by compilers and (implicitly) calculators to
parse expressions.
What is a Degenerate BST?
A degenerate binary search tree is one where most or
all of the nodes contain only one sub node.
It is unbalanced and, in the worst case, performance
degrades to that of a linked list.
If your add node function does not handle rebalancing,
then you can easily construct a degenerate tree by
feeding it data that is already sorted.
Does the order of inserting
elements into a tree matter?
Yes, certain orders might produce very
unbalanced trees or degenerated trees!
Unbalanced trees are not desirable because search
time increases!
Advanced tree structures, such as red-black
trees, AVL trees, guarantee balanced trees.
Does the
order of
inserting
elements
into a tree
matter?
Better Search Trees
Prevent the degeneration of the BST :
A BST can be set up to maintain balance during updating
operations (insertions and removals)
Types of ST which maintain the optimal performance in
other words balanced trees:
– splay trees
– AVL trees
– 2-4 Trees
– Red-Black trees
– B-trees
References