Binary Search Tree: CS221 (A) - Data Structures & Algorithms
Binary Search Tree: CS221 (A) - Data Structures & Algorithms
3 5 7 9
Binary Search Trees Implementation
• typedef struct TreeNode *PtrToNode;
• typedef struct *PtrToNode SearchTree;
• typedef struct *PtrToNode Position;
• struct TreeNode
• {
– ElementType Element;
– TreeNode Left;
– TreeNode Right;
• }
Binary Search Tree Implementation
• SearchTree MakeEmpty(SearchTree T)
• {
– if (T != NULL)
–{
• MakeEmpty(TLeft);
• MakeEmpty(TRight);
• free(T);
–}
– return NULL;
• }
Binary Search Tree Implementation
• Find Value
– Requires returning a pointer to the node in tree T
that has the key value.
– If T is Null return NULL, if the key stored at T is X,
we can return T. Otherwise depending on the less
than and greater than relationship we can traverse
the tree recursively either on the left sub tree or
the right subtree.
Binary Tree Implementation
• Position Find(ElementType X, SearchTree T)
• {
– if (T == NULL)
– {
• return NULL;
– }
– if (X < TElement)
– {
• return Find(X, TLeft);
– }else
– if (X > TElement)
– {
• return Find(X, TRight);
– }
– else
• return T;
• }
Binary Search Tree Implementation
• Find Minimum Value
– Get us the minimum or smallest key value in the
tree.
– For Find Minimum, start at the root and go left as
long as there is a left child.
Binary Tree Implementation
• Position FindMin(SearchTree T)
• {
– if (T == NULL)
– {
• return NULL;
– }else
– if (TLeft == NULL)
– {
• return T;
– }else
• Return FindMin(TLeft);
• }
Binary Search Tree Implementation
• Find Maximum Value
– Get us the maximum or largest key value in the
tree.
– For Find Maximum start at the root and go right as
long as there is a right child.
Binary Tree Implementation
• Position FindMax(SearchTree T)
• {
– if (T == NULL)
– {
• return NULL;
– }else
– if (TRight == NULL)
– {
• return T;
– }else
• Return FindMax(TRight);
• }
Binary Search Tree Implementation
• Insert (Duplication Not Allowed)
– Proceed down the tree in similar fashion as we did
in Find Function.
– Insert X at the last spot on the path traversed.
Binary Search Tree Implementation
• SearchTree Insert(ElementType X, SearchTree T)
• {
– if (T == NULL)
– {
• T = malloc(sizeof(struct TreeNode));
• TElement = X;
• TLeft = TRight = NULL;
– }else
– if (X < TElement)
– {
• TLeft = Insert(X,TLeft);
– } else
– if (X > TElement)
– {
• TRight = Insert(X,TRight);
– } else
• return T;
What's wrong with this code ?
• }
Binary Search Tree Implementation
• SearchTree Insert(ElementType X, SearchTree T)
• {
– if (T == NULL)
– {
• T = malloc(sizeof(struct TreeNode));
• TElement = X;
• TLeft = TRight = NULL;
– }else
– if (X < TElement)
– {
• TLeft = Insert(X,TLeft);
– } else
– if (X > TElement)
– {
• TRight = Insert(X,TRight);
– } else
• return T;
What's wrong with this code ?
• }
Binary Search Tree Implementation
• SearchTree Insert(ElementType X, SearchTree T)
• {
– if (T == NULL)
– {
• T = malloc(sizeof(struct TreeNode));
• TElement = X;
• TLeft = TRight = NULL;
– }else
– if (X < TElement)
– {
• TLeft = Insert(X,TLeft);
– } else
– if (X > TElement)
– {
• TRight = Insert(X,TRight);
– }
• return T;
• }
Binary Search Tree Implementation
• Delete
– If the node is a leaf , it can be deleted
immediately.
– If the node has one child, the node can be deleted
after its parent adjust a pointer to bypass the
node.
– If the node has two children, replace the data of
this node with the smallest data of the right sub-
tree and recursively delete that node.
Binary Search Tree Implementation
• Delete
Node to be deleted
R
T U
Binary Search Tree Implementation
• Delete
Node to be deleted
R
R
T U
Before Deletion
T
U
After Deletion
Binary Search Tree Implementation
• Delete
6
2 8
1 4
Node to be deleted
3
Binary Search Tree Implementation
• Delete
6 6 6
2 8 8
2 2 8
1 4 1 4 1 4
3 3
3
Node to be deleted
Binary Search Tree Implementation
• Delete 6
2 8
1 5
Node to be deleted
3
4
Binary Search Tree Implementation
• Delete
6 6 6
2 8 2 8 2 8
1 5 1 5 1 5
3 3 3
4 4
4
Node to be deleted
Binary Search Tree Implementation
• SearchTree Delete(ElementType X, SearchTree T)
• {
– Position TempCell
– if (T == NULL)
– {
• Error(“Element Not Found”);
– }else
– if (X < TElement)
– {
• TLeft = Delete(X,TLeft);
– } else
– if (X > TElement)
– {
• TRight = Delete(X,TRight);
– } else
// contd. On next page
Binary Search Tree Implementation
– If (TLeft && TRight) // left and right child //
– {
• TmpCell = FindMin(TRight);
• TElement = TmpCellElement;
• TRight = Delete(TElement, TRight);
– }
– Else
– {
• TmpCell = T;
• If (TLeft == NULL) // leaves , no children //
• {
– T = TRight;
• }else if (TRight == NULL)
• {
– T = TLeft;
}
• Free(TmpCell);
– }
– return T;
• }