python實現樹結構
A. 如何用python將幾個列表轉換成樹形結構
不要費那個勁了,直接用pathlib
B. 想知道如何用python語言實現一棵樹結構代碼
class node:
left=None
right=None
def __init__(self, parent=None):
self.parent=parent
賦值的時候對應就好了。如root=node(),a=node(root),root.left=a,就有點像C語言里的回指針了答。
C. 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)
D. 用python畫一棵樹
1、准備
1
打開我們的Python shell界面,也就是大家所說的idle界面。
E. python 怎樣實現多叉樹
class node:
def __init__(self, data):
self._data = data
self._children = []
def getdata(self):
return self._data
def getchildren(self):
return self._children
def add(self, node):
##if full
if len(self._children) == 4:
return False
else:
self._children.append(node)
def go(self, data):
for child in self._children:
if child.getdata() == data:
return child
return None
class tree:
def __init__(self):
self._head = node('header')
def linktohead(self, node):
self._head.add(node)
def insert(self, path, data):
cur = self._head
for step in path:
if cur.go(step) == None:
return False
else:
cur = cur.go(step)
cur.add(node(data))
return True
def search(self, path):
cur = self._head
for step in path:
if cur.go(step) == None:
return None
else:
cur = cur.go(step)
return cur
F. Python 如何實現如下樹功能: 樣例數據:tmp=[[0,'A'],[1,'B'],[2,'B1'],[2,'B2'],[3,'
#!/usr/bin/envpython
#coding=utf-8
#python2.7
tmp=[[0,'A'],[1,'B'],[2,'B1'],[2,'B2'],[3,'B21'],[3,'B22'],[3,'B23'],[1,'C'],[1,'D']]
tmp_=tmp[1:]
printtmp[0][1]
tmp__=tmp[1][1]
foridx,iinenumerate(tmp_):
print'-'*4,i[1]
ifidx+1<len(tmp_)andtmp_[idx+1][1].find(tmp__)<0:
print'----/%s'%tmp__
tmp__=tmp_[idx+1][1]
print'----/%s'%tmp__[-1]
print'/%s'%tmp[0][1]
-----
A
----B
----B1
----B2
----B21
----B22
----B23
----/B
----C
----/C
----D
----/D
/A
G. python如何表述樹形結構
這是數據結構的問題,按照數據結構中樹的實現即可,當然,要是圖方便也可以使用dict來模擬
H. 求帶有樹結構的python的演算法,常見的樹結構
是不是指python里的lambda編程啊,這是面向函數的設計方法,也是樹結構
I. 如何實現Python多叉樹
classnode:
def__init__(self,data):
self._data=data
self._children=[]
defgetdata(self):
returnself._data
defgetchildren(self):
returnself._children
defadd(self,node):
##iffull
iflen(self._children)==4:
returnFalse
else:
self._children.append(node)
defgo(self,data):
forchildinself._children:
ifchild.getdata()==data:
returnchild
returnNone
classtree:
def__init__(self):
self._head=node('header')
deflinktohead(self,node):
self._head.add(node)
definsert(self,path,data):
cur=self._head
forstepinpath:
ifcur.go(step)==None:
returnFalse
else:
cur=cur.go(step)
cur.add(node(data))
returnTrue
defsearch(self,path):
cur=self._head
forstepinpath:
ifcur.go(step)==None:
returnNone
else:
cur=cur.go(step)
returncur
'''
definenode
'''
a=node('A')
b=node('B')
c=node('C')
d=node('D')
e=node('E')
f=node('F')
g=node('G')
h=node('H')
i=node('I')
j=node('J')
k=node('K')
l=node('L')
m=node('M')
n=node('N')
o=node('O')
'''
addingnodetobuildtrue
'''
a.add(b)
a.add(g)
a.add(h)
b.add(c)
b.add(e)
g.add(i)
g.add(j)
g.add(k)
g.add(l)
h.add(m)
h.add(n)
h.add(o)
c.add(d)
c.add(f)
i.add(node(29))
j.add(node(28))
k.add(node(27))
l.add(node(26))
m.add(node(25))
n.add(node(24))
o.add(node(23))
f.add(node(30))
tree=tree()
tree.linktohead(a)
#testcase
print'Node',tree.search("ABE").getdata()
print'Node',tree.search("ABC").getdata()
print'Node',tree.search("AHM").getdata()
tree.insert("ABCD",1)
foriind.getchildren():
print'valueafter',d.getdata(),'is',i.getdata()