0% found this document useful (0 votes)
60 views

Binary Search Tree: CS221 (A) - Data Structures & Algorithms

The document describes the implementation of a binary search tree (BST) data structure. It discusses that BSTs allow for efficient searching and includes operations like insertion, deletion, finding minimum/maximum values, and searching for a value. Pseudocode is provided for implementing these BST operations recursively using pointers to nodes.

Uploaded by

Nouman Khan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views

Binary Search Tree: CS221 (A) - Data Structures & Algorithms

The document describes the implementation of a binary search tree (BST) data structure. It discusses that BSTs allow for efficient searching and includes operations like insertion, deletion, finding minimum/maximum values, and searching for a value. Pseudocode is provided for implementing these BST operations recursively using pointers to nodes.

Uploaded by

Nouman Khan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Binary Search Tree

CS221(A)-Data Structures &


Algorithms
Binary Search Trees
• A binary tree , used for searching.
• Each node in the tree is assigned a key value.
• Keys are assumed as distinct.
Binary Search Trees
• For every node X
in the tree
• Left Sub-tree key 6
values < X < Right 12
Sub-tree key
2 10 13
values.
1 4 8 11

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(TLeft);
• MakeEmpty(TRight);
• 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 < TElement)
– {
• return Find(X, TLeft);
– }else
– if (X > TElement)
– {
• return Find(X, TRight);
– }
– 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 (TLeft == NULL)
– {
• return T;
– }else
• Return FindMin(TLeft);
• }
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 (TRight == NULL)
– {
• return T;
– }else
• Return FindMax(TRight);
• }
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));
• TElement = X;
• TLeft = TRight = NULL;
– }else
– if (X < TElement)
– {
• TLeft = Insert(X,TLeft);
– } else
– if (X > TElement)
– {
• TRight = Insert(X,TRight);
– } 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));
• TElement = X;
• TLeft = TRight = NULL;
– }else
– if (X < TElement)
– {
• TLeft = Insert(X,TLeft);
– } else
– if (X > TElement)
– {
• TRight = Insert(X,TRight);
– } 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));
• TElement = X;
• TLeft = TRight = NULL;
– }else
– if (X < TElement)
– {
• TLeft = Insert(X,TLeft);
– } else
– if (X > TElement)
– {
• TRight = Insert(X,TRight);
– }
• 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 < TElement)
– {
• TLeft = Delete(X,TLeft);
– } else
– if (X > TElement)
– {
• TRight = Delete(X,TRight);
– } else
// contd. On next page
Binary Search Tree Implementation
– If (TLeft && TRight) // left and right child //
– {
• TmpCell = FindMin(TRight);
• TElement = TmpCellElement;
• TRight = Delete(TElement, TRight);
– }
– Else
– {
• TmpCell = T;
• If (TLeft == NULL) // leaves , no children //
• {
– T = TRight;
• }else if (TRight == NULL)
• {
– T = TLeft;
}
• Free(TmpCell);
– }
– return T;
• }

You might also like