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()