(1) 在二叉树中,第i层的结点总数不超过2^(i-1);
(2) 深度为h的二叉树最多有2^h-1个结点(h>=1),最少有h个结点;
(3) 对于任意一棵二叉树,如果其叶结点数为N0,而度数为2的结点总数为N2,
则N0=N2+1;
(4) 具有n个结点的完全二叉树的深度为int(log2n)+1
(5)有N个结点的完全二叉树各结点如果用顺序方式存储,则结点之间有如下关系:
若I为结点编号则 如果I<>1,则其父结点的编号为I/2;
如果2*I<=N,则其左儿子(即左子树的根结点)的编号为2*I;若2*I>N,则无左儿子;
如果2*I+1<=N,则其右儿子的结点编号为2*I+1;若2*I+1>N,则无右儿子。
(6)给定N个节点,能构成h(N)种不同的二叉树。
h(N)为卡特兰数的第N项。h(n)=C(n,2*n)/(n+1)。
”’
Created on 2018年2月25日
@author: Administrator
”’
class Node(object):
”’
classdocs
”’
def __init__(self, value=-1,left=None,right=None):
'''
Constructor
'''
self.item=value
self.child1=left
self.child2=right
class Tree(object):
def init(self):
self.root=None
def add(self, item):
node = Node(item)
if self.root is None:
self.root = node
else:
q = [self.root]
while True:
pop_node = q.pop(0)
if pop_node.child1 is None:
pop_node.child1 = node
return
elif pop_node.child2 is None:
pop_node.child2 = node
return
else:
q.append(pop_node.child1)
q.append(pop_node.child2)
def traverse(self): # 层次遍历
if self.root is None:
return None
q = [self.root]
res = [self.root.item]
while q != []:
pop_node = q.pop(0)
if pop_node.child1 is not None:
q.append(pop_node.child1)
res.append(pop_node.child1.item)
if pop_node.child2 is not None:
q.append(pop_node.child2)
res.append(pop_node.child2.item)
return res
def preorder(self,root): # 先序遍历
if root is None:
return []
result = [root.item]
left_item = self.preorder(root.child1)
right_item = self.preorder(root.child2)
return result + left_item + right_item
def inorder(self,root): # 中序序遍历
if root is None:
return []
result = [root.item]
left_item = self.inorder(root.child1)
right_item = self.inorder(root.child2)
return left_item + result + right_item
def postorder(self,root): # 后序遍历
if root is None:
return []
result = [root.item]
left_item = self.postorder(root.child1)
right_item = self.postorder(root.child2)
return left_item + right_item + result
def inverttree(self, treenode): #真正的翻转只有这8行代码
if treenode == None:
return None
treenode.child1,treenode.child2=treenode.child2,treenode.child1
temp = treenode.child1
treenode.child1 = treenode.child2
treenode.child2 = temp
self.inverttree(treenode.child1)
self.inverttree(treenode.child2)
def invert_tree(self, node):
"""
:type node: TreeNode
:rtype: TreeNode
"""
if node:
node.child1, node.child2 = node.child2, node.child1
if node.child1:
node.left = self.invert_tree(node.child1)
if node.child2:
node.right = self.invert_tree(node.child2)
return node
def print_tree(self,node=None, is_child=False, deep=3):
if not node and is_child:
return
if not is_child:
print (node.item)
if not node.child1 and not node.child2:
return
print ("%s> " % node.item, None if not node.child1 else node.child1.item, None if not node.child2 else node.child2.item)
self.print_tree(node.child1, is_child=True)
self.print_tree(node.child2, is_child=True)
t = Tree()
for i in range(10):
t.add(i)
print(‘层序遍历:’,t.traverse())
print(t.print_tree(t.root))
print(‘先序遍历:’,t.preorder(t.root))
print(‘中序遍历:’,t.inorder(t.root))
print(‘后序遍历:’,t.postorder(t.root))
print(‘翻转遍历:’,t.inverttree(t.root))
print(‘层序遍历:’,t.traverse())
print(t.print_tree(t.root))