0% found this document useful (0 votes)
9 views15 pages

BST_TREE

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)
9 views15 pages

BST_TREE

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/ 15

BST: Constructor

Create a BinarySearchTree class.

The BinarySearchTree class should contain the following


components:

1. A Node class, which serves as the building block for


the binary search tree. The Node class should have an
__init__ method that initializes the following
attributes:
● value: The value of the node.

● left: A reference to the left child node, initialized

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

class to None, indicating that the tree is initially


empty.
Algorithm for Initializing a Binary Search Tree

1. Define the Node Class:


○ Define a Node class, which represents each
individual node in the tree.
○ Each node has:
■ A value property that holds the data for the
node.
■ A left pointer, initially set to None, which
will point to the node’s left child.
■ A right pointer, initially set to None, which
will point to the node’s right child.

python
Copy code
class Node:

def __init__(self, value):

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.

The method should perform the following tasks:

1. Create a new instance of the Node class using the provided


value.
2. If the binary search tree is empty (i.e., self.root is None),
set the root attribute of the BinarySearchTree class to point
to the new node and return True.
3. If the binary search tree is not empty, initialize a temporary
variable temp to point to the root node, and then perform the
following steps in a loop until the new node is inserted:
● If the value of the new node is equal to the value of the
current node (stored in temp), return False, indicating
that duplicate values are not allowed in the tree.
● If the value of the new node is less than the value of the
current node, check if the left child of the current node
is None:
● If it is, set the left child of the current node to the
new node and return True.
● If it is not, update temp to point to the left child
and continue the loop.
● If the value of the new node is greater than the value of
the current node, check if the right child of the current
node is None:
● If it is, set the right child of the current node to the
new node and return True.
● If it is not, update temp to point to the right child
and continue the loop.

Explanation of Each Step

1. Create a New Node:


○ Create an instance of the Node class with the provided
value.
2. Check if Tree is Empty:
○ If self.root is None, the tree is empty. Set
self.root to the new node and return True,
indicating the insertion was successful.
3. Traverse the Tree:
○ Initialize a temporary pointer, temp, to self.root and
enter a loop.
4. Duplicate Check:
○ If the value of the new node matches the value of the
temp node, return False (no duplicates are allowed).
5. Move to Left Child:
○ If the value of the new node is less than temp.value,
check if temp.left is None.
■ If temp.left is None, set it to new_node and
return True.
■ Otherwise, update temp to temp.left and
continue the loop.
6. Move to Right Child:
○ If the value of the new node is greater than
temp.value, check if temp.right is None.
■ If temp.right is None, set it to new_node and
return True.
■ Otherwise, update temp to temp.right and
continue the loop.
class Node:

def __init__(self, value):

self.value = value

self.left = None

self.right = None

class BinarySearchTree:

def __init__(self):

self.root = None

def insert(self, value):

# Step 1: Create a new node with the given value

new_node = Node(value)
# Step 2: Check if the BST is empty

if self.root is None:

self.root = new_node # Set root to new node

return True

# Step 3: If BST is not empty, use temp to traverse the tree

temp = self.root

while True:

# Step 4: Check for duplicate value

if value == temp.value:
return False # Duplicate value, insertion not allowed

# Step 5: If value is less, go left

elif value < temp.value:

if temp.left is None:

temp.left = new_node # Insert new node as left child

return True

else:

temp = temp.left # Move to the left child

# Step 6: If value is greater, go right

else: # value > temp.value

if temp.right is None:
temp.right = new_node # Insert new node as right
child

return True

else:

temp = temp.right # Move to the right child


BST: Contains

Implement the contains method for the BinarySearchTree class that


checks if a node with a given value exists in the binary search tree.

The method should perform the following tasks:

1. Initialize a temporary variable temp to point to the root node of


the binary search tree.
2. Use a loop to traverse the binary search tree until the target value
is found or the end of the tree is reached:
● If the target value is less than the value of the current node
(stored in temp), update temp to point to the left child and
continue the loop.
● If the target value is greater than the value of the current
node, update temp to point to the right child and continue
the loop.
● If the target value is equal to the value of the current node,
return True, indicating that the target value exists in the
tree.
3. If the loop ends without finding the target value, return False,
indicating that the target value does not exist in the tree.
Algorithm for contains Method

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

def insert(self, value):


new_node = Node(value)
if self.root is None:
self.root = new_node
return True
temp = self.root
while True:
if value == temp.value:
return False # Duplicate value
elif value < temp.value:
if temp.left is None:
temp.left = new_node
return True
temp = temp.left
else:
if temp.right is None:
temp.right = new_node
return True
temp = temp.right

def contains(self, value):


# Initialize temp to the root of the BST
temp = self.root

# 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

# Move right if target value is greater than current node's value


else:
temp = temp.right

# If the loop ends, the value is not in the tree


return False

You might also like