def recoverFromPreorder(S: str) -> TreeNode: # print(S) n = len(S) i = 0 while S[i] == "-": i += 1 j = i while j < n and S[j] != "-": j += 1 root = TreeNode(int(S[i: j])) if j == n: return root i = j while S[i] == "-": i += 1 d, j = i - j, i # d is depth of left child, i is start index of left child count = 0 while j < n: if S[j] != "-": if count == d: break count = 0 else: count += 1 j += 1 # j is start index of right child root.left = recoverFromPreorder(S[i: j].rstrip('-')) if j < n: # if right child exists root.right = recoverFromPreorder(S[j:]) return root
def setUp(self): cathy = TreeNode('Cathy') cathy.left = TreeNode('Alex') cathy.right = TreeNode('Frank') sam = TreeNode('Sam') sam.left = TreeNode('Nancy') violet = TreeNode('Violet') violet.left = TreeNode('Tony') violet.right = TreeNode('Wendy') sam.right = violet les = TreeNode('Les') les.left = cathy les.right = sam self.tree = BinaryTree(les)
def sortedArrayToBST(nums: List[int]) -> TreeNode: if not nums: return None n = len(nums) mid = n // 2 root = TreeNode(nums[mid]) if mid > 0: root.left = sortedArrayToBST(nums[:mid]) if mid < n - 1: root.right = sortedArrayToBST(nums[mid + 1:]) return root
def makeBst(array, start, end): mid = ((end - start) / 2) + start currentNode = TreeNode(array[mid], None, None) if start == end: return None if start == mid: return currentNode else: currentNode.left = makeBst(array, start, mid) currentNode.right = makeBst(array, mid + 1, end) return currentNode
def buildTree(preorder: List[int], inorder: List[int]) -> TreeNode: if not preorder: return None root = TreeNode(preorder[0]) if len(preorder) == 1: return root i = inorder.index(preorder[0]) root.left = buildTree(preorder[1:1 + i], inorder[0:i]) root.right = buildTree(preorder[1 + i:], inorder[i + 1:]) return root
def build(nums): if not nums: return None mid = len(nums) // 2 root_val = nums[mid] root = TreeNode(root_val) ltn = nums[0:mid] rtn = nums[mid + 1:] root.left = build(ltn) root.right = build(rtn) return root
def build(preorder, inorder): if not preorder: return None else: root_val = preorder[0] root = TreeNode(root_val) root_val_idx = inorder.index(root_val) ltn = inorder[0:root_val_idx] rtn = inorder[root_val_idx + 1:] root.left = build(preorder[1:len(ltn) + 1], ltn) root.right = build(preorder[len(ltn) + 1:], rtn) return root
def postfixToExpressionTree(postfix): exprTreeStack = [] for e in postfix: node = TreeNode(e) if e in OPERATORS: if e == '!': node.left = exprTreeStack.pop() else: node.right = exprTreeStack.pop() node.left = exprTreeStack.pop() exprTreeStack.append(node) root = exprTreeStack.pop() return root
def postfixToExpressionTree(postfix): exprTreeStack = [] for e in postfix: node = TreeNode(e) if e in OPERATORS: if e == '!': node.left = exprTreeStack.pop() else: node.right = exprTreeStack.pop() node.left = exprTreeStack.pop() exprTreeStack.append(node) root = exprTreeStack.pop() return root
def buildTree(preorder: List[int], inorder: List[int]) -> TreeNode: if not preorder: return None node = TreeNode(preorder[0]) if len(preorder) == 1: return node i = inorder.index(node.val) node.left = buildTree(preorder[1:i + 1], inorder[:i]) node.right = buildTree(preorder[i + 1:], inorder[i + 1:]) return node
def build(io, po): if not po: return None else: root_val = po[-1] root = TreeNode(root_val) root_idx = io.index(root_val) ltn = io[0:root_idx] rtn = io[root_idx + 1:] right_po = po[-1 - 1:-1 - (len(rtn)) - 1:-1][::-1] left_po = po[-1 - (len(rtn)) - 1::-1][::-1] root.left = build(ltn, left_po) root.right = build(rtn, right_po) return root
def gensub(nums): if not nums: return [None] if len(nums) == 1: return [TreeNode(nums[-1])] nodeList = [] for i in range(len(nums)): leftArr = gensub(nums[0:i]) rightArr = gensub(nums[i + 1:]) for l in leftArr: for r in rightArr: node = TreeNode(nums[i]) node.left = l node.right = r nodeList.append(node) return nodeList
def VarianceImpurityHeur(self, examples, targetAttribute, attributes): if (len(examples) == 0): return None root = TreeNode(-1) # The variance impurity calculation of the datapoints varImp = self.getVarImpurity(examples, targetAttribute) # majorityTargetValue of the root node stores the majority value of the targetAttribute root.majorityTargetValue = self.getMostCommonValue(targetAttribute) # If variance impurity is 0, return root node # If no attributes, return root node if varImp == 0 or len(attributes) == 0: return root else: selectedAttribute = self.chooseSelectedAttribute( examples, targetAttribute, attributes, varImp) if int(selectedAttribute) == -1: return root root.val = selectedAttribute # Can have one attribute considered only once in the tree path remainingAttributes = [] for attr in attributes: if attr != selectedAttribute: remainingAttributes.append(attr) attributes = remainingAttributes #splitMatrix[0][0] = indices of the datapoints with value 0 #splitMatrix[1][0] = indices of the datapoints with value 1 #splitMatrix[0][1] = target values corresponding to splitMatrix[0][0] #splitMatrix[1][1] = target values corresponding to splitMatrix[1][0] splitMatrix = self.split(examples, targetAttribute, selectedAttribute) root.left = self.VarianceImpurityHeur(splitMatrix[0][0], splitMatrix[0][1], attributes) root.right = self.VarianceImpurityHeur(splitMatrix[1][0], splitMatrix[1][1], attributes) return root
from treeNode import TreeNode node = TreeNode(5) node.left = TreeNode(3) node.left.left = TreeNode(2) node.left.left.left = TreeNode(1) node.left.right = TreeNode(4) node.right = TreeNode(7) node.right.left = TreeNode(6) node.right.right = TreeNode(8) def printNode(root): if root is None: return printNode(root.left) print(root.val) printNode(root.right) printNode(node) print("=====================") result = node.search(8) print(result) print(type(result))
import unittest from lca import LCA from treeNode import TreeNode T = TreeNode(3) T.left = TreeNode(2) T.left.left = TreeNode(1) T.left.left.left = TreeNode(0.2) T.left.right = TreeNode(2.5) T.right = TreeNode(4) T.right.right = TreeNode(8) ''' 3 / \ 2 4 / \ \ 1 2.5 8 / 0.2 ''' class LCATester(unittest.TestCase): def testLCA(self): self.assertEqual(LCA.findLCA(T, 1, 2.5).data, 2) self.assertEqual(LCA.findLCA(T, 3, 3).data, 3) self.assertEqual(LCA.findLCA(T, 8, 2.5).data, 3) self.assertEqual(LCA.findLCA(T, 4, 3).data, 3) self.assertEqual(LCA.findLCA(T, 10, 2).data, 2) self.assertEqual(LCA.findLCA(T, 2, 1).data, 2)