10 Binary Search Tree 12052023 031745pm
10 Binary Search Tree 12052023 031745pm
AND ALGORITHMS
DR SAMABIA TEHSIN
BS (AI)
Binary Search Tree
Binary search tree is a data structure that quickly allows us to maintain a
sorted list of numbers.
•It is called a binary tree because each tree node has a maximum of two
children.
•It is called a search tree because it can be used to search for the presence
of a number in O(log(n)) time.
The properties that separate a binary search tree from a regular binary tree is
➢All nodes of left subtree are less than the root node
➢All nodes of right subtree are more than the root node
➢Both subtrees of each node are also BSTs i.e. they have the above two
properties
The binary tree on the right isn't a
binary search tree because the
right subtree of the node "3"
contains a value smaller than it.
Search Operation
The algorithm depends on the property of BST that if each left
subtree has values below root and each right subtree has values
above the root.
If the value is below the root, we can say for sure that the value is
not in the right subtree; we need to only search in the left subtree
and if the value is above the root, we can say for sure that the value
is not in the left subtree; we need to only search in the right subtree.
Search Operation
Algorithm
If root == NULL
return NULL;
If number == root->data
return root->data;
If number < root->data
return search(root->left)
If number > root->data
return search(root->right)
Search Operation
Search for 4
Search Operation
Search for 4
Search Operation
Search for 4
Insert Operation
Inserting a value in the correct position is similar to searching
because we try to maintain the rule that the left subtree is lesser
than root and the right subtree is larger than root.
4 is to be deleted
Deletion Operation: Case 1
6 is to be deleted
Deletion Operation: Case II
Final tree
Deletion Operation: Case III
Case III
In the third case, the node to be deleted has two children. In
such a case follow the steps below:
3 is to be deleted
Deletion Operation: Case III
// Create a node
struct node *newNode(int item) {
struct node *temp = new node;
temp->key = item;
temp->left = temp->right = NULL;
return temp;
}
Binary Search Tree: Code
void inorder(struct node *root) {
if (root != NULL) {
// Traverse left
inorder(root->left); // Inorder Traversal
// Traverse root
cout << root->key << " -> ";
// Traverse right
inorder(root->right);
}
}
Binary Search Tree: Code
struct node *insert(struct node *node, int key) {
// Return a new node if the tree is empty
if (node == NULL)
return newNode(key); Insert a node
// Traverse to the right place and insert the node
if (key < node->key)
node->left = insert(node->left, key);
else
node->right = insert(node->right, key);
return node;
}
Binary Search Tree: Code
// Find the inorder successor
struct node *minValueNode(struct node *node) {
struct node *current = node;
return current;
}
Binary Search Tree: Code
// Deleting a node
struct node *deleteNode(struct node *root, int key) {
// Return if the tree is empty
if (root == NULL) return root;
Item dictionary::retrieve("Washington");
The Dictionary Data Type
When you want to retrieve an
item, you specify the key...
... and the retrieval procedure
returns the item.
Item dictionary::retrieve("Washington");
The Dictionary Data Type
Hampshire
New
Virginia
West
Arkansas
A Binary Search Tree of
States
Storage rules:
Every key to the left of Oklahoma
Colorado
a node is
alphabetically before
the key of the node. Arizona
Mass.
Washington
Hampshire
New
Virginia
West
Arkansas
A Binary Search Tree of
States
Storage rules:
Every key to the left of Oklahoma
Colorado
a node is
alphabetically before
the key of the node. Arizona
Mass.
Washington
Example:
“Massachusetts” and
Hampshire
“ New Hampshire”
New
Virginia
West
Arkansas
are alphabetically
before “Oklahoma”
A Binary Search Tree of
States
Storage rules:
Every key to the left of
Oklahoma
a node is Colorado
alphabetically before
the key of the node. Arizona Mass.
Washington
Every key to the right
of a node is
Hampshire
New
alphabetically after
Virginia
West
Arkansas
the key of the node.
A Binary Search Tree of
States
Storage rules:
Every key to the left of
Oklahoma
a node is Colorado
alphabetically before
the key of the node. Arizona Mass.
Washington
Every key to the right
of a node is
Hampshire
New
alphabetically after
Virginia
West
Arkansas
the key of the node.
Retrieving Data
Start at the root.
❑ If the current node has
the key, then stop and Colorado Oklahoma
Hampshire
❑ If the current node's key
New
Virginia
is too small, move right
West
Arkansas
Hampshire
❑ If the current node's key
New
Virginia
is too small, move right
West
Arkansas
Hampshire
❑ If the current node's key
New
Virginia
is too small, move right
West
Arkansas
Hampshire
❑ If the current node's key
New
Virginia
is too small, move right
West
Arkansas
Hampshire
❑ If the current node's key
New
Virginia
is too small, move right
West
Arkansas
Hampshire
New
there had been a
Virginia
West
Arkansas
node.
Adding
Iowa
Hampshire
New
there had been a
Virginia
West
Arkansas
node.
Adding
Iowa
Hampshire
New
there had been a
Virginia
West
Arkansas
node.
Adding
Iowa
Hampshire
New
there had been a
Virginia
West
Arkansas
node.
Adding
Iowa
Hampshire
New
there had been a
Virginia
West
Arkansas
node.
Adding
Iowa
Hampshire
New
there had been a
Virginia
West
Arkansas
node.
Adding
❑ Pretend that you are
trying to find the key,
but stop when there is Colorado Oklahoma
Hampshire
New
there had been a
Virginia
West
Arkansas
node.
Adding
Mass.
Arizona
Washington
Iowa
Hampshire
New
Virginia
West
Arkansas
Adding
Kazakhstan is the
new right child Colorado Oklahoma
of Iowa?
Mass.
Arizona
Washington
Iowa
Hampshire
New
Virginia
West
Arkansas
Removing an Item with a
Given Key
❑ Find the item.
❑ If necessary, swap the Oklahoma
Colorado
item with one that is
easier to remove.
Mass.
Iowa
Hampshire
New
Virginia
West
Arkansas
Removing "Florida"
❑ Find the item.
Colorado Oklahoma
Mass.
Arizona
Washington
Iowa
Hampshire
New
Virginia
West
Arkansas
Removing "Florida"
Colorado Oklahoma
Mass.
Arizona
Washington
Florida cannot be
Iowa
removed at the
Hampshire
New
moment...
Virginia
West
Arkansas
Removing "Florida"
Colorado Oklahoma
Mass.
Arizona
... because removing Washington
Hampshire
break the tree into
New
Virginia
two pieces.
West
Arkansas
Removing "Florida"
❑ If necessary, do some
rearranging.
Colorado Oklahoma
Mass.
Arizona
The problem of Washington
Hampshire
happens because
New
Virginia
Florida has 2 children.
West
Arkansas
Removing "Florida"
❑ If necessary, do some
rearranging.
Colorado Oklahoma
Mass.
Arizona
Washington
For the rearranging,
Iowa
take the smallest item
Hampshire
New
in the right subtree...
Virginia
West
Arkansas
Removing "Florida"
Iowa
❑ If necessary, do some
rearranging.
Colorado Oklahoma
Mass.
Arizona
Washington
...copy that smallest
Iowa
item onto the item
Hampshire
New
that we're removing...
Virginia
West
Arkansas
Removing "Florida"
Iowa
❑ If necessary, do some
rearranging.
Colorado Oklahoma
Mass.
Arizona
Washington
... and then remove
the extra copy of the
Hampshire
New
item we copied...
Virginia
West
Arkansas
Removing "Florida"
Iowa
❑ If necessary, do some
rearranging.
Colorado Oklahoma
Mass.
Arizona
Washington
Hampshire
the tree
New
Virginia
West
Arkansas
Removing "Florida"
Colorado Oklahoma
Mass.
Arizona
Why did I choose Washington
Hampshire
New
in the right subtree?
Virginia
West
Arkansas
Removing "Florida"
Iowa
Colorado Oklahoma
Mass.
Arizona
Washington
Because every key
must be smaller than
Hampshire
the keys in its
New
Virginia
right subtree
West
Arkansas
Removing an Item with a
Given Key
❑ Find the item.
❑ If the item has a right child, rearrange the
tree:
◦ Find smallest item in the right subtree
◦ Copy that smallest item onto the one that you
want to remove
◦ Remove the extra copy of the smallest item
(making sure that you keep the tree connected)
else just remove the item.
Credits and Acknowledgements
https://www.gatevidyalay.com
https://www.programiz.com/