def insertRecur(self, current, val): if current.val >= val: if current.left is None: current.left = TreeNode(val) return return self.insertRecur(current.left, val) else: if current.right is None: current.right = TreeNode(val) return return self.insertRecur(current.right, val)
def insert(self, value): node = self.get_root() new_node = TreeNode(value) while True: if node.get_value() > value: if node.has_left_child(): node = node.get_left_child() else: node.set_left_child(new_node) break else: if node.has_right_child(): node = node.get_right_child() else: node.set_right_child(new_node) break
def run_tree(): print() print(" apple") print(" / \\") print(" banana cherry") print(" / \ / \\") print(" dates elderberry figs grapes") print() print(end="\n\n") tree = Tree("apple") tree.get_root().set_left_child(TreeNode("banana")) tree.get_root().set_right_child(TreeNode("cherry")) tree.get_root().get_left_child().set_left_child(TreeNode("dates")) tree.get_root().get_left_child().set_right_child(TreeNode("elderberry")) tree.get_root().get_right_child().set_left_child(TreeNode("figs")) tree.get_root().get_right_child().set_right_child(TreeNode("grapes")) print("pre-order: Node -> Left -> Right") print("-"*75) tree.pre_order(tree.root) print(end="\n\n\n") print() print("in-order: Left -> Node -> Right") print("-"*75) tree.in_order(tree.root) print(end="\n\n\n") print() print("post-order: Left -> Right -> Node") print("-"*75) tree.post_order(tree.root) print(end="\n\n\n") print() print("BFS (level order): Traverse all nodes at one level at a time, left to right") print("-"*75) tree.level_order() print(end="\n\n\n")
def __init__(self, root_value=None): self.root = TreeNode(root_value)
def insert_iter(self, data): if self.root: self.root.insert_iter(data) else: self.root = TreeNode(data)
def __init__(self, data): self.root = TreeNode(data)
class BinarySearchTree(object): def __init__(self, data): self.root = TreeNode(data) def insert_iter(self, data): if self.root: self.root.insert_iter(data) else: self.root = TreeNode(data) def insert_rec(self, data): if self.root: self.root.insert_rec(data) else: self.root = TreeNode(data) def search_rec(self, data): return self.root.search_rec(data) if self.root else False def display(self, node): lines, _, _, _ = self._display_aux(node) for line in lines: print(line) def _display_aux(self, node): """ Returns list of strings, width, height, and horizontal coordinate of the root. """ # No child. if node.right is None and node.left is None: line = str(node.data) width = len(line) height = 1 middle = width // 2 return [line], width, height, middle # Only left child. if node.right is None: lines, n, p, x = self._display_aux(node.left) s = str(node.data) u = len(s) first_line = (x + 1) * ' ' + (n - x - 1) * '_' + s second_line = x * ' ' + '/' + (n - x - 1 + u) * ' ' shifted_lines = [line + u * ' ' for line in lines] final_lines = [first_line, second_line] + shifted_lines return final_lines, n + u, p + 2, n + u // 2 # Only right child. if node.left is None: lines, n, p, x = self._display_aux(node.right) s = str(node.data) u = len(s) # first_line = s + x * '_' + (n - x) * ' ' first_line = s + x * '_' + (n - x) * ' ' second_line = (u + x) * ' ' + '\\' + (n - x - 1) * ' ' shifted_lines = [u * ' ' + line for line in lines] final_lines = [first_line, second_line] + shifted_lines return final_lines, n + u, p + 2, u // 2 # Two children. left, n, p, x = self._display_aux(node.left) right, m, q, y = self._display_aux(node.right) s = '%s' % node.data u = len(s) first_line = (x + 1) * ' ' + (n - x - 1) * '_' + s + y * '_' + (m - y) * ' ' second_line = x * ' ' + '/' + (n - x - 1 + u + y) * ' ' + '\\' + (m - y - 1) * ' ' if p < q: left += [n * ' '] * (q - p) elif q < p: right += [m * ' '] * (p - q) zipped_lines = zip(left, right) lines = [first_line, second_line] + \ [a + u * ' ' + b for a, b in zipped_lines] return lines, n + m + u, max(p, q) + 2, n + u // 2
def insert_rec(self, data): if self.root: self.root.insert_rec(data) else: self.root = TreeNode(data)
def create_bst(self): return TreeNode(35, TreeNode(20, TreeNode(15, TreeNode(10), TreeNode(18)), TreeNode(30)), TreeNode(55, TreeNode(40), TreeNode(65)))
def insert(self, val): if self.root is None: self.root = TreeNode(val) self.insertRecur(self.root, val)
def creare_bt(self): return TreeNode(1, TreeNode(2, TreeNode(4), TreeNode(5)), TreeNode(3, TreeNode(6), TreeNode(7)))