Skip to content
This repository was archived by the owner on Sep 20, 2024. It is now read-only.

Commit 6e4df7b

Browse files
Update 8_binary_tree.py
1 parent 9bf8289 commit 6e4df7b

File tree

1 file changed

+53
-53
lines changed

1 file changed

+53
-53
lines changed

set3/8_binary_tree.py

+53-53
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,65 @@
11
class BinaryTreeNode:
2+
#implments a complete binary tree
23
def __init__(self, value, left=None, right=None):
3-
self.value=value;
4-
self.left = left
5-
self.right = right
4+
self.value=value;
5+
self.nodes = [self.value]
66

77
def __add__(self, other):
8-
if(self.left == None):
9-
self.left = other
10-
print("Added " +str(other.value) + " as left child of " + str(self.value))
11-
elif(self.right == None):
12-
self.right = other
13-
print("Added " +str(other.value) + " as right child of " + str(self.value))
14-
else:
15-
print("The current node is not vacant")
16-
17-
def __sub__(self, other):
18-
if(self.left == other):
19-
self.left = None
20-
print("Deleted the left node of " + str(self.value))
21-
elif(self.right == other):
22-
self.right = None
23-
print("Deleted the right node of" + str(self.value))
24-
else:
25-
print(str(other.value) +" is not a child of " + str(self.value))
26-
27-
def printTree(self, root):
28-
#inorder traversal of the tree
29-
print(root.value)
30-
if(root.left !=None):
31-
self.printTree(root.left)
32-
if(root.right!=None):
33-
self.printTree(root.right)
34-
35-
root = BinaryTreeNode(1)
36-
a = BinaryTreeNode(2)
37-
b = BinaryTreeNode(3)
38-
c = BinaryTreeNode(4)
8+
#Nodes are added to the end of the tree as in CBT
9+
self.nodes.append(other.value)
10+
#print("Node added at the end of the tree")
3911

40-
root+a#a is added as left child of root
41-
root+b#b is added as right child of root
42-
b + c#c is added as the left child of b
4312

44-
'''Tree is now
45-
1
46-
/ \
47-
2 3
48-
/
49-
4
50-
'''
13+
5114

52-
root.printTree(root)
53-
54-
b-c#c is removed as the left child of b
15+
def __sub__(self, other):
16+
#Since there are no constraints on how to delete nodes
17+
#the specified node is deleted and the last node in the tree is put in its place
18+
if other.value not in self.nodes:
19+
print(str(other.value) + " is not in the tree")
20+
else:
21+
curPosition = self.nodes.index(other.value)
22+
self.nodes[curPosition] = self.nodes[-1]
23+
self.nodes.remove(self.nodes[-1])
24+
print("Node is " + "Deleted")
25+
if(len(self.nodes) == 0):
26+
root = None
5527

56-
'''Tree is now
57-
1
58-
/ \
59-
2 3
60-
'''
6128

62-
root.printTree(root)
29+
def printTree(self, root):
30+
#level order traversal
31+
if(root==None):
32+
print("Tree is empty")
33+
print("*********")
34+
for i in range(len(self.nodes)):
35+
print(self.nodes[i], end = " ")
36+
print(" ")
37+
print("*********")
38+
39+
root = None
40+
while(True):
41+
print("Select on option")
42+
print("1. Insert a node")
43+
print("2. Delete a node")
44+
print("3. Print the tree")
45+
choice = int(input())
46+
if(choice ==1):
47+
if(root==None):
48+
root = BinaryTreeNode(int(input('Enter the value to insert...')))
49+
print("Done inserting at the root")
50+
else:
51+
root + BinaryTreeNode(int(input('Enter the value to insert...')))
52+
print("Node is inserted")
53+
elif(choice == 2):
54+
root - BinaryTreeNode(int(input("Enter the node to be deleted")))
55+
elif(choice ==3):
56+
if(root==None):
57+
print("Tree is empty")
58+
else:
59+
print("The tree is ")
60+
root.printTree(root)
61+
else:
62+
print("Choose a valid option")
6363

6464

6565

0 commit comments

Comments
 (0)