数据结构与算法-- 第五章 树
来源:互联网 发布:linux stage2存放在哪 编辑:程序博客网 时间:2024/06/01 08:28
1.树
是有限个结点的集合。
2.有关树的概念
结点、度、叶子结点、父结点、孩子结点、兄弟结点、树的深度等。
此节主要介绍 树中的常用树 ----------- 二叉树
3. 二叉树
1)每个结点最多有两棵子树。
2)左子树与右子树是有顺序的,不能颠倒。
3)即使树中某结点只有一棵子树,也要区分是左子树还是右子树
3.1满二叉树 与 完全二叉树 区别
满二叉树:在一棵树中,如果所有分支结点都存在左子树与右子树,并且叶子结点都在同一层上。
完全二叉树:对一棵二叉树按层次编号,如果编号为 i 的结点与同样深度的满二叉树中编号为 i 的结点在二叉树中的位置完全相同。
满二叉树一定是完全二叉树,但是完全二叉树不一定是满二叉树。
3.2 二叉树的存储结构 ( 顺序存储 / 链式存储)
1) 顺序存储
使用一维数组存储二叉树中的结点,结点存储位置(按层次编号后的数字)是数组的下标。 (一般只用于完全二叉树)
2)二叉链表 (链式存储)
二叉树每个结点最多有两个孩子,所以需要一个数据域存储结点数据,两个指针域存储左右子树。
3.3 二叉树的遍历 ( 前序遍历 / 中序遍历 / 后序遍历)--(二叉树存储结构为链式存储为例)
1)前序遍历 根结点 -- 左子树 -- 右子树
2)中序遍历 左子树 --根结点 -- 右子树
3)后序遍历 左子树 -- 右子树 -- 根结点
代码转载地址:【数据结构】之二叉树的java实现
package itcast.tree;import java.util.Stack;/** * 二叉树的链式存储 * @author WWX */ public class BinaryTree { private TreeNode root=null; public BinaryTree(){ root=new TreeNode(1,"rootNode(A)"); } /** * 创建一棵二叉树 * A * B C * D E F * @param root * @author WWX */ public void createBinTree(TreeNode root){ TreeNode newNodeB = new TreeNode(2,"B"); TreeNode newNodeC = new TreeNode(3,"C"); TreeNode newNodeD = new TreeNode(4,"D"); TreeNode newNodeE = new TreeNode(5,"E"); TreeNode newNodeF = new TreeNode(6,"F"); root.leftChild=newNodeB; root.rightChild=newNodeC; root.leftChild.leftChild=newNodeD; root.leftChild.rightChild=newNodeE; root.rightChild.rightChild=newNodeF; } public boolean isEmpty(){ return root==null; } //树的高度 public int height(){ return height(root); } //节点个数 public int size(){ return size(root); } private int height(TreeNode subTree){ if(subTree==null) return 0;//递归结束:空树高度为0 else{ int i=height(subTree.leftChild); int j=height(subTree.rightChild); return (i<j)?(j+1):(i+1); } } private int size(TreeNode subTree){ //全部使用递归实现 if(subTree==null){ return 0; }else{ return 1+size(subTree.leftChild) +size(subTree.rightChild); } } //返回双亲结点 public TreeNode parent(TreeNode element){ return (root==null|| root==element)?null:parent(root, element); } public TreeNode parent(TreeNode subTree,TreeNode element){ if(subTree==null) return null; if(subTree.leftChild==element||subTree.rightChild==element) //返回父结点地址 return subTree; TreeNode p; //现在左子树中找,如果左子树中没有找到,才到右子树去找 if((p=parent(subTree.leftChild, element))!=null) //递归在左子树中搜索 return p; else //递归在右子树中搜索 return parent(subTree.rightChild, element); } public TreeNode getLeftChildNode(TreeNode element){ return (element!=null)?element.leftChild:null; } public TreeNode getRightChildNode(TreeNode element){ return (element!=null)?element.rightChild:null; } public TreeNode getRoot(){ return root; } //在释放某个结点时,该结点的左右子树都已经释放, //所以应该采用后续遍历,当访问某个结点时将该结点的存储空间释放 public void destroy(TreeNode subTree){ //删除根为subTree的子树 if(subTree!=null){ //删除左子树 destroy(subTree.leftChild); //删除右子树 destroy(subTree.rightChild); //删除根结点 subTree=null; } } public void traverse(TreeNode subTree){ System.out.println("key:"+subTree.key+"--name:"+subTree.data);; traverse(subTree.leftChild); traverse(subTree.rightChild); } //前序遍历 public void preOrder(TreeNode subTree){ if(subTree!=null){ visted(subTree); preOrder(subTree.leftChild); preOrder(subTree.rightChild); } } //中序遍历 public void inOrder(TreeNode subTree){ if(subTree!=null){ inOrder(subTree.leftChild); visted(subTree); inOrder(subTree.rightChild); } } //后续遍历 public void postOrder(TreeNode subTree) { if (subTree != null) { postOrder(subTree.leftChild); postOrder(subTree.rightChild); visted(subTree); } } //前序遍历的非递归实现 public void nonRecPreOrder(TreeNode p){ Stack<TreeNode> stack=new Stack<TreeNode>(); TreeNode node=p; while(node!=null||stack.size()>0){ while(node!=null){ visted(node); stack.push(node); node=node.leftChild; } while(stack.size()>0){ node=stack.pop(); node=node.rightChild; while(node!=null){ visted(node); stack.push(node); node=node.leftChild; } } } } //中序遍历的非递归实现 public void nonRecInOrder(TreeNode p){ Stack<TreeNode> stack =new Stack<BinaryTree.TreeNode>(); TreeNode node =p; while(node!=null||stack.size()>0){ //存在左子树 while(node!=null){ stack.push(node); node=node.leftChild; } //栈非空 if(stack.size()>0){ node=stack.pop(); visted(node); node=node.rightChild; } } } //后序遍历的非递归实现 public void noRecPostOrder(TreeNode p){ Stack<TreeNode> stack=new Stack<BinaryTree.TreeNode>(); TreeNode node =p; while(p!=null){ //左子树入栈 for(;p.leftChild!=null;p=p.leftChild){ stack.push(p); } //当前结点无右子树或右子树已经输出 while(p!=null&&(p.rightChild==null||p.rightChild==node)){ visted(p); //纪录上一个已输出结点 node =p; if(stack.empty()) return; p=stack.pop(); } //处理右子树 stack.push(p); p=p.rightChild; } } public void visted(TreeNode subTree){ subTree.isVisted=true; System.out.println("key:"+subTree.key+"--name:"+subTree.data);; } /** * 二叉树的节点数据结构 * @author WWX */ private class TreeNode{ private int key=0; private String data=null; private boolean isVisted=false; private TreeNode leftChild=null; private TreeNode rightChild=null; public TreeNode(){} /** * @param key 层序编码 * @param data 数据域 */ public TreeNode(int key,String data){ this.key=key; this.data=data; this.leftChild=null; this.rightChild=null; } } //测试 public static void main(String[] args) { BinaryTree bt = new BinaryTree(); bt.createBinTree(bt.root); System.out.println("the size of the tree is " + bt.size()); System.out.println("the height of the tree is " + bt.height()); System.out.println("*******(前序遍历)[ABDECF]遍历*****************"); bt.preOrder(bt.root); System.out.println("*******(中序遍历)[DBEACF]遍历*****************"); bt.inOrder(bt.root); System.out.println("*******(后序遍历)[DEBFCA]遍历*****************"); bt.postOrder(bt.root); System.out.println("***非递归实现****(前序遍历)[ABDECF]遍历*****************"); bt.nonRecPreOrder(bt.root); System.out.println("***非递归实现****(中序遍历)[DBEACF]遍历*****************"); bt.nonRecInOrder(bt.root); System.out.println("***非递归实现****(后序遍历)[DEBFCA]遍历*****************"); bt.noRecPostOrder(bt.root); } }
0 0
- 数据结构与算法-- 第五章 树
- 第五章 二叉树 练习题(数据结构与算法MOOC)
- Python宝典第五章:数据结构与算法
- 数据结构与算法 第五课
- 数据结构与算法分析课后习题第五章
- 算法与数据结构第五章树上机练习
- 数据结构与算法分析学习笔记--第五章--排序
- 数据结构与算法JavaScript描述[第五章](队列)
- [翻译]C#数据结构与算法 – 第五章栈与队列(Part 1)
- [翻译]C#数据结构与算法 – 第五章栈与队列(Part 2)
- 《数据结构》 第五章 树与二叉树之 笔记
- 数据结构与算法C#语言描述第五章栈和队列
- 数据结构与算法C#语言描述第五章栈和队列
- 数据结构与算法分析学习笔记--第五章--散列---分离链接法
- (8)链表 《java数据结构与算法》一书第五章读书笔记。
- 微软等数据结构与算法面试100题 第五题
- 算法与数据结构 第五次作业 身陷敌阵
- [数据结构]第五章-排序与选择
- <mvc:annotation-driven/>与静态资源配置引发的问题
- java Error:"The method xxx is undefind for the type of xxx"
- Part01
- Python中原始字符串操作符 r R
- hdoj-1008-Elevator(解题报告)
- 数据结构与算法-- 第五章 树
- Part02
- Ubuntu 上不了网
- 选择排序-JAVA
- 居中方法总结
- Thinkphp MVC以及4种url放文方式
- 设计模式之单例模式
- java汉诺塔问题
- 第七届蓝桥杯JAVA语言B组_分小组