Binary tree is a data structure of a very important data structure, in the use of Python to build a decision tree model, found that the need to implement the fork tree, so back to look at the two-fork tree, with the following results.
I built this node-type data first, and he has several properties and functions:
(1) attribute: Name, data, left child node, right child node, parent node, child node number (degree);
(2) Method: Add child nodes and delete child nodes, and the number of child nodes changes, the child node's parent node becomes the node.
I need to make my two-fork tree have this feature:
(1) attribute: depth, root node, all pluggable node dictionaries {name: data}, all node dictionary {name: data}
(2) Methods:
Using an existing node or a node with multiple nodes to generate the tree structure, calculate the depth of the tree at any time, make a subtree by using one of the nodes of the tree, or insert or delete a subtree on one of the inserted nodes of the tree; print the tree structure
The following code
#-*-coding:utf-8-*-' created by ZWG in 2016-10-7 ' Import copy Class Node:def __init__ (Self,name,data): Self.data=data Self.name=name Self. Rchild=none self. Lchild=none self.child_number=0 self.parent=none def add_rchild (self,node): if self. Rchild is not none:self. Rchild=node else:self. Rchild=node self.child_number+=1 node.set_parent (self) def drop_rchild (self): self. Rchild=none self.child_number-=1 def set_parent (Self,node): Self.parent=node def add_lchild (self,n ODE): If self. Lchild is not none:self. Lchild=node else:self. Lchild=node self.child_number+=1 node.set_parent (self) def drop_lchild (self): self. Lchild=none Self.child_number-=1 class Tree:def __init__ (Self,node): "Initialize to use a child-free The root of a node as a tree can also use one that itself contains a multilayered tree structurenode to build the tree each node contains two primary attributes of name and data, and two nodes All_node for the node Enable_node as pluggable "self.parent=no De self.depth=1 self.all_node={node.name:node} self.enable_node={node.name:node} C1=NODE.R Child C2=node. Lchild C=[C1,C2] B=[i for i in C if I am not None] If Len (B) ==2:del self.enable_node[
Node.name] While Len (b)!=0:self.depth+=1 c=copy.copy (b) for i in B: C.remove (i) self.all_node[i.name]=i if i.child_number!=2:self. Enable_node[i.name]=i if I.rchild is not none:c.append (i.rchild) If I .
Lchild is not none:c.append (i.lchild) b=copy.copy (C) def get_depth (self): ' Compute the depth of the tree ' depth=1 node=self.parent c1=node. Rchild C2=node.
LchildC=[C1,C2] B=[i for i in C if I was not None] while Len (B)!=0:depth+=1 c=copy.copy ( B) for I-b:c.remove (i) if I.rchild is not None:c.appe
nd (i.rchild) If I.lchild is not none:c.append (i.lchild) b=copy.copy (C)
Return Depth def show (self): ' Print tree structure ' a=[copy.deepcopy (self.parent)]
N=copy.deepcopy (self.depth) m=copy.copy (n) print self.parent.name.center (2**n*m) while n>=1: B=[] N-=1 for i in A:If I am not None:c1=i.lchil D B.append (C1) If C1 is not none:print C1.name.center (2**
n*m), Else:print '. Center (2**n*m), C2=i.rchild
B.append (C2) If C2 is not none:print c2.name.center (2**n*m), Else:
print '. Center (2**n*m), Else:print '. Center (2**N*M), print '. Center (2**n*m), a=copy.deepcopy (b) print ' \ n ' #del a,n,b def gener Ate_childtree (self,child_name): "Select Child_name this node to generate a subtree, the root node of the subtree is Child_node" "ch Ild_node=self.all_node[child_name] Child_tree=tree (Child_node) return child_tree def add_child_tree (SE Lf,parent_name,child_tree,rl= ' right '): ' Add the subtree tree can be a single node tree, or it can be a multi-layer node tree ' l1=child_ Tree.all_node L2=child_tree.enable_node l4=child_tree.parent Parent_node=self.all_node[parent_name ] If rl== ' right ': Parent_node.add_rchild (L4) If rl== ' left ': parent_node.add_lchild (L4) for I in L1. Keys (): Self.all_node[i]=l1[i] for I in L2.keys (): Self.enable_node[i]=l2[i] If P Arent_node.child_number==2:self.enable_node.pop (Parent_node) self.depth=self.get_depth () def drop _child_tree (self,child_name): "Delete the subtree, child_name the node and all subsequent child nodes of the deleted section to delete the ' ch ' Ild_node=self.all_node[child_name] Child_tree=tree (child_node) L1=child_tree.all_node L2=child_tre E.enable_node parent_node=child_node.parent if Parent_node. Rchild==child_node:parent_node.drop_rchild () Else:parent_node.drop_Lchild () for I in L1.keys (): Self.all_node.pop (L1[i].name) for I in L2:self.enable_node.pop (l1[i].name
If not Self.enable_node.has_key (parent_node.name): Self.enable_node[parent_node.name]=parent_node Self.depth=self.get_depth () If __name__== ' __main__ ': A=node (' A ', 1) a1=node (' A1 ', 2) a2=node (' A2 ', 2) a11=node (' A11 ', 3) a12=node (' A12 ', 3)
A21=node (' A21 ', 3) a111=node (' a111 ', 4) a112=node (' a112 ', 4) a211=node (' a211 ', 4) a212=node (' a212 ', 4)
A11.add_lchild (a111) a11.add_rchild (a112) a21.add_lchild (a211) a21.add_rchild (a212) a.add_lchild (A1) A.add_rchild (A2) A1.add_rchild (A11) a1.add_lchild (A12) a2.add_rchild (A21) "' verifies that node properties and methods are correct PRI NT A.lchild.name print a.rchild.name print a.child_number print a.parent print A1. Rchild.name print A.rchild.rchild.name ' ' #生成node关于a的树, A has 4 layers t=tree (a) Print t.depth print T . All_node.keys () print T.enable_node.keys () #T. Show () #打印树 #生成node关于b的树, A has a two-storey b=node (' B ', 5); B1=node (' B1 ', 6); B2=node (' B2 ', 6) B.add_lchild (B1); B.add_rchild (B2) b_tree=tree (b) #b_tree. Show () #打印树 #生成树T的子树 to A1
As a node, the depth of the A1 is 3 t1=t.generate_childtree (' A1 ') Print t1.depth print T1.all_node.keys () print T1.enable_node.keys () #t1. Show () #打印树 #增加子树, in a111 Followed by a subtree b_tree, at which point the height of the tree is 6 t.add_child_tree (' a111 ', B_tree, ' left ') print t.depth print T.enable_node.keys () prin T T.all_node.keys () #T. Show () #打印树 #删除以节点b开始的子树, revert to the original look T.drop_child_tree (' B ') print t.depth Prin T T.enable_node.keys () print T.all_node.keys () #T. Show () #打印树
Results: 4
[' A ', ' A21 ', ' a112 ', ' A11 ', ' A12 ', ' a211 ', ' a212 ', ' A1 ', ' A2 ', ' a111 ']
[' a111 ', ' a112 ', ' A12 ', ' a212 ', ' a211 ', ' A2 ']
3
[' A1 ', ' a111 ', ' a112 ', ' A11 ', ' A12 ']
[' a111 ', ' a112 ', ' A12 ']
6
[' a111 ', ' a112 ', ' A12 ', ' a212 ', ' a211 ', ' B1 ', ' B2 ', ' A2 ']
[' A ', ' A21 ', ' a112 ', ' A11 ', ' A12 ', ' a211 ', ' a212 ', ' A1 ', ' A2 ', ' B1 ', ' B2 ', ' B ', ' a111 ']
4
[' a111 ', ' a112 ', ' A12 ', ' a212 ', ' a211 ', ' A2 ']
[' A ', ' A21 ', ' a112 ', ' A11 ', ' A12 ', ' a211 ', ' a212 ', ' A1 ', ' A2 ', ' a111 ']