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

New Text Document

Uploaded by

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

New Text Document

Uploaded by

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

class Node:

def __init__(self, key):


self.left = None
self.right = None
self.val = key

class BST:
def __init__(self):
self.root = None

# Insert a new node into the BST


def insert(self, root, key):
# If the tree is empty, create a new node
if root is None:
return Node(key)

# Otherwise, recur down the tree


if key < root.val:
root.left = self.insert(root.left, key)
else:
root.right = self.insert(root.right, key)

return root

# Delete a node from the BST


def delete(self, root, key):
# If the tree is empty
if root is None:
return root

# Find the node to be deleted


if key < root.val:
root.left = self.delete(root.left, key)
elif key > root.val:
root.right = self.delete(root.right, key)
else:
# Node to be deleted found

# Case 1: Node has no children (leaf node)


if root.left is None and root.right is None:
return None

# Case 2: Node has only one child


if root.left is None:
return root.right
elif root.right is None:
return root.left

# Case 3: Node has two children


# Get the inorder successor (smallest in the right subtree)
min_larger_node = self._min_value_node(root.right)
root.val = min_larger_node.val

# Delete the inorder successor


root.right = self.delete(root.right, min_larger_node.val)

return root

# Helper function to find the node with the minimum value


def _min_value_node(self, node):
current = node
while current.left is not None:
current = current.left
return current

# In-order traversal to print the BST elements


def inorder(self, root):
if root:
self.inorder(root.left)
print(root.val, end=" ")
self.inorder(root.right)

# Driver code
if __name__ == "__main__":
bst = BST()

# Inserting elements into the BST


bst.root = bst.insert(bst.root, 50)
bst.root = bst.insert(bst.root, 30)
bst.root = bst.insert(bst.root, 20)
bst.root = bst.insert(bst.root, 40)
bst.root = bst.insert(bst.root, 70)
bst.root = bst.insert(bst.root, 60)
bst.root = bst.insert(bst.root, 80)

# Print the in-order traversal of the BST (sorted order)


print("In-order traversal of the BST:")
bst.inorder(bst.root)
print()

# Deleting node 20 (leaf node)


bst.root = bst.delete(bst.root, 20)
print("\nIn-order traversal after deleting 20:")
bst.inorder(bst.root)
print()

# Deleting node 30 (node with one child)


bst.root = bst.delete(bst.root, 30)
print("\nIn-order traversal after deleting 30:")
bst.inorder(bst.root)
print()

# Deleting node 50 (node with two children)


bst.root = bst.delete(bst.root, 50)
print("\nIn-order traversal after deleting 50:")
bst.inorder(bst.root)
print()

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy