LEC12
Binary Search Trees
A binary tree is a tree where each node has at most two children, referred to as the left
and right child
A binary search tree is a binary tree in which every node's left subtree holds values
less than the root, and every right subtree holds values greater than the root's value.
A new node is added as a leaf.
Binary Search Tree has some special properties: -
Each element in the left subtree of an element is less than the root element of that
subtree.
The left and right subtrees are themselves Binary Search Trees
50, 30, 12, 90, 100, 40, 94
Binary Search Tree (example)
Not a Binary Search Tree
Implementation of Binary Node
A binary search tree need not be full, complete or a two-tree, but it could be any of those
If a binary search tree is full or complete, its height is logarithmic (base 2) in n
Drawback of BST: - Chain BST Search complexity is High.
Building a Binary Search Tree
Insertion strategy:
– If the value of the new element is less than the value of the current node, proceed to
the left subtree (left child) of the node. If null, insert the node as a left child of the
parent.
– If the value of the new element is greater than the value of the current node, proceed
to the right subtree (right child) of the node. If null, insert the node as a left child of
the parent.
Building a Binary Search Tree (continued): -
Insert value > root = Right Side
Insert value < root = Left Side
Question. Sample Insertion?
100, 164, 130, 189, 244, 42, 141, 231, 20, 153
Worst Case Performance
In the worst case a BST can degenerate into a singly linked list.
Performance goes to O(N)
2 3 5 7 11 13 17
Adding to a Binary Search Tree
Searching a Binary Search Tree
Removing elements from a Binary Search Tree