class Node(object): def __init__(self, value): self.value = value self.left = None self.right = None class BinaryTree(object): def __init__(self, root): self.root = Node(root) def search(self, find_val): return self.preorder_serach(tree.root, find_val) def print_tree(self): return self.preorder_print(tree.root, "")[:-1] def preorder_search(self, start, find_val): if start: if start.value == find_val: return True else return self.preorder_search(start.left, find_val) or self.preorder_search(start.right, find_val) return False def preorder_print(self, start, traversal): if start: traversal += (str(start.value) + "-") traversal = self.preorder_print(start.left, traversal) traversal = self.preorder_print(start.right, traversal) return traversal
Binary Search Tree
class Node(object): def __init__(self, value): self.value = value self.left = None self.right = None class BST(object): def __init__(self, root): self.root = Node(root) def insert(self, new_val): self.insert_helper(self.root, new_val) def insert_helper(self, current, new_val): if current_value < new_val: if current.right: self.insert_helper(current.right, new_val) else: current.right = Node(new_val) else: if current.left: self.insert_helper(current.left, new_val) else: current.left = Node(new_val) def search(self, find_val): return self.search_helper(self.root, find_val) def search_helper(self, current, find_val): if current: if current.value == find_val: return True elif current.value < find_val: return self.search_helper(current.right, find_val) else: return self.search_helper(current.left, find_val) return False
graph(network): how things is connect one another which is similar to tree.
0[0, 1, 0, 0]
1[1, 0, 1, 1]
2[0, 1, 0, 1]
3[0, 1, 1, 0]