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

Binary Search Tree: Reny Jose

The document discusses binary search trees (BSTs), including their properties, operations, and algorithms. A BST is a binary tree where the left subtree of a node contains only nodes with keys less than the node's key, and the right subtree contains only nodes greater than the node's key. Common BST operations include searching, inserting, and deleting nodes in a way that maintains the BST properties. Insertion and deletion can have different cases depending on the number of children of the node.

Uploaded by

Reny Jose
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
554 views

Binary Search Tree: Reny Jose

The document discusses binary search trees (BSTs), including their properties, operations, and algorithms. A BST is a binary tree where the left subtree of a node contains only nodes with keys less than the node's key, and the right subtree contains only nodes greater than the node's key. Common BST operations include searching, inserting, and deleting nodes in a way that maintains the BST properties. Insertion and deletion can have different cases depending on the number of children of the node.

Uploaded by

Reny Jose
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 36

Binary Search Tree

Reny Jose
Binary Search Tree 
• Binary Search Tree is a node-based
binary tree data structure which has the
following properties:
• The left sub tree of a node contains only
nodes with keys lesser than the node’s key.
• The right sub tree of a node contains only
nodes with keys greater than the node’s
key.
• The left and right sub tree each must also
be a binary search tree.
Binary Search Tree Property
• Stored keys must satisfy the binary search
tree property. 56
•  y in left subtree of x, then key[y]  key[x].
•  y in right subtree of x, then key[y]  key[x]. 26 200

18 28 190 213

12 24 27

Comp 122, Spring 2004


Binary Search tree or not
Create the binary search tree
• Create the binary search tree using the following data elements 43,
10, 79, 90, 12, 54, 11, 9, 50.
• Insert 43 into the tree as the root of the tree.
• Read the next element, if it is lesser than the root node element, insert it as
the root of the left sub-tree.
• Otherwise, insert it as the root of the right of the right sub-tree.
Create the binary search tree using the following data elements
43, 10, 79, 90, 12, 54, 11, 9, 50.
Create a binary search tree using the following data elements:
45, 39, 56, 12, 34, 78, 32, 10, 89, 54, 67, 81
45, 39, 56, 12, 34, 78, 32, 10, 89, 54, 67, 81
Operations on Binary Search Tree
• Searching in BST Finding the location of some specific element in a
binary search tree.
• Insertion in BST Adding a new element to the binary search tree at the
appropriate location so that the property of BST do not violate.
• Deletion in BST Deleting some specific node from a binary search
tree. However, there can be various cases in deletion depending upon
the number of children, the node have.
Searching in BST
• The search function is used to find whether a given value
is present in the tree or not.
1. Compare the element with the root of the tree.
2. If the item is matched then return the location of the node.
3. Otherwise check if item is less than the element present on
root, if so then move to the left sub-tree.
4. If not, then move to the right sub-tree.
5. Repeat this procedure recursively until match found.
6. If element is not found then return NULL.
Example
Algorithm
Searching a node with the value 40
Tree Search 56

Tree-Search(x,
Tree-Search(x,k) k)
1.
1. ififxx==NIL
NILor
orkk==key[x]
key[x] 26 200

2.
2. then thenreturn
returnxx
3.
3. ififkk<<key[x]
key[x]
18 28 190 213
4.
4. then thenreturn
returnTree-Search(left[x],
Tree-Search(left[x],k)
k)
5.
5. else elsereturn
returnTree-Search(right[x],
Tree-Search(right[x],k)k)
12 24 27
Running time: O(h)

Aside: tail-recursion

Comp 122, Spring 2004


Inserting a New Node in a Binary Search
Tree
1. Allocate the memory for tree.
2. Set the data part to the value and set the left and right pointer of
tree, point to NULL.
3. If the item to be inserted, will be the first element of the tree, then
the left and right of this node will point to NULL.
4. Else, check if the item is less than the root element of the tree, if
this is true, then recursively perform this operation with the left of
the root.
5. If this is false, then perform this operation recursively with the
right sub-tree of the root.
Insert data 95
Insert data 16
e.g., insert 11

