❶ python怎么在二叉树中
#coding:utf-8#author:Elvis class TreeNode(object): def __init__(self): self.data = '#' self.l_child = None self.r_child = None class Tree(TreeNode): #create a tree def create_tree(self, tree): data = raw_input('->') if data == '#': tree = None else: tree.data = data tree.l_child = TreeNode() self.create_tree(tree.l_child) tree.r_child = TreeNode() self.create_tree(tree.r_child) #visit a tree node def visit(self, tree): #输入#号代表空树 if tree.data is not '#': print str(tree.data) + '\t', #先序遍历 def pre_order(self, tree): if tree is not None: self.visit(tree) self.pre_order(tree.l_child) self.pre_order(tree.r_child) #中序遍历 def in_order(self, tree): if tree is not None: self.in_order(tree.l_child) self.visit(tree) self.in_order(tree.r_child) #后序遍历 def post_order(self, tree): if tree is not None: self.post_order(tree.l_child) self.post_order(tree.r_child) self.visit(tree) t = TreeNode()tree = Tree()tree.create_tree(t)tree.pre_order(t)print '\n'tree.in_order(t)print '\n'tree.post_order(t)
❷ python 二叉树是怎么实现的
#coding:utf-8
#author:Elvis
classTreeNode(object):
def__init__(self):
self.data='#'
self.l_child=None
self.r_child=None
classTree(TreeNode):
#createatree
defcreate_tree(self,tree):
data=raw_input('->')
ifdata=='#':
tree=None
else:
tree.data=data
tree.l_child=TreeNode()
self.create_tree(tree.l_child)
tree.r_child=TreeNode()
self.create_tree(tree.r_child)
#visitatreenode
defvisit(self,tree):
#输入#号代表空树
iftree.dataisnot'#':
printstr(tree.data)+' ',
#先序遍历
defpre_order(self,tree):
iftreeisnotNone:
self.visit(tree)
self.pre_order(tree.l_child)
self.pre_order(tree.r_child)
#中序遍历
defin_order(self,tree):
iftreeisnotNone:
self.in_order(tree.l_child)
self.visit(tree)
self.in_order(tree.r_child)
#后序遍历
defpost_order(self,tree):
iftreeisnotNone:
self.post_order(tree.l_child)
self.post_order(tree.r_child)
self.visit(tree)
t=TreeNode()
tree=Tree()
tree.create_tree(t)
tree.pre_order(t)
print' '
tree.in_order(t)
print' '
tree.post_order(t)
❸ 如何将数据存储为二叉树python
(1)二叉树是有序树,即使只有一个子树,也必须区分左、右子树;
(2)二叉树的每个结点的度不能大于2,只能取0、1、2三者之一;
(3)二叉树中所有结点的形态有5种:空结点、无左右子树的结点、只有左子树的结点、只有右子树的结点和具有左右子树的结点。
❹ 如何用python构造一个n层的完全二叉树
用python构造一个n层的完全二叉树的代码如下:
typedefstruct{
intweight;
intparent,lchild,rchild;
}HTNode,*HuffmanTree;//动态分配数组存储huffman树
算法设计
voidcreateHuffmantree(){
ht=(HuffmanTree)malloc(m+1)*sizeof(HTNode);//动态分配数组存储huffman树,0号单元未用
//m:huffman树中的结点数(m=2*n-1)
for(i=1;i<=m;++i)
ht[i].parent=ht[i]->lch=ht[i]->rch=0;
for(i=1;i<=n;++i)
ht[i].weight=w[i];//初始化,w[i]:n个叶子的权值
for(i=n+1;i<=m,++i){//建哈夫曼树
select(i-1),s1,s2);//在ht[k](1<=k<=i-1)中选择两个双亲域为零而权值取最小的结点:s1和s2
ht[s1].parent=ht[s2].parent=i;
ht[i].lch=s1;
ht[i].rch=s2;
ht[i].weight=ht[s1].weight+ht[s2].weight;
};
}
❺ 二叉树统计叶子节点 python
leave定义函数的缩进不对吧
defleave(self,root):
❻ python怎么获得二叉树根到所有叶子的路径
一棵树当中没有子结点(即度为0)的结点,称为叶子结点因为只有一个所以1 2 3 4是一直这样连下去 的
❼ python字典怎么表现二叉树
用python构造一个n层的完全二叉树的代码如下: typedef struct {int weight;int parent, lchild, rchild; } HTNode ,*HuffmanTree; // 动态分配数组存储huffman树 算法设计void createHuffmantree(){ ht=(HuffmanTree)malloc(m+1)*sizeof(HTNode.
❽ Python怎么实现二叉树排序
常用的排序算法(主要指面试中)包含两大类,一类是基础比较模型的,也就是排序的过程,是建立在两个数进行对比得出大小的基础上,这样的排序算法又可以分为两类:一类是基于数组的,一类是基于树的;基础数组的比较排序算法主要有:冒泡法,插入法,选择法,归并法,快速排序法;基础树的比较排序算法主要有:堆排序和二叉树排序;基于非比较模型的排序,主要有桶排序和位图排序(个人认为这两个属于同一思路的两个极端)。
❾ python二叉树问题
def __init__(self ,value=3): # value = default_value
self.value = value
这样就行了撒。
PS:以后贴代码记得把缩进对齐。。。
❿ python二叉树算法
定义一颗二叉树,请看官自行想象其形状
class BinNode( ):
def __init__( self, val ):
self.lchild = None
self.rchild = None
self.value = val
binNode1 = BinNode( 1 )
binNode2 = BinNode( 2 )
binNode3 = BinNode( 3 )
binNode4 = BinNode( 4 )
binNode5 = BinNode( 5 )
binNode6 = BinNode( 6 )
binNode1.lchild = binNode2
binNode1.rchild = binNode3
binNode2.lchild = binNode4
binNode2.rchild = binNode5
binNode3.lchild = binNode6