0% found this document useful (0 votes)
2 views9 pages

ds31

Uploaded by

bprajna64
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)
2 views9 pages

ds31

Uploaded by

bprajna64
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/ 9

THE TREE DATA STRUCTURE

Representation of Binary search Tree

The above example of the binary tree represented using Linked list representation is shown as follows,

class BST:
def __init__(self, value):
self.left = None
self.right = None
self.value = value
root=BST(10)
print(root.value)
print(root.left)
print(root.right)
Output:
10
None
None
Time and Space Complexity
The time complexity for the creation of a binary search tree is O(1) because it takes constant time for the
initialization of data variables. The space complexity is O(1) as well since no additional memory is
required.

Subject: SEPP Notes/20CS43P pg. 1


Operations in Binary Search Tree
1. Insertion
2. Search
3. Deletion
4. Traversal (Pre-order, In-order, Post-order)
Insertion
while performing insertion we need to check different scenario
1. The first condition is we need to check whether tree is empty or not, if tree is empty the node which
we are going add will be the first node
2. If tree is not empty at that time, we need to check the position of new node. If the value to be
inserted is less than the node, we will traverse its left subtree recursively. We traverse the right
subtree recursively when the value to be inserted is greater than the node.
Time and Space Complexity
The recursive function searches for the correct position by dividing the subtrees at each level and moving
on to the next level depending on the value of the node. In this way, the number of function calls takes the
form of logN. Hence, the time complexity for insertion is O(logN).
The space complexity is O(logN) as well because every time the function is called, it allocates space in the
stack in memory to determine the next function call.
Algorithm
1. Create a new BST node and assign values to it.
2. insert(data)
i) if tree is empty,
return the new node to the calling function.
ii)If the value to be inserted is less than the node, we will traverse its left subtree recursively.
iii)We traverse the right subtree recursively when the value to be inserted is greater than the node.
3. Finally, return the original root pointer to the calling function.
Code
class BST:
def __init__(self, value):
self.left = None
self.right = None
self.value = value
def insert(self, data):
if self.value:
if data < self.value:
if self.left is None:
self.left = BST(data)
else:
self.left.insert(data)
elif data > self.value:
if self.right is None:
self.right = BST(data)
else:
self.right.insert(data)

Subject: SEPP Notes/20CS43P pg. 2


else:
self.value = data
root = BST(10)
list = [20, 4, 30]
for i in list:
root.insert(i)

Search Operation
If we want to know whether a given node is there or not, we will compare the data of the given node with
that in the root node.
First, we need to search whether the root key is equal to the given data. If the given node is present in the
tree, we can print a message.
If the data is less than the root key, we will search on the left subtree, else, we look at the right subtree.
Algorithm
1. Traverse binary search tree
2. Compare input value with every node of BST.
3. At every node, we will make decision about the traversal of BST
 Input number is equal node data (We found the element).
 Input number is less than node data
 Find the element in left subtree.
 Input number is greater than node data.
 Find the element in right subtree.
Code
class BST:
def __init__(self, value):
self.left = None
self.right = None
self.value = value
def insert(self, data):
if self.value:
if data < self.value:
if self.left is None:
self.left = BST(data)
else:
self.left.insert(data)
elif data > self.value:
if self.right is None:
self.right = BST(data)
else:
self.right.insert(data)
else:
self.value = data
def search(self, data):
if self.value==data:

Subject: SEPP Notes/20CS43P pg. 3


print("Node is found")
return
if data < self.value:
if self.left:
self.left.search(data)
else:
print("Node is not present in tree")
else:
if self.right:
self.right.search(data)
else:
print("Node is not present in tree")
root=BST(10)
list=[20,4,30,15,6]
for i in list:
root.insert(i)
root.search(6)
root.search(25)
Output:
Node is found
Node is not present in tree
Time and space Complexity
The recursive function searches for the correct node by dividing the subtrees at each level and moving on
to the next level depending on the value of the node. In this way, the number of function calls takes the
form of logN. Hence, the time complexity for search is O(logN).
Delete Operation
For deleting a node from a BST, there are some cases, i.e., deleting a root or deleting a leaf node.
Also, after deleting a root, we need to think about the root node.
If we want to delete a leaf node, we can just delete it, but if we want to delete a root, we need to replace the
root’s value with another node. Let’s take the following example:
 Case 1- Node with zero children: here just need to delete the node which has no further children on
the right or left.
 Case 2 – Node with one child: once you delete the node, simply connect its child node with the
parent node of the deleted value.
 Case 3 Node with two children: it works on the following two rules
a. Replace the parent node with largest value in left subtree
b. Replace the parent node with smallest key in right subtree
Algorithm
Step1: if root is empty then print tree is empty
Step2: if not find the position of the data to be deleted
Step3: if node with zero children just need to delete the node
Step4: Node with one child: once you delete the node, simply connect its child node with the parent
node of the deleted value.
Step5: Node with two children: it works on the following two rules

Subject: SEPP Notes/20CS43P pg. 4


 Replace the parent node with largest value in left subtree
 Replace the parent node with smallest key in right subtree