• Implementing
insertion
resursively
Does the
order of
inserting
elements
into a tree
matter?
• Unbalanced trees are not desirable
because search time increases!
• Advanced tree structures, such as
red-black trees, guarantee balanced
trees.
Deletion
• Delete function is used to delete the specified node
from a binary search tree.
• There are three situations of deleting a node from
binary search tree.
• The node to be deleted is a leaf node
• The node to be deleted has only one child.
• The node to be deleted has two children.
The node to be deleted is a leaf node

• Replace the leaf node with the NULL and simple free the allocated
space.
The node to be deleted has only one child.

• Replace the node with its child and delete the child node
Deleting a node with only one child
The node to be deleted has two children.
1. Replace the node’s value with its in-order predecessor (largest value
in the left sub-tree) or in-order successor (smallest value in the right
sub-tree).
2. After the procedure, replace the node with NULL and free the
allocated space.
Deleting a node with two children (cont.)

• Find predecessor (i.e., rightmost node in the left subtree)


• Replace the data of the node to be deleted with predecessor's data
• Delete predecessor node
What is Predecessor and Successor
• If X has two children its predecessor is the maximum value in its left
subtree and its successor the minimum value in its right subtree
If it does not have a left child a nodes
predecessor is its first left ancestor

• In-order predecessor of 17 is 15 ,
• Predecessor of 6 is 4
• Predecessor of 13 is 9.
The in order predecessor is a node with maximum value in left subtree

Case 1: if given node is left most leaf node of tree, there is no in order predecessor, in that case return NULL
Case 2: Node has left subtree. In that case, maximum value in left subtree will be predecessor of given node.

We can find maximum value in tree by going deep down right subtree, till right subtree is NULL, and then return the
last node.

Case 3: node does not have left subtree but it is also not the leftmost leaf node
Then parent of given node will be inorder predecessor.
.
Algorithm Step 1
Delete (TREE, VAL) 1. if TREE=NULL, the node to be deleted is not present in the tree.
Step 1:
1. IF TREE = NULL
2. If the value is less, we call the algorithm recursively on the node’s
Write "VAL not found in the tree" left sub-tree,
2. ELSE IF VAL < TREE DATA 3. If the value is greater we call the algorithm recursively on the
Delete(TREE->LEFT, VAL) node’s right sub-tree
3. ELSE IF VAL > TREE DATA 4. If we have found the node whose value is equal to VAL, then we
Delete(TREE RIGHT, VAL) check which case of deletion it is.
4. ELSE IF TREE LEFT AND TREE RIGHT a) If the node to be deleted has both left and right children, then
1. SET TEMP = findLargestNode(TREE LEFT)
b) we find the in-order predecessor of the node by calling
2. SET TREE DATA = TEMP DATA
3. Delete(TREE LEFT, TEMP DATA)
findLargestNode(TREE -> LEFT)
5. ELSE c) replace the current node’s value with that of its in-order
1. SET TEMP = TREE predecessor.
2. IF TREE LEFT = NULL AND TREE RIGHT = d) call Delete(TREE -> LEFT, TEMP -> DATA) to delete the initial
NULL node of the in-order predecessor.
SET TREE = NULL 5. If the node to be deleted does not have any child, then we simply
3. ELSE IF TREE LEFT != NULL set the node to NULL.
SET TREE = TREE LEFT
6. if the node to be deleted has either a left or a right child but not
4. ELSE
SET TREE = TREE RIGHT
both, then the current node is replaced by its child node and the
5. [END OF IF] initial child node is deleted from the tree.
6. FREE TEMP
6. [END OF IF]
Step 2: END
3 is to be deleted
Home work
• Consider the following numbers

98, 2, 48, 12, 56, 32, 4, 67, 23, 87, 23, 55, 46
1. Create a binary search tree with the given input
2. Insert 21, 39, 45, 54, and 63 into the tree
3. Delete values 23, 56, 2, and 45 from the tree
4. Demonstrate the binary traversal
References
• https://www.javatpoint.com/binary-search-tree
• https://makeinjava.com/find-inorder-predecessor-binary-search-tree-
bst-examples
/
• https://youtu.be/jBDc4Uuif28

You might also like