|
1 | 1 | class BinaryTreeNode:
|
| 2 | + #implments a complete binary tree |
2 | 3 | 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] |
6 | 6 |
|
7 | 7 | 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") |
39 | 11 |
|
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 |
43 | 12 |
|
44 |
| -'''Tree is now |
45 |
| - 1 |
46 |
| - / \ |
47 |
| - 2 3 |
48 |
| - / |
49 |
| - 4 |
50 |
| -''' |
| 13 | + |
51 | 14 |
|
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 |
55 | 27 |
|
56 |
| -'''Tree is now |
57 |
| - 1 |
58 |
| - / \ |
59 |
| - 2 3 |
60 |
| -''' |
61 | 28 |
|
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") |
63 | 63 |
|
64 | 64 |
|
65 | 65 |
|
0 commit comments