树(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 else128 {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)------实现和遍历相关推荐
- 程序员面试题精选100题(06)-二元查找树的后序遍历结果[数据结构]
题目:输入一个整数数组,判断该数组是不是某二元查找树的后序遍历的结果.如果是返回true,否则返回false. 例如输入5.7.6.9.11.10.8,由于这一整数序列是如下树的后序遍历结果: 8 ...
- 6.二元查找树的后序遍历结果[PostOrderOfBST]
[题目] 输入一个整数数组,判断该数组是不是某二元查找树的后序遍历的结果.如果是返回true,否则返回false. 例如输入5.7.6.9.11.10.8,由于这一整数序列是如下树的后序遍历结果: 8 ...
- 树的先序遍历的栈实现
树的先序遍历的栈实现 先把根节点访问了,右子树入栈,去访问左子树. 1 void preorder(tree bt) //先序遍历bt所指的二叉树 2 { 3 tree stack[n]; //栈 4 ...
- 判断整数序列是不是二元查找树的后序遍历结果
题目:输入一个整数数组,判断该数组是不是某二元查找树的后序遍历的结果. 如果是返回true,否则返回false. 例如输入5.7.6.9.11.10.8,由于这一整数序列是如下树的后序遍历结果: 8 ...
- 二元查找树的后序遍历结果
题目:输入一个整数数组,判断该数组是不是某二元查找树的后序遍历的结果.如果是返回 true ,否则返回 false . 例如输入5.7.6.9.11.10.8,由于这一整数序列是如下树的后序遍历结果: ...
- 树的几种遍历方式(递归/非递归)
树的几种遍历方式,前序遍历,中序遍历,后序遍历,包括它的递归实现以及非递归实现 非递归遍历 -----------------前序遍历------------------------ class So ...
- 二叉查找(排序)树/二叉树----建树,遍历
二叉查找(排序)树/二叉树----建树,遍历 import java.util.ArrayList; import java.util.LinkedList; import java.util.Lis ...
- python 数据结构 树 dev get items_python数据结构之树(二叉树的遍历)
'''树的构造 1.递归实现先序遍历.中序遍历.后序遍历 2.堆栈实现先序遍历.中序遍历.后序遍历 3.队列实现层次遍历''' #节点类 classNode(object):__slots__ = ' ...
- 怎样将树的中序遍历的数输入到一个数组中_数据结构与算法-二叉查找树平衡(DSW)...
上一节探讨了二叉查找树的基本操作,二叉查找树的查找效率在理想状态下是O(lgn),使用该树进行查找总是比链表快得多.但是,该论点并不总是正确,因为查找效率和二叉树的形状息息相关.就像这样: 图1-1给 ...
- 树和森林与二叉树的转换、树和森林的遍历
全部数据结构.算法及应用课内模板请点击:https://blog.csdn.net/weixin_44077863/article/details/101691360 树和森林转二叉树其实十分简单,我 ...
最新文章
- 德约科维奇横扫纳达尔夺冠 加冕澳网七冠王
- wxWidgets:wxString概述
- springboot干什么的_Spring Boot 项目的这些文件都是干啥用的?
- 可以伸缩的查询面板 (searchBar)
- 命令行模式下编译多个C/C++源文件
- laravel yii thinkphp 框架对比_thinkPHP--项目
- 2011-2-14 | Android Handler
- linux自学笔记--DNS服务器
- 拼写检查器——朴素贝叶斯应用
- 知识答题小程序功能总结
- 报表生成器FastReport .Net如何存储和加载报告
- 《UnityAPI.Cloth布料》(Yanlz+Unity+SteamVR+云技术+5G+AI+VR云游戏+Cloth+friction+useGravity+normals+立钻哥哥++OK++)
- (自适应动态规划综述)
- 机器学习中的主动学习实现_我如何使用机器学习来帮助实现正念
- 关于KingbaseES启动数据库服务时,license授权文件路径的相关问题
- 2015年第六届C/C++ B组蓝桥杯省赛真题
- 最大m子段和总结与例题 51nod1052 HDU1024
- c++ const 转非const
- web界面测试用例(shelley_shu)
- 中文版-Because He Lives-因他活着-好消息诗班(音乐河2)
热门文章
- C++ 11 新特性: constexpr变量和constexpr函数
- scikit-learning_特征分析(数据挖掘入门与实践-实验7)
- Hi3516DV300 U-boot移植应用开发指南(1)
- Oracle修改表列名与顺序的解决方案 (sql 修改列名)
- android studio 入门比较好的书籍
- 转:深度学习与自然语言处理之五:从RNN到LSTM
- 老李推荐: 第8章4节《MonkeyRunner源码剖析》MonkeyRunner启动运行过程-启动AndroidDebugBridge 2...
- SQL Server blocking session
- 通过NetMassDownloader批量下载和使用微软.NET框架源代码
- Linux命令学习(三):文件操作命令(1)