Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
219 views37 pages

Binary Search Trees: Welcome To CS221: Programming & Data Structures

This document provides an overview of binary search trees and operations on binary search trees such as searching, inserting, and deleting nodes. It defines a binary search tree as a binary tree where all left descendants of a node are less than the node's value and all right descendants are greater. The document explains algorithms for searching a node by recursively traversing left or right subtrees based on key values, inserting a new node by adding it in the correct position to maintain the binary search tree properties, and deleting a node by handling cases where the node has zero, one, or two children. Examples are provided to illustrate inserting nodes into an empty binary search tree.

Uploaded by

PRITAM RAJ
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
219 views37 pages

Binary Search Trees: Welcome To CS221: Programming & Data Structures

This document provides an overview of binary search trees and operations on binary search trees such as searching, inserting, and deleting nodes. It defines a binary search tree as a binary tree where all left descendants of a node are less than the node's value and all right descendants are greater. The document explains algorithms for searching a node by recursively traversing left or right subtrees based on key values, inserting a new node by adding it in the correct position to maintain the binary search tree properties, and deleting a node by handling cases where the node has zero, one, or two children. Examples are provided to illustrate inserting nodes into an empty binary search tree.

Uploaded by

PRITAM RAJ
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

Welcome to CS221: Programming & Data Structures

Binary Search Trees


• Topics covered are: Binary Search trees, Operations
Note:
• The slides are adapted from the textbook “Data Structures Using C” by Reema Theraja, Oxford University Press
• Online materials from the home page of Prof. Sartaj Sahni, Distinguished Professor of Computer and Information Sciences and Engineering at the University of Florida.
Definition Of Binary Search Tree

• A binary tree.
• Each node has a (key, value) pair.
• For every node x, all keys in the left
subtree of x are smaller than that in x.
• For every node x, all keys in the right
subtree of x are greater than that in x.
Example Binary Search Tree
20

10 40

6 15 30

2 8 25

Only keys are shown.


Operations on Binary Search Trees
1. Searching for a Node in a Binary Search Tree
2. Inserting a New Node in a Binary Search Tree
3. Deleting a Node from a Binary Search Tree
4. Determining the Height of a Binary Search Tree
5. Determining the Number of Nodes
Searching for an element in Binary Search Tree
Algorithm Description of the algorithm
The searchElement() function is used to find whether a given value
is present in the tree or not. The searching process begins at the
root node. The function first checks if the binary search tree is
empty. If it is empty, then the value we are searching for is not
present in the tree. So, the search algorithm terminates by
displaying an appropriate message. However, if there are nodes in
the tree, then the search function checks to see if the key value of
the current node is equal to the value to be searched. If not, it
checks if the value to be searched for is less than the value of the
current node, in which case it should be recursively called on the
left child node. In case the value is greater than the value of the
current node, it should be recursively called on the right child
node.
• In Step 1, we check if the value stored at the current node of
TREE is equal to VAL or if the current node is NULL, then we
return the current node of TREE. Otherwise, if the value stored
at the current node is less than VAL, then the algorithm is
recursively called on its right sub-tree, else the algorithm is
called on its left sub-tree.
Binary Search Tree Insertion
Algorithm Description of the algorithm
The Insert() function is used to add a new node with a given
value at the correct position in the binary search tree. Adding the
node at the correct position means that the new node should not
violate the properties of the binary search tree.
The insertion function changes the structure of the tree.
Therefore, when the insert function is called recursively, the
function should return the new tree pointer.
• In Step 1 of the algorithm, the insert function checks if the
current node of TREE is NULL. If it is NULL, the algorithm simply
adds the node, else it looks at the current node’s value and then
recursively goes down the left or right sub-tree.If the current
node’s value is less than that of the new node, then the right
sub-tree is traversed, else the left sub-tree is traversed. The
Insert() function continues moving down the levels of a binary
tree until it reaches a leaf node. The new node is added by
following the rules of the binary search trees. That is, if the new
node’s value is greater than that of the parent node, the new
node is inserted in the right sub-tree, else it is inserted in the
left sub-tree.
• The Insert() function requires time proportional to the height of
the tree in the worst case. It takes O(log n) time to execute in
the average case and O(n) time in the worst case.
Example of Binary Search Tree Insertion
Construct a Binary Search Tree by inserting the following sequence of numbers

Insert 10

10
Binary Search Tree Insertion
Insert 12

10

12
Binary Search Tree Insertion
Insert 5

10

5 12
Binary Search Tree Insertion
Insert 4

10

5 12

4
Binary Search Tree Insertion
Insert 20

10

5 12

4 20
Binary Search Tree Insertion
Insert 8

10

5 12

4 8 20
Binary Search Tree Insertion
Insert 7

10

5 12

4 8 20

7
Binary Search Tree Insertion
Insert 15

10

5 12

4 8 20

7 15
Binary Search Tree Insertion

Insert 13

10

5 12

4 8 20

7 15

13
Binary Search Tree – Deletion
Challenge: How to maintain the order after deletion?

25 37
\\ //
10 37
10 65
15 30 65
15 30
Delete node 25
Bad Solution: 30 is out of place
Binary Search Tree – Deletion
Challenge: How to maintain the order after deletion?

25 30
\\ //
10 37 10 37

15 30 65
15 65

Delete node 25
Good Solution
Binary Search Tree – Deletion
Challenge: How to maintain the order after deletion?

33

6 52

2 32 41 66

