0% found this document useful (0 votes)
26 views2 pages

BST Handwritten Style

Binary Search Tree

Uploaded by

eithen.zayyan
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)
26 views2 pages

BST Handwritten Style

Binary Search Tree

Uploaded by

eithen.zayyan
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

Binary Search Tree (BST)

Implementation
1. Insert a Node

def insert(root, value):


if root is None:
return Node(value)
if value < root.data:
root.left = insert(root.left, value)
else:
root.right = insert(root.right, value)
return root

2. Search for a Node

def search(root, value):


if root is None or root.data == value:
return root
if value < root.data:
return search(root.left, value)
return search(root.right, value)

3. In-order Traversal

def inOrderTraversal(root):
if root:
inOrderTraversal(root.left)
print(root.data, end=' ')
inOrderTraversal(root.right)
4. Delete a Node

def deleteNode(root, value):


if root is None:
return root
if value < root.data:
root.left = deleteNode(root.left, value)
elif value > root.data:
root.right = deleteNode(root.right, value)
else:
if root.left is None:
return root.right
elif root.right is None:
return root.left
temp = minValueNode(root.right)
root.data = temp.data
root.right = deleteNode(root.right, temp.data)
return root

def minValueNode(node):
current = node
while current.left is not None:
current = current.left
return current

You might also like