MCPcopy Create free account
hub / github.com/geekcomputers/Python / insert

Method insert

binary_search_tree.py:25–47  ·  view source on GitHub ↗

Creating a BST with root value as val

(self, val)

Source from the content-addressed store, hash-verified

23 self.root = None
24
25 def insert(self, val):
26 """Creating a BST with root value as val"""
27 # Check if tree has root with None value
28 if self.root is None:
29 self.root = Node(val)
30 # Here the tree already has one root
31 else:
32 current = self.root
33 while True:
34 if val < current.info:
35 if current.left:
36 current = current.left
37 else:
38 current.left = Node(val)
39 break
40 elif val > current.info:
41 if current.right:
42 current = current.right
43 else:
44 current.right = Node(val)
45 break
46 else:
47 break
48
49 def search(self, val, to_delete=False):
50 current = self.root

Callers 1

Calls 1

NodeClass · 0.70

Tested by

no test coverage detected