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

dsa 4

4

Uploaded by

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

dsa 4

4

Uploaded by

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

Mohamed Laamih

23BCE2220
Binary Search Tree(BST) Algorithms
Insertion Algorithm
Insert(node, value):

If node is NULL:

Create a new node with value and return it

Else If value < node.value:

node.left = Insert(node.left, value)

Else If value > node.value:

node.right = Insert(node.right, value)

Return node

Search Algorithm
Search(node, target):

If node is NULL:

Return "Not Found"

If node.value == target:

Return "Found"

Else If target < node.value:

Return Search(node.left, target)

Else:

Return Search(node.right, target)

Deletion Algorithm
Delete(node, value):

If node is NULL:

Return NULL

If value < node.value:


node.left = Delete(node.left, value)

Else If value > node.value:

node.right = Delete(node.right, value)

Else:

# Case 1: No child (leaf node)

If node.left is NULL and node.right is NULL:

Return NULL

# Case 2: One child

Else If node.left is NULL:

Return node.right

Else If node.right is NULL:

Return node.left

# Case 3: Two children

Else:

# Find the minimum value in the right subtree

minNode = FindMin(node.right)

node.value = minNode.value

node.right = Delete(node.right, minNode.value)

Return node

FindMin(node):

While node.left is not NULL:

node = node.left

Return node

You might also like