树(1)------实现和遍历
生活随笔
收集整理的這篇文章主要介紹了
树(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)------实现和遍历的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: kafka 消息服务
- 下一篇: 中国的EMM市场迎来爆发期?