BST_TREE
BST_TREE
to None.
● right: A reference to the right child node,
initialized to None.
2. The BinarySearchTree class should have an
__init__ method that initializes an empty binary
search tree. The __init__ method should perform
the following task:
● Set the root attribute of the BinarySearchTree
python
Copy code
class Node:
self.value = value
self.left = None
self.right = None
2.
3. Define the BinarySearchTree Class:
○ Define a BinarySearchTree class, which will
manage the nodes and operations of the BST.
4. Initialize the BinarySearchTree:
○ Create an __init__ method within the
BinarySearchTree class.
○ This method will initialize a new binary search
tree by setting up an empty root attribute,
initially set to None.
○ Setting self.root to None indicates that the
tree is empty when it’s first created and doesn’t
contain any nodes yet.
python
Copy code
class BinarySearchTree:
def __init__(self):
self.root = None
5.
Explanation of the Initialization Algorithm
● Purpose: The initialization algorithm for a BST
prepares an empty tree where nodes can be added
later.
● State: After calling BinarySearchTree(), the tree’s
root attribute is None, indicating that the tree has no
nodes initially.
● Outcome: A BinarySearchTree object is created,
and it’s ready for operations such as insertion, search,
and deletion.
class Node:
def __init__(self,value):
self.value=value
self.left=None
self.right=None
class BinarySearchTree:
def __init__(self):
self.root=None
BST: Insert
Implement the insert method for the BinarySearchTree class
that inserts a new node with a given value into the binary search
tree.
self.value = value
self.left = None
self.right = None
class BinarySearchTree:
def __init__(self):
self.root = None
new_node = Node(value)
# Step 2: Check if the BST is empty
if self.root is None:
return True
temp = self.root
while True:
if value == temp.value:
return False # Duplicate value, insertion not allowed
if temp.left is None:
return True
else:
if temp.right is None:
temp.right = new_node # Insert new node as right
child
return True
else:
1. Initialize temp:
○ Set temp to point to the root of the tree. This variable
will be used to traverse the tree.
2. Traverse the Tree:
○ Enter a loop to traverse the tree. Continue until temp
becomes None or the target value is found.
3. Compare Target Value with Current Node:
○ If the target value is equal to temp.value, return
True, as the value exists in the tree.
○ If the target value is less than temp.value, update
temp to point to temp.left, moving left in the tree.
○ If the target value is greater than temp.value, update
temp to point to temp.right, moving right in the tree.
4. End of Loop:
○ If the loop ends without finding the target value, return
False, as the value does not exist in the tree.
class Node:
def __init__(self, value):
self.value = value
self.left = None
self.right = None
class BinarySearchTree:
def __init__(self):
self.root = None
# Traverse the tree until the target is found or we reach a leaf node
while temp is not None:
# Check if the target value matches the current node's value
if value == temp.value:
return True # Value exists in the tree
# Move left if target value is less than current node's value
elif value < temp.value:
temp = temp.left