1 4 9
Binary Search Tree Deletion
Deletion of a node from the binary search tree is possible. However, utmost
care should be taken that the properties of the binary search tree are not
violated and nodes are not lost in the process. Three cases of deleting a node
from BST is illustrated below.
• Case 1: Deleting a Node that has No Children: We can simply remove this
node without any issue.
• Case 2: : Deleting a Node with One Child: To handle this case, the node’s
child is set as the child of the node’s parent. In other words, replace the
node with its child. Now, if the node is the left child of its parent, the node’s
child becomes the left child of the node’s parent. Correspondingly, if the
node is the right child of its parent, the node’s child becomes the right child
of the node’s parent.
• Case 3: Deleting a Node with Two Children: To handle this case, 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). The in-order
predecessor or the successor can then be deleted using any of the above
cases.
Binary Search Tree Deletion
Algorithm Description of the algorithm
• In Step 1 of the algorithm, we first check if TREE=NULL, because if it is true,
then the node to be deleted is not present in the tree. However, if that is not
the case, then we check if the value to be deleted is less than the current
node’s data. In case the value is less, we call the algorithm recursively on the
node’s left sub-tree, otherwise the algorithm. is called recursively on the
node’s right sub-tree.
• Note that if we have found the node whose value is equal to VAL, then we
check which case of deletion it is. If the node to be deleted has both left and
right children, then we find the in-order predecessor of the node by calling
findLargestNode(TREE -> LEFT) and replace the current node’s value with
that of its in-order predecessor. Then, we call Delete(TREE -> LEFT, TEMP ->
DATA) to delete the initial node of the in-order predecessor. Thus, we reduce
the case 3 of deletion into either case 1 or case 2 of deletion.
• If the node to be deleted does not have any child, then we simply set the
node to NULL. Last but not the least, if the node to be deleted has either a
left or a right child but not both, then the current node is replaced by its
child node and the initial child node is deleted from the tree.
• The Delete() function requires time proportional to the height of the tree in
the worst case. It takes O(log n) time to execute in the average case and
Ω(n) time in the worst case.
Binary Search Tree Node – Deletion (1)

Delete 15 from the tree


50

30 55

25 35 53 60

10 32 37 62

15

For leaf nodes, simply delete it.


Binary Search Tree Node – Deletion (1)

After deleting 15 from the tree


50

30 55

25 35 53 60

10 32 37 62

For leaf nodes, simply delete it.


Binary Search Tree Node – Deletion (2)

Delete 60 from the tree

50

30 55

25 35 53 60
60

10 32 37 62

15

For internal nodes with only one child, simply replace


with that child.
Binary Search Tree Node – Deletion (2)

After deleting 60 from the tree

50

30 55

25 35 53 62
60

10 32 37

15
Binary Search Tree Node – Deletion (3)

Delete 10 from the tree

50

30 55

25 35 53 60
60

10 32 37 62

15

For internal nodes with only one child, simply replace


with that child.
Binary Search Tree Node – Deletion (3)

After deleting 10 from the tree

50

30 55

25 35 53 60
60

10
15 32 37 62
Binary Search Tree Node – Deletion (4)

Delete 50 from the tree

50

30 55

25 35 53 60
60

10 32 37 62

15

For internal nodes with two children, we can replace it


with the previous in-order node.
Binary Search Tree Node – Deletion (4)

Delete 50 from the tree

50

30 55

25 35 53 60
60

10 32 37 62

15

For internal nodes with two children, we can replace it


with the previous in-order node.
Binary Search Tree Node – Deletion (4)

After deleting 50 from the tree

37
50

30 55

25 35 53 60
60

10 32 62

15

For internal nodes with two children, we can replace it


with the previous in-order node.
Binary Search Tree Node – Deletion (5)

Delete 37 from the tree

37
50

30 55

25 35 53 60
60

10 32 62

15

For internal nodes with two children, we can also


replace it with the next in-order node.
Binary Search Tree Node – Deletion (5)

Delete 37 from the tree

37
50

30 55

25 35 53 60
60

10 32 62

15

For internal nodes with two children, we can also


replace it with the next in-order node.
Binary Search Tree Node – Deletion (5)

After deleting 37 from the tree

53
37
50

30 55

25 35 60
60

10 32 62

15

For internal nodes with two children, we can also


replace it with the next in-order node.
Binary Search Tree Node – Deletion (6)

Delete 25 from the tree

50

30 55

25 35 53 60
60

10 32 37 62

15

For internal nodes with only one subtree, simply replace


with the root of the subtree.
Binary Search Tree Node – Deletion (6)

After deleting 25 from the tree

50

30 55

10 35 53 60
60

15 32 37 62

For internal nodes with only one subtree, simply replace


with the root of the subtree.
Binary Search Tree Node – Deletion (7)

Delete 55 from the tree

50

30 55

25 35 60
60

10 32 37 62

15

For internal nodes with only one subtree, we can replace


it with the root of the subtree.
Binary Search Tree Node – Deletion (7)

After deleting 55 from the tree

50

30 60

25 35 62
60

10 32 37

15

For internal nodes with only one subtree, we can replace


it with the root of the subtree.
Solve:
1. Create a binary search tree with the input given below:
98, 2, 48, 12, 56, 32, 4, 67, 23, 87, 23, 55, 46.
• Find the result of in-order, pre-order, and post-order traversals of the tree
• Insert 21, 39, 45, 54, and 63 into the tree
• Delete values 23, 56, 2, and 45 from the tree
• Show the deletion of the root node

You might also like