PPT6 - Binary Search Tree
PPT6 - Binary Search Tree
// X is found
if ( X == curr->data ) return curr;
• Inserting new
• key (35)
Operations:
Insertion – Example
Operations:
Insertion – Example
Operations:
Insertion – Example
Operations:
Deletion
• There are 3 cases which should be considered:
If the key is in a leaf, just delete that node
If the key is in a node which has one child, delete that node
and connect its child to its parent
If the key is in a node which has two children, find the right
most child of its left sub tree (node P), replace its key with
P’s key and remove P recursively. (or alternately you can
choose the left most child of its right sub tree)
Operations:
Deletion
Algorithm:
Step 1: IF TREE = NULL, then
Write “VAL not found in the tree”
ELSE IF VAL < TREE->DATA
Delete(TREE->LEFT, VAL)
ELSE IF VAL > TREE->DATA
Delete(TREE->RIGHT, VAL)
ELSE IF TREE->LEFT AND TREE->RIGHT
SET TEMP = findLargestNode(TREE->LEFT)
SET TREE->DATA = TEMP->DATA
Delete(TREE->LEFT, TEMP->DATA)
Operations:
Deletion
Algorithm:
ELSE
SET TEMP = TREE
IF TREE->LEFT = NULL AND TREE ->RIGHT = NULL
SET TREE = NULL
ELSE IF TREE->LEFT != NULL
SET TREE = TREE->LEFT
ELSE
SET TREE = TREE->RIGHT
FREE TEMP
Step 2: End
Operations:
Deletion – Example
Linked
representation of
the binary tree
Threaded Binary Tree
Concept
• In one way threading, a thread will appear either in the right
field or the left field of the node.
• A one way threaded tree is also called a single threaded tree.
• If the thread appears in the left field, then the left field will be
made to point to the in-order predecessor of the node.
• Such a one way threaded tree is called a left threaded binary
tree.
• On the contrary, if the thread appears in the right field, then it
will point to the in-order successor of the node. Such a one
way threaded tree is called a right threaded binary tree.
Threaded Binary Tree
Concept