Review the creation of a binary tree

1  '' ' 
2  tree:
 3      each node has zero or more child nodes
 4      nodes without a parent node are called root nodes
 5      every non-root node has only one parent node
 6      except the root node In addition, each child node can be divided into multiple disjoint subtrees.
 7 The  nature of the binary tree:
 8 There are      at most 2 ^ (i-1) nodes in the i-th layer of the binary tree.
 9      The binary tree of depth k has at most 2 ^ k -1 node
 10      leaf nodes N0 degree 2 nodes N2
 11          N0 = N2 + 1
 12      the depth of a complete binary tree with n nodes is log2 (n + 1)
 13      complete binary tree:
 14 is          numbered as i's node
 15              left child-> 2i
 16              right child-> 2i + 1
 17 The          left child's parent node number must be i / 2
 18  
19 '' ' 
20 is  class the Node (Object):
 21 is      '' ' defines a node has a left child and a right child ' '' 
22 is      DEF  the __init__ (Self, Data):
 23 is          # node data 
24          self.data = Data
 25          # left and right child is null point 
26 is          self.lchild = None
 27          self.rchild = None
 28  
29  class BinaryTree (Object):
 30      '' ' defined binary ' '' 
31 is      DEF  the __init__ (Self):
 32          # root defaults Empty 
33          self.root =None
 34 is  
35      DEF the Add (Self, Data):
 36          # add data to a binary tree to finally add data 
37          # processing sequence: parent node left child and right child 
38 is          Node = the Node (Data)
 39          # If empty tree 
40          IF self.root iS None:
 41 is              self.root = node
 42 is              # empty tree, the root node in the added data 
43 is              return 
44 is          Queue = [self.root]
 45          # add the root node, as a sign of the presence of the node 
46 is          the while queue:
 47              # If the queue is not empty 
48             = cur_node queue.pop (0)
 49              # current root node point, take the first element 
50              IF cur_node.lchild IS None:
 51 is                  # If left empty node 
52 is                  cur_node.lchild = Node
 53 is                  return 
54 is              the else :
 55                  # added to the pointer, prove the existence of a left node 
56 is                  queue.append (cur_node.lchild)
 57 is              IF cur_node.rchild iS None:
 58                  # if the right node is empty 
59                  cur_node.rchild = node
 60                  return 
61 is              the else:
 62                  # added to the pointer, prove the existence of a right node 
63 is                  queue.append (cur_node.rchild)

2020-04-17

Guess you like

Origin www.cnblogs.com/hany-postq473111315/p/12723432.html