binary tree in python
class Binarytree:
def __init__(self,data):
self.data = data
self.left = None
self.right = None
def addChild(self, data):
if data == self.data:
return
if data < self.data:
if self.left:
self.left.addChild(data)
else:
self.left = Binarytree(data)
else:
if self.right:
self.right.addChild(data)
else:
self.right = Binarytree(data)
def inorder(self):
element = [ ]
if self.left:
element += self.left.inorder()
element.append(self.data)
if self.right:
element += self.right.inorder()
return element
def search(self,val):
if val == self.data:
return True
if val < self.data:
if self.left:
return self.left.search(val)
else:
return False
else:
if self.right:
return self.right.search(val)
else:
return False
def buildtree(element):
root = Binarytree(element[0])
for i in range(1,len(element)):
root.addChild(element[i])
return root
if __name__ == '__main__':
element = [39, 87, 21, 42, 95, 52, 12]
tree = buildtree(element)
print(tree.inorder())
print(tree.search(38))
0
0
class Binary:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
def addChild(self, data):
if data == self.data:
return
if data < self.data:
if self.left:
self.left.addChild(data)
else:
self.left = Binary(data)
else:
if self.right:
self.right.addChild(data)
else:
self.right = Binary(data)
def inorder(self):
ele = []
if self.left:
ele += self.left.inorder()
ele.append(self.data)
if self.right:
ele += self.right.inorder()
return ele
def search(self, data):
if data == self.data:
return True
if data < self.data:
if self.left:
return self.left.search(data)
else:
return False
else:
if self.right:
return self.right.search(data)
else:
return False
def find_min(self):
if self.left is None:
return self.data
return self.left.find_min()
def find_max(self):
if self.right is None:
return self.data
return self.right.find_max()
def delete(self, val):
if val < self.data:
if self.left:
self.left = self.left.delete(val)
elif val > self.data:
if self.right:
self.right = self.right.delete(val)
else:
if self.left is None and self.right is None:
return None
if self.left is None:
return self.right
if self.right is None:
return self.left
min_val = self.right.find_min()
self.data = min_val
self.right = self.right.delete(val)
return self
def build(element):
root = Binary(element[0])
for i in range(1,len(element)):
root.addChild(element[i])
return root
if __name__ == '__main__':
element = [32, 89, 12, 94, 23, 61, 2]
tree = build(element)
print(tree.inorder())
print(tree.search(62))
print(tree.find_min())
print(tree.find_max())
tree.delete(12)
print(tree.inorder())
Thank you!
0
0
3
2
Binary 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.
Thank you!
2
0
4.5
6
Binary Tree implementation at this link:
https://github.com/shreyasvedpathak/Data-Structure-Python/tree/master/BinaryTrees
Thank you!
6
0
4
8
Binary Search Tree at this link:
https://github.com/shreyasvedpathak/Data-Structure-Python/tree/master/BinaryTrees
Thank you!
8
0
Are there any code examples left?
New code examples in category Python
-
Python 2023-04-11 03:04:20
-
Python 2022-03-27 22:40:04 pycharm no module named
-
Python 2022-03-27 22:25:05 assign multiple variablesin one line
-
Python 2022-03-27 22:20:02 levenshtein distance
-
Python 2022-03-27 21:35:09 get text from url python last slash
-
Python 2022-03-27 21:30:30 df concatenate df
-
Python 2022-03-27 21:25:09 python odd or even
-
Python 2022-03-27 21:15:32 python include function from another file
-
Python 2022-03-27 21:10:01 color module python
-
Python 2022-03-27 21:00:27 python tkinter cursor types