Code
class BST:
def __init__(self, value):
self.left = None
self.right = None
self.value = value
def insert(self, data):
if self.value:
if data < self.value:
if self.left is None:
self.left = BST(data)
else:
self.left.insert(data)
elif data > self.value:
if self.right is None:
self.right = BST(data)
else:
self.right.insert(data)
else:
self.value = data
def inorder(self):
if self.left:
self.left.inorder()
print(self.value, end=" ")
if self.right:
self.right.inorder()
def delete(self, data):
if self.value is None:
print("tree is empty")
return
if data < self.value:
if self.left:
self.left = self.left.delete(data)
else:
print("the given node is not present in tree")
elif data > self.value:
if self.right:
self.right = self.right.delete(data)
else:
print("the given node is not present in tree")
else:
if self.left is None:

Subject: SEPP Notes/20CS43P pg. 5


temp = self.right
self = None
return temp
if self.right is None:
temp = self.left
self = None
return temp
node = self.right
while node.left:
node = node.left
self.value = node.value
self.right = self.right.delete(node.value)
return self
root=BST(10)
list=[6,12,1,16,98,3,7]
for i in list:
root.insert(i)
root.inorder()
root.delete(6)
print()
print("After deleting the node")
root.inorder()
Output:
Tree elements are
1 3 6 7 10 12 16 98
After deleting the node
1 3 7 10 12 16 98
Time and space Complexity
The recursive function searches for the node to be deleted by dividing the subtrees at each level and
moving on to the next level depending on the value of the node. In this way, the number of function calls
takes the form of logN. Hence, the time complexity for search is O(logN).
The space complexity is O(logN) as well because every time the function is called, it allocates space in the
stack in memory to determine the next function call.
Tree Traversal
Traversal means visiting nodes in some specific manner.
Types of Tree Traversal
 Pre-order traversal
For In-order, traverse from the left subtree to the root then to the right subtree.
Inorder => Left, Root, Right.
 Post-order traversal
For Pre-order, traverse from the root to the left subtree then to the right subtree.
Preorder => Root, Left, Right.

 In-order traversal

Subject: SEPP Notes/20CS43P pg. 6


For Post-order, traverse from the left subtree to the right subtree then to the root.
Post order => Left, Right, Root.
Example,

Preorder Traversal- 100 20 10 30 200 150 300


inorder Traversal- 10 20 30 100 150 200 300
Postorder Traversal- 10 30 20 150 300 200 100
Applications of Tree traversal in BST
1. In-order traversal gives nodes in non-decreasing order.
2. Post-order traversal is also useful to get the postfix expression of an expression tree
3. Pre-order traversal is also used to get prefix expression on an expression tree.
Implementation of Tree traversal in BST
Algorithm for Post-order
1. Start from the root node.
2. If the root is empty, return.
3. Traverse the left sub-tree recursively.
4. Traverse the right sub-tree recursively.
5. Print the root node.
6. Stop.
Algorithm for In-order
1. Start from the root node.
2. If the root is empty, return.
3. Traverse the left sub-tree recursively.
4. Print the root node.
5. Traverse the right sub-tree recursively.
6. Stop.
Algorithm for Pre-order
1. Start from the root node.
2. If the root is empty, return.
3. Print the root node.
4. Traverse the left sub-tree recursively.
5. Traverse the right sub-tree recursively.
6. Stop.

Code
class BST:
def __init__(self, value):

Subject: SEPP Notes/20CS43P pg. 7


self.left = None
self.right = None
self.value = value
def insert(self, data):
if self.value:
if data < self.value:
if self.left is None:
self.left = BST(data)
else:
self.left.insert(data)
elif data > self.value:
if self.right is None:
self.right = BST(data)
else:
self.right.insert(data)
else:
self.value = data
def preorder(self):
print(self.value,end=" ")
if self.left:
self.left.preorder()
if self.right:
self.right.preorder()
def inorder(self):
if self.left:
self.left.inorder()
print(self.value,end=" ")
if self.right:
self.right.inorder()
def postorder(self):
if self.left:
self.left.postorder()
if self.right:
self.right.postorder()
print(self.value,end=" ")
root=BST(10)
list=[20,4,30,15,6]
for i in list:
root.insert(i)
print("preorder is")
root.preorder()
print()
print("postorder is")
root.postorder ()

Subject: SEPP Notes/20CS43P pg. 8


print()
print("inorder is ")
root.inorder()
Output:
preorder is
10 4 6 20 15 30
postorder is
6 4 15 30 20 10
inorder is
4 6 10 15 20 30
Time and Space Complexity
The time complexity for tree traversal is O(N) because the function recursively visits all nodes of the tree.
The space complexity for tree traversal is O(N) because the stack holds memory continuously while using a
recursive function.

Questions
1. List and explain Tree terminologies
2. Define tree, binary tree, binary search tree.
3. Explain real time applications of tree.
4. List properties of tree.
5. Demonstrate how data elements are added into the tree.
6. Memory representation of binary tree.
7. Explain a)Insertion b)Search c)Deletion d)Traversal with respect to tree. – algorithm, code,
explanation.
8. Explain different tree traversals.

Subject: SEPP Notes/20CS43P pg. 9

You might also like