1class Binarytree:
2 def __init__(self,data):
3 self.data = data
4 self.left = None
5 self.right = None
6
7 def addChild(self, data):
8 if data == self.data:
9 return
10
11 if data < self.data:
12 if self.left:
13 self.left.addChild(data)
14 else:
15 self.left = Binarytree(data)
16 else:
17 if self.right:
18 self.right.addChild(data)
19 else:
20 self.right = Binarytree(data)
21
22 def inorder(self):
23 element = [ ]
24
25 if self.left:
26 element += self.left.inorder()
27
28 element.append(self.data)
29
30 if self.right:
31 element += self.right.inorder()
32
33 return element
34
35 def search(self,val):
36 if val == self.data:
37 return True
38 if val < self.data:
39 if self.left:
40 return self.left.search(val)
41 else:
42 return False
43 else:
44 if self.right:
45 return self.right.search(val)
46 else:
47 return False
48
49def buildtree(element):
50 root = Binarytree(element[0])
51 for i in range(1,len(element)):
52 root.addChild(element[i])
53 return root
54
55if __name__ == '__main__':
56 element = [39, 87, 21, 42, 95, 52, 12]
57 tree = buildtree(element)
58 print(tree.inorder())
59 print(tree.search(38))
1class Binary:
2 def __init__(self, data):
3 self.data = data
4 self.left = None
5 self.right = None
6
7 def addChild(self, data):
8 if data == self.data:
9 return
10 if data < self.data:
11 if self.left:
12 self.left.addChild(data)
13 else:
14 self.left = Binary(data)
15 else:
16 if self.right:
17 self.right.addChild(data)
18 else:
19 self.right = Binary(data)
20
21 def inorder(self):
22 ele = []
23
24 if self.left:
25 ele += self.left.inorder()
26 ele.append(self.data)
27
28 if self.right:
29 ele += self.right.inorder()
30
31 return ele
32
33 def search(self, data):
34 if data == self.data:
35 return True
36 if data < self.data:
37 if self.left:
38 return self.left.search(data)
39 else:
40 return False
41 else:
42 if self.right:
43 return self.right.search(data)
44 else:
45 return False
46
47 def find_min(self):
48 if self.left is None:
49 return self.data
50 return self.left.find_min()
51
52 def find_max(self):
53 if self.right is None:
54 return self.data
55 return self.right.find_max()
56
57 def delete(self, val):
58 if val < self.data:
59 if self.left:
60 self.left = self.left.delete(val)
61 elif val > self.data:
62 if self.right:
63 self.right = self.right.delete(val)
64 else:
65 if self.left is None and self.right is None:
66 return None
67 if self.left is None:
68 return self.right
69 if self.right is None:
70 return self.left
71
72 min_val = self.right.find_min()
73 self.data = min_val
74 self.right = self.right.delete(val)
75
76 return self
77
78
79
80def build(element):
81 root = Binary(element[0])
82 for i in range(1,len(element)):
83 root.addChild(element[i])
84 return root
85
86if __name__ == '__main__':
87 element = [32, 89, 12, 94, 23, 61, 2]
88 tree = build(element)
89 print(tree.inorder())
90 print(tree.search(62))
91 print(tree.find_min())
92 print(tree.find_max())
93 tree.delete(12)
94 print(tree.inorder())
95
1Binary tree - each node can have at most 2 nodes, Binary Search tree - is a binary tree and put smaller values on the left and larger values on the right of the root.
1Binary Tree implementation at this link:
2
3https://github.com/shreyasvedpathak/Data-Structure-Python/tree/master/BinaryTrees
1Binary Search Tree at this link:
2
3https://github.com/shreyasvedpathak/Data-Structure-Python/tree/master/BinaryTrees