def breathFirstSearch(head): queue = deque() if head is None: return None levelMap = {} levelMap[head] = 1 queue.append(head)
cLevel = 1 cNodes = 0 # setto0 because we need to use +=1 in whiletomake loop run maxa = 0 while queue: cur = queue.popleft() level = levelMap.get(cur)
if level == cLevel: cNodes += 1 # nodes on current level +=1 else: print("level: {}, nodes: {}".format(cLevel, cNodes)) if cNodes > maxa: maxa = cNodes cLevel += 1 cNodes = 1 # cNodes is the first element of the level, (it has been popped)
print("Node: {}, level: {}".format(cur.value, level)) if cur.leftis not None: levelMap[cur.left] = level + 1 queue.append(cur.left) if cur.rightis not None: levelMap[cur.right] = level + 1 queue.append(cur.right)
¶A direct way of finding the successor node and previous node in the in-order traversal
S_uccessor:_ Two situations:
the leftest child in the right tree of a node X is the next node in the traversal of node X.
if the X does not have right tree, go to the parent node, and continue if the parent is always the right child of its parent, until it is the left child of the parent Y. this Y is the next node of X. However, there is no such y, means x does not have the next node.
''' to find the next node of a node in the in-order traversal''' classNode: def__init__(self, val): self.value = val self.left = None self.right = None self.parent = None ''' two situations: 1. the leftest child in the right tree of a node X is the next node in the traversal of node X 2. if the X does not have right tree, go to the parent node, and continue if the parent is always the right child of its parent, until it is the left child of the parent Y. this Y is the next node of X. However, there is no such y, means x does not have the next node. ''' deffindSuccessor(node): if node isNone: returnNone if node.right isnotNone: return getMostLeft(node.right) else: parent = node.parent while parent isnotNoneand node != parent.left: node = parent parent = node.parent return parent
defgetMostLeft(node): if node isNone: returnNone while node.left isnotNone: node = node.left return node
Previous Node: Two situations:
if the node has left child, the prev node is rightest of the left child.
if the node X does not have left child, we keep going to its parent. if the parent is also the left child of its parent, keep going until it is the right child of a parent. the parent here is its previous node X.
Serialization:
serialize the binary tree in the memory into string and
deserialize it from the string to memory.
Structure and Data should be preserved.
'''record a node by _ and do not ignore the None node'''
defpreOrderSerialize(head): if head isNone: return'#_' output = str(head.value) + "_" output += preOrderSerialize(head.left) # output is waiting for further recursion from the left output += preOrderSerialize(head.right) # waiting for the right return output # return output itself to append to the output on the upper level
from collections import deque
defpreOrderDeserialize_String(string): values = string.split('_') queue = deque() for i in values: queue.append(i) return preOrderDeserialize_Tree_subprocess(queue)
defpreOrderDeserialize_Tree_subprocess(queue): value = queue.popleft() if value == '#': returnNone head = Node(int(value)) head.left = preOrderDeserialize_Tree_subprocess(queue) head.right = preOrderDeserialize_Tree_subprocess(queue) return head
应用: 一个树是否是另外一个树的子树
1 2 3 4 5 6 7 8
''' application: is a tree t1 the subtree of another tree t2 ? answer: serialize both and check t1_string is part of t2_string ''' defisSubtreeOf(t1, t2): t1_string = preOrderSerialize(t1) t2_string = preOrderSerialize(t2) return t1_string in t2_string
Test:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
if __name__ == "__main__": head = Node(1) head.left = Node(2) head.right = Node(3) head.left.left = Node(4) head.left.right = Node(5) head.right.left = Node(6) head.right.right = Node(7) serial = preOrderSerialize(head) print("this is a serialized tree: \n{}".format(serial)) head = preOrderDeserialize_String(serial) print("Now the tree has be deserialized, start to check") print(head, head.left, head.right, head.left.left, head.left.right, head.right.left, head.right.right) print("\nnow check if t2 is a subtree of t1:") head_2 = Node(3) head_2.left = Node(6) head_2.right = Node(7) print(isSubtreeOf(head_2, head))
_Fold the paper inwards (always), everytime after the first inward folding, we will have two new fold mark (one inward mark upon and outward mark below every fold marks in current level
(current times of folding))
Solution: the folding process is a tree with inward mark. all heads of left child is inward, and all head of right child is outward_
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
defprintAllFolds(N): printProcess(1, N, True)
defprintProcess(i, N, down):# args: (level, the total times of folding, inwards) if i > N: returnNone printProcess(i+1, N, True) if down: print('inwards') else: print('outwards') printProcess(i+1, N, False)