二叉树(二叉链表实现)JAVA代码

publicclassTest{publicstaticvoid main(String[] args){char[] ch =newchar[]{'A','B','D','#','#','G','#','#','C','J','#','#','M','#','#'};BinaryTree binaryTree =newBinaryTree(ch);binaryTree.preOrder();System.out.println();binaryTree.inOrder();System.out.println();binaryTree.postOrder();System.out.println();binaryTree.levelOrder();System.out.println();}}
publicclassBinaryTree{//二叉树结点publicclassBiNode{char data;BiNode left;BiNode right;BiNode(char data,BiNode left,BiNode right){this.data = data;this.left = left;this.right = right;}int flag =0;//供非递归后序遍历使用}//根结点publicBiNode root;privatestaticint i;publicBinaryTree(char[] pre){i =0;root = create(pre);}//初始化(先序遍历顺序存放数组、'#'表示null)privateBiNode create(char[] pre){if(i < pre.length){if(pre[i]=='#') //结点为空{i++;return null;}BiNode p =newBiNode(pre[i], null, null); //结点非空i++;p.left = create(pre); //递归建立左子树p.right = create(pre); //递归建立右子树return p;}return null;}//先序遍历publicvoid preOrder(){System.out.print("preOrder traversal with recursion:");preOrder(root);System.out.println();System.out.print("preOrder traversal without recursion:");preOrder2(root);System.out.println();}//递归privatevoid preOrder(BiNode root){if(root == null)return;System.out.print(root.data); //访问结点preOrder(root.left);preOrder(root.right);}//非递归privatevoid preOrder2(BiNode head){LinkedList<BiNode> s =newLinkedList<BiNode>();while(head != null ||!s.isEmpty()){while(head != null) //访问左子树{System.out.print(head.data); //访问左子树s.push(head); //结点入栈(待后面找其右子树使用)= =(“递归”)head = head.left;}if(!s.isEmpty()) //转向右子树{head = s.peek().right; //转向右子树s.pop(); //结点出栈(已经找到其右子树)= =(“递归结束”)}}}//中序遍历publicvoid inOrder(){System.out.print("inOrder traversal with recursion:");inOrder(root);System.out.println();System.out.print("inOrder traversal without recursion:");inOrder2(root);System.out.println();}//递归privatevoid inOrder(BiNode root){if(root == null)return;inOrder(root.left);System.out.print(root.data); //访问结点inOrder(root.right);}//非递归privatevoid inOrder2(BiNode head){LinkedList<BiNode> s =newLinkedList<BiNode>();while(head != null ||!s.isEmpty()){while(head != null) //左子树入栈{s.push(head); //结点入栈(待后面找其右子树使用)= =(“递归”)head = head.left;}System.out.print(s.peek().data); //访问左子树if(!s.isEmpty()) //转向右子树{head = s.peek().right; //转向右子树s.pop(); //结点出栈(已经找到其右子树)= =(“递归结束”)}}}//后序遍历publicvoid postOrder(){System.out.print("postOrder traversal with recursion:");postOrder(root);System.out.println();System.out.print("postOrder traversal without recursion:");postOrder2(root);System.out.println();}//递归privatevoid postOrder(BiNode root){if(root == null)return;postOrder(root.left);postOrder(root.right);System.out.print(root.data); //访问结点}//非递归//后序遍历特点:递归左右子树后,还需访问结点://1、左子树入栈//2、“两次出栈”(用flag标记模仿):第一次是为了找到左子树相应的右子树结点;第二次是为了访问结点privatevoid postOrder2(BiNode head){LinkedList<BiNode> s =newLinkedList<BiNode>();while(head != null ||!s.isEmpty()){while(head != null) //左子树入栈{head.flag =1;s.push(head); //结点连同flag入栈(待后面找其右子树使用)= =(“递归”)head = head.left;}while(!s.isEmpty()&& s.peek().flag ==2) //若flag为2(已经找到其右子树出过一次栈),访问结点{System.out.print(s.peek().data); //访问结点元素s.pop(); //(第二次“结点出栈”)实际结点出栈(已经访问结点元素)= =(“递归结束”)}if(!s.isEmpty()) //flag为1,转向右子树{head = s.peek().right; //转向右子树s.peek().flag =2; //(第一次“flag模拟出栈”)标记为2,但实际结点不出栈(已经找到其右子树)}}}//层序遍历publicvoid levelOrder(){levelOrder(root);}privatevoid levelOrder(BiNode root){LinkedList<BiNode>queue=newLinkedList<BiNode>(); //LinkedList实现了Queue接口BiNode p = root;while(p != null){System.out.print(p.data); //访问结点if(p.left != null)queue.add(p.left);if(p.right != null)queue.add(p.right);p =queue.poll(); //队头出队并返回为p}}//在p结点后插入datapublicvoid insert(BiNode p,char data, boolean left){if(p != null){if(left) //插入位置为左孩子p.left =newBiNode(data,p.left,null);else //插入位置为右孩子p.right =newBiNode(data,p.right,null);}}//删除p的一个子树publicvoiddelete(BiNode p, boolean left){if(p != null){if(left) //删除目标为左子树p.left = null;else //删除目标为右子树p.right = null;}}}
二叉树(二叉链表实现)JAVA代码的更多相关文章
- C#实现二叉树--二叉链表结构
二叉树的简单介绍 关于二叉树的介绍请看这里 : 二叉树的简单介绍 http://www.cnblogs.com/JiYF/p/7048785.html 二叉链表存储结构: 二叉树的链式存储结构是指,用 ...
- C语言递归实现二叉树(二叉链表)的三种遍历和销毁操作(实验)
今天写的是二叉树操作的实验,这个实验有三个部分: ①建立二叉树,采用二叉链表结构 ②先序.中序.后续遍历二叉树,输出节点值 ③销毁二叉树 二叉树的节点结构定义 typedef struct BiTNo ...
- 二叉树的二叉链表存储结构及C++实现
前言:存储二叉树的关键是如何表示结点之间的逻辑关系,也就是双亲和孩子之间的关系.在具体应用中,可能要求从任一结点能直接访问到它的孩子. 一.二叉链表 二叉树一般多采用二叉链表(binary linke ...
- c使用二叉链表创建二叉树遇到的一些疑问和思考
二叉链表存储二叉树 学习的时候参考的是<大话数据结构>,书中是这样定义的 typedef char TElemType; typedef struct BiTNode { TElemTyp ...
- 树(二叉树 & 二叉搜索树 & 哈夫曼树 & 字典树)
树:n(n>=0)个节点的有限集.有且只有一个root,子树的个数没有限制但互不相交.结点拥有的子树个数就是该结点的度(Degree).度为0的是叶结点,除根结点和叶结点,其他的是内部结点.结点 ...
- 编程算法 - 二叉搜索树 与 双向链表 代码(C++)
二叉搜索树 与 双向链表 代码(C++) 本文地址: http://blog.csdn.net/caroline_wendy 题目:输入一颗二叉搜索树, 将该二叉搜索树转换成一个排序的双向链表. 要求 ...
- 【算法与数据结构】二叉搜索树的Java实现
为了更加深入了解二叉搜索树,博主自己用Java写了个二叉搜索树,有兴趣的同学可以一起探讨探讨. 首先,二叉搜索树是啥?它有什么用呢? 二叉搜索树, 也称二叉排序树,它的每个节点的数据结构为1个父节点指 ...
- 二叉搜索树 思想 JAVA实现
二叉搜索树:一棵二叉搜索树是以一棵二叉树来组织的,这样一棵树可以使用链表的数据结构来表示(也可以采用数组来实现).除了key和可能带有的其他数据外,每个节点还包含Left,Right,Parent,它 ...
- 二叉搜索树及java实现
二叉搜索树(Binary Search Tree) 二叉搜索树是二叉树的一种,是应用非常广泛的一种二叉树,英文简称为 BST 又被称为:二叉查找树.二叉排序树 任意一个节点的值都大于其左子树所有节 ...
- 二叉搜索树的java实现
转载请注明出处 一.概念 二叉搜索树也成二叉排序树,它有这么一个特点,某个节点,若其有两个子节点,则一定满足,左子节点值一定小于该节点值,右子节点值一定大于该节点值,对于非基本类型的比较,可以实现Co ...
随机推荐
- hdu 1281棋盘游戏(二分匹配)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1281 Problem Description 小希和Gardon在玩一个游戏:对一个N*M的棋盘, ...
- Android单位度量
px(像素):屏幕上的点. in(英寸):长度单位.mm(毫米):长度单位.pt(磅):1/72英寸.dp(与密度无关的像素):一种基于屏幕密度的抽象单位.在每英寸160点的显示器上,1dp = 1p ...
- javascript获得浏览器工作区域的大小
浏览器的窗口发生变化时会执行window.onresize方法,通过这个方法我们可以获得到浏览器工作区域的大小: window.onresize=function(){ bodyHeight = wi ...
- Python新手学习基础之函数-可变参数**
可变参数( ** ) 讲好了一颗*,那如果函数的最后一个参数带有 ** 前缀: 所有正常参数之外的其他的关键字参数都将被放置在一个字典中传递给函数. 要好好理解* 和 ** 两种可变参数哦~ 看个** ...
- UVA 572 Oil Deposits油田(DFS求连通块)
UVA 572 DFS(floodfill) 用DFS求连通块 Time Limit:1000MS Memory Limit:65536KB 64bit IO Format: ...
- C++中的dll
创建动态链接库 (DLL) 项目 在菜单栏上,依次选择“文件”.“新建”.“项目”. 在“新建项目”对话框的左窗格中,依次展开“已安装”.“模板”.“Visual C++”,然后选择“Win32”. ...
- python urllib2
http://my.oschina.net/duhaizhang/blog/69883
- CSS中.和#区别
一.问题来源 制作导航栏,参考别人的代码,发现的. 二.解析 2.1 概述 id:用来定义页面中大的样式,如栏目划分,顶部,正文,底部等:用#top的形式来定义: class:用来定义一些比较细节的样 ...
- Red Hat TimesTen安装记录
1:内核参数修改 # vi /etc/sysctl.conf kernel.sem= #sysctl –p 备注:此安装过程为测试环境,具体参数修改要参考TimesTen官方文档. 2:创建用户及组信 ...
- 教你如何用Qt做透明的窗体,setMask, Opacity
// In this function, we can get the height and width of the current widgetvoid Widget::resizeEvent(Q ...