❶ 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