Tree (BST) Implementation Half
Tree (BST) Implementation Half
M. Ehtasham Ul Haq
Chenab College of Engineering and Technology (CCET)
Introduction
When we store ordered data in an array, we
have a very efficient search algorithm, the
binary search.
However, we have very inefficient insertion
and deletion algorithms that require shifting
data in the array.
To provide for efficient insertions and
deletions, we developed the linked list.
The problem with linked lists, however, is
that their search algorithms are sequential
searches, which are very inefficient.
Cant we get the best of both worlds (i.e.,
Key
Key Left
Left Right
Right
Key
Key LeftRight
Key
Key LeftRight
LeftRight
8
4
6
2
1
8
4
a) 22 > 17
b) 11 < 17
c) 11 > 6
d) 22 > 17
Insertion
Search
Traversal
Deletion
Find Minimum
Find the item that has the minimum value in the tree
Find Maximum
t
2
NULL
NULL
NULL
NULL
t
1
NULL
NULL
NULL
NULL
NULL
New Node
}
}
}
If so, go right?
current = current.rightChild;
if(current == null) // if no right child
{
parent.rightChild = newNode;.
return; // we are done if we get here.
}
// end if
}
// end else
// end while
// end else not root
// end insert()