def stringToTree(s, loc): #print s[loc[0]:] #print loc[0] if loc[0] >= len(s): return None ind = 0 while loc[0] + ind < len(s) and (s[loc[0] + ind] != '(' and s[loc[0] + ind] != ')'): ind += 1 #print ind #print s[loc[0]:(loc[0]+ind)] res = Node(s[loc[0]:(loc[0] + ind)]) loc[0] = loc[0] + ind if s[loc[0]] == '(': loc[0] = loc[0] + 1 res.left = stringToTree(s, loc) loc[0] = loc[0] + 1 if s[loc[0]] == '(': loc[0] = loc[0] + 1 res.right = stringToTree(s, loc) loc[0] = loc[0] + 1 return res
def insert_rec(self, n: Node, val: int): if val > n.val: if n.right is None: n.right = Node(val) return self.insert_rec(n.right, val) if val < n.val: if n.left is None: n.left = Node(val) return self.insert_rec(n.right, val)
def build_tree(data, centers, distances, valid_centers, valid_data): node = Node() if valid_centers.sum() == 1: node.value = np.argmax(valid_centers) return node dim, cut, cost = best_cut(data, valid_data, centers, valid_centers, distances) node.feature = dim node.value = cut n = data.shape[0] left_valid_data = np.zeros(n, dtype=bool) right_valid_data = np.zeros(n, dtype=bool) for i in range(n): if valid_data[i]: if data[i,dim] <= cut: left_valid_data[i] = True else: right_valid_data[i] = True k = centers.shape[0] left_valid_centers = np.zeros(k, dtype=bool) right_valid_centers = np.zeros(k, dtype=bool) for i in range(k): if valid_centers[i]: if centers[i, dim] <= cut: left_valid_centers[i] = True else: right_valid_centers[i] = True node.left = build_tree(data, centers, distances, left_valid_centers, left_valid_data) node.right = build_tree(data, centers, distances, right_valid_centers, right_valid_data) return node
if not tree: return tree if tree.val < minVal: tree = trimBST(tree.right, minVal, maxVal) elif tree.val > maxVal: tree = trimBST(tree.left, minVal, maxVal) else: tree.left = trimBST(tree.left, minVal, maxVal) tree.right = trimBST(tree.right, minVal, maxVal) return tree tree = Node(8) t6 = Node(6) t6.left = Node(4) t6.right = Node(7) t3 = Node(3) t3.left = Node(1) t3.right = t6 t14 = Node(14) t14.left = Node(13) t10 = Node(10) t10.right = t14 tree.left = t3 tree.right = t10 levelOrderPrint(tree) trimBST(tree, 5, 13) levelOrderPrint(tree)
def preorder_recursive(root): if not root: return print(root.val) preorder_recursive(root.left) preorder_recursive(root.right) parser = argparse.ArgumentParser() parser.add_argument('--recursive', "-r", action='store_true') parser.add_argument('--iterative', "-i", action='store_true') args = parser.parse_args() root = Node(1) root.left = Node(2) root.right = Node(3) root.left.left = Node(4) root.left.right = Node(5) if not args.recursive and not args.iterative: print("Add \"--recursive\" or \"--iterative\" flag to command") elif args.recursive and args.iterative: print("Choose \"--recursive\" or \"--iterative\" as flag") elif args.recursive: print("Recursive preorder traversal") preorder_recursive(root) elif args.iterative: print("Iterative preorder traversal") preorder_iterative(root)
currentCounter -= 1 print(item.val, end=" ") if item.left: q.append(item.left) nextCount += 1 if item.right: q.append(item.right) nextCount += 1 if currentCounter == 0: print() currentCounter = nextCount nextCount = 0 if __name__ == "__main__": tree = Node(1) t2 = Node(2) t2.left = Node(4) t3 = Node(3) t3.left = Node(5) t3.right = Node(6) tree.left = t2 tree.right = t3 levelOrderPrint(tree) #result should be: #1 #2 3 #4 5 6