二叉树的前序、中序、后序遍历
来源:互联网 发布:微信淘宝客封号 编辑:程序博客网 时间:2024/05/20 08:26
递归方式:
- /**
- * 实现二叉树的创建、前序遍历、中序遍历和后序遍历
- **/
- package DataStructure;
- /**
- * Copyright 2014 by Ruiqin Sun
- * All right reserved
- * created on 2014-9-9 下午2:34:15
- **/
- public class BinTreeInt {
- private Node root;
- /**
- * 创建内部节点类
- **/
- private class Node{
- // 左节点
- private Node leftChild;
- // 右节点
- private Node rightChild;
- // 节点对应的值
- private int data;
- public Node(int data){
- this.leftChild = null;
- this.rightChild = null;
- this.data = data;
- }
- }// class Node
- public BinTreeInt(){
- root = null;
- }
- /*
- *递归的创建二叉树
- * */
- public void buildTree(Node node ,int data){
- if (root == null){// 如果根节点为空,创建根节点
- root = new Node(data);
- }else{
- if(data <node.data){//插入到左子树
- if(node.leftChild == null){//左节点为空,直接创建值为data的左节点
- node.leftChild = new Node(data);
- }else{//左节点不为空,调用buildTree函数插到左子树中
- buildTree(node.leftChild,data);
- }
- }else{
- if(node.rightChild == null){
- node.rightChild = new Node(data);
- }else{
- buildTree(node.rightChild,data);
- }
- }
- }
- }//end buildTree
- /*
- *前序遍历二叉树
- * */
- public void preOrder(Node node){
- if(node != null){
- System.out.print(node.data);
- preOrder(node.leftChild);
- preOrder(node.rightChild);
- }
- }
- /*
- *中序遍历二叉树
- * */
- public void inOrder(Node node){
- if(node != null){
- inOrder(node.leftChild);
- System.out.print(node.data);
- inOrder(node.rightChild);
- }
- }
- /*
- *后序遍历二叉树
- * */
- public void postOrder(Node node){
- if(node != null){
- postOrder(node.leftChild);
- postOrder(node.rightChild);
- System.out.print(node.data);
- }
- }
- public static void main(String ars[]){
- int[] a={2,4,12,45,21,6,111};
- BinTreeInt binTree = new BinTreeInt();
- for(int i = 0; i<a.length; i++){
- binTree.buildTree(binTree.root, a[i]);
- }
- System.out.print("前序遍历");
- binTree.preOrder(binTree.root);
- System.out.println("");
- System.out.print("中序遍历");
- binTree.inOrder(binTree.root);
- System.out.println("");
- System.out.print("后序遍历");
- binTree.postOrder(binTree.root);
- }
- }
栈的实现:
- /**
- *
- * @author kerryfish
- * JAVA实现二叉树的先序、中序、后序、层序遍历
- * 递归和非递归版本
- *
- */
- class Node{
- public int value;
- public Node left;
- public Node right;
- public Node(int v){
- this.value=v;
- this.left=null;
- this.right=null;
- }
- }
- class BinaryTreeTraversal {
- /**
- *
- * @param root 树根节点
- * 利用栈实现循环先序遍历二叉树
- * 这种实现类似于图的深度优先遍历(DFS)
- * 维护一个栈,将根节点入栈,然后只要栈不为空,出栈并访问,接着依次将访问节点的右节点、左节点入栈。
- * 这种方式应该是对先序遍历的一种特殊实现(看上去简单明了),但是不具备很好的扩展性,在中序和后序方式中不适用
- */
- public static void preOrderStack_1(Node root){
- if(root==null)return;
- Stack<Node> s=new Stack<Node>();
- s.push(root);
- while(!s.isEmpty()){
- Node temp=s.pop();
- System.out.println(temp.value);
- if(temp.right!=null) s.push(temp.right);
- if(temp.left!=null) s.push(temp.left);
- }
- }
- /**
- *
- * @param root 树的根节点
- * 利用栈模拟递归过程实现循环先序遍历二叉树
- * 这种方式具备扩展性,它模拟递归的过程,将左子树点不断的压入栈,直到null,然后处理栈顶节点的右子树
- */
- public static void preOrderStack_2(Node root){
- if(root==null)return;
- Stack<Node> s=new Stack<Node>();
- while(root!=null||!s.isEmpty()){
- while(root!=null){
- System.out.println(root.value);
- s.push(root);//先访问再入栈
- root=root.left;
- }
- root=s.pop();
- root=root.right;//如果是null,出栈并处理右子树
- }
- }
- /**
- *
- * @param root 树根节点
- * 利用栈模拟递归过程实现循环中序遍历二叉树
- * 思想和上面的preOrderStack_2相同,只是访问的时间是在左子树都处理完直到null的时候出栈并访问。
- */
- public static void inOrderStack(Node root){
- if(root==null)return;
- Stack<Node> s=new Stack<Node>();
- while(root!=null||!s.isEmpty()){
- while(root!=null){
- s.push(root);//先访问再入栈
- root=root.left;
- }
- root=s.pop();
- System.out.println(root.value);
- root=root.right;//如果是null,出栈并处理右子树
- }
- }
- /**
- *
- * @param root 树根节点
- * 后序遍历不同于先序和中序,它是要先处理完左右子树,然后再处理根(回溯),所以需要一个记录哪些节点已经被访问的结构(可以在树结构里面加一个标记),这里可以用map实现
- */
- public static void postOrderStack(Node root){
- if(root==null)return;
- Stack<Node> s=new Stack<Node>();
- Map<Node,Boolean> map=new HashMap<Node,Boolean>();
- s.push(root);
- while(!s.isEmpty()){
- Node temp=s.peek();
- if(temp.left!=null&&!map.containsKey(temp.left)){
- temp=temp.left;
- while(temp!=null){
- if(map.containsKey(temp))break;
- else s.push(temp);
- temp=temp.left;
- }
- continue;
- }
- if(temp.right!=null&&!map.containsKey(temp.right)){
- s.push(temp.right);
- continue;
- }
- Node t=s.pop();
- map.put(t,true);
- System.out.println(t.value);
- }
- }
- /**
- *
- * @param root 树根节点
- * 层序遍历二叉树,用队列实现,先将根节点入队列,只要队列不为空,然后出队列,并访问,接着讲访问节点的左右子树依次入队列
- */
- public static void levelTravel(Node root){
- if(root==null)return;
- Queue<Node> q=new LinkedList<Node>();
- q.add(root);
- while(!q.isEmpty()){
- Node temp = q.poll();
- System.out.println(temp.value);
- if(temp.left!=null)q.add(temp.left);
- if(temp.right!=null)q.add(temp.right);
- }
- }
- }
0 0
- 二叉树的前序、中序、后序遍历
- 二叉树的遍历(前序,中序,后序)
- 二叉树的遍历:前序、中序、后序
- 二叉树的前序、中序、后序遍历
- 二叉树的前序、中序、后序遍历
- 二叉树的前序,中序,后序遍历
- 二叉树的前序、中序、后序遍历
- 二叉树的前序,中序,后序遍历
- 二叉树的前序、中序、后序遍历
- 二叉树的前序、中序、后序遍历
- 二叉树的前序、中序、后序遍历
- 二叉树的前序,中序,后序遍历
- 二叉树的前序,中序,后序遍历
- 二叉树的前序、中序、后序遍历
- 二叉树的前/中/后序遍历
- JAVA实现二叉树的前、中、后序遍历
- 二叉树的前、中、后序遍历详解
- 二叉树的前、中、后序遍历
- TRUNK的作用功能.什么是TRUNK
- 希尔排序【O(n^1.3)】
- android 双击事件的实现
- 【c++】字符串的冒泡排序【存疑,待查】
- Hibernate关系映射1:单向N-1关联
- 二叉树的前序、中序、后序遍历
- zigbee协议解析:地址分配机制
- 并查集模板
- Python基础--人们一些最爱的标准库(random time)
- 屏幕适配问题1
- leetcode62.[DP] Unique Paths
- Mybatis junit测试时报: parameter *** not found.available 错误处理
- weblogic安装与配置
- TabControl控件和TabPage