日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

树(1)------实现和遍历

發布時間:2024/4/17 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 树(1)------实现和遍历 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1、樹的性質:

(1)樹是分層的,分層的意思是樹的頂層部分更加寬泛一般底層部分更加精細具體。

(2)一個節點(node)的所有子節點(children)和另一個節點的子節點是完全獨立的。

(3)它的每個葉子節點(leaf)都是不同的。

2、相關概念:

節點、邊、根節點、路徑、父節點、兄弟節點、……

3、樹的實現

(1)嵌套列表

def BinaryTree(r):return [r,[],[]] def insertLeft(root,newBranch):t=root.pop(1)if len(t)>1:root.insert(1,[newBranch,t,[]])else:root.insert(1,[newBranch,[],[]])return root def insertRight(root,newBranch):t=root.pop(2)if len(t)>1:root.insert(2,[newBranch,[],t])else:root.insert(2,[newBranch,[],[]])return root def getRootVal(root):return root[0] def setRootVal(root,newVal):root[0]=newVal def getLeftChild(root):return root[1] def getRightChild(root):return root[2]r=BinaryTree('a') insertLeft(r,'b') insertRight(r,'c') insertRight(r[1],'d') insertLeft(r[2],'e') insertRight(r[2],'f') print(r) I=getLeftChild(r) print(I)

(2)節點和引用

class BinaryTree:def __init__(self,rootObi):self.key=rootObiself.leftChild=Noneself.rightChild=Nonedef insertLeft(self,newNode):if self.leftChild==None:self.leftChild=BinaryTree(newNode)else:t=BinaryTree(newNode)t.leftChild=self.leftChildself.leftChild=tdef insertRight(self,newNode):if self.rightChild==None:self.rightChild=BinaryTree(newNode)else:t=BinaryTree(newNode)t.rightChild=self.rightChildself.rightChild=t def getRightChild(self):return self.rightChilddef getLeftChild(self):return self.leftChilddef setRootVal(self,obj):self.key=objdef getRootVal(self):return self.keyr=BinaryTree('a') print(r.getRootVal()) print(r.getLeftChild()) r.insertLeft('b') r.insertRight('c') r.insertLeft('d') r.insertRight('e') print(r.getRootVal()) print(r.getLeftChild().getLeftChild().getRootVal()) print(r.getRightChild().getRightChild().getRootVal())

4、樹的遍歷

前序遍歷:中,左,右

中序遍歷:左,中,右

后序遍歷:左,右,中

前序遍歷:

def preorder(tree):if tree:print(tree.getRootVal())preorder(tree.getLeftChild())preorder(tree.getRightChild())#放入上面類中的代碼 def preorder(self):print(self.key)if self.leftChild:self.leftChild.preorder()if self.RightChild:self.RightChild.preorder()
#非遞歸
def pre1(root):
??????? if not root:
??????????? return []
??????? res = []
??????? stack = [root]
??????? while stack:
??????????? node = stack.pop()
??????????? res.append(node.val)
??????????? if node.right:
??????????????? stack.append(node.right)
??????????? if node.left:
??????????????? stack.append(node.left)
??????? return res

后序遍歷:

def postorder(tree):if tree:postorder(tree.getLeftChild())postorder(tree.getRightChild())print(tree.getRootVal())#非遞歸 113 void posOrderUnRecur2(Node *head)
114 {
115???? if(NULL != head)
116 ??? {
117???????? stack<Node*> s;
118 ??????? s.push(head);
119???????? Node *cur = NULL;
120???????? while(!s.empty())
121 ??????? {
122???????????? cur = s.top();
123???????????? if(NULL != cur->left && head != cur->left && head != cur->right)
124???????????????? s.push(cur->left);
125???????????? else if(NULL != cur->right && head != cur->right)
126???????????????? s.push(cur->right);
127???????????? else
128 ??????????? {
129???????????????? cout << s.top()->value << " ";
130 ??????????????? s.pop();
131???????????????? head = cur;
132 ??????????? }
133 ??????? }
134 ??? }
135???? cout << endl;
136???? return ;???
137
138 }

中序遍歷:

def inorder(tree): if tree:inorder(tree.getLeftChild())print(tree.getRootVal())inorder(tree.getRightChild()) # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = x # self.left = None # self.right = Noneclass Solution(object):def inorderTraversal(self, root):""":type root: TreeNode:rtype: List[int]"""'''######遞歸res=[]left=[]right=[]if root:left=self.inorderTraversal(root.left)res.append(root.val)right=self.inorderTraversal(root.right)return left+res+right'''###非遞歸if not root:return []stack = []node = rootres = []while stack or node:if node:stack.append(node)node = node.leftelse:node = stack.pop()res.append(node.val)node = node.rightreturn res

?

?

?

5、堆(最小堆、最大堆)

python模塊:from pythonds.trees.binheap import BinHeap

?

轉載于:https://www.cnblogs.com/Lee-yl/p/9048081.html

總結

以上是生活随笔為你收集整理的树(1)------实现和遍历的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。