当前位置: 移动技术网 > IT编程>开发语言>Java > java 完全二叉树的构建与四种遍历方法示例

java 完全二叉树的构建与四种遍历方法示例

2019年07月22日  | 移动技术网IT编程  | 我要评论

本来就是基础知识,不能丢的太干净,今天竟然花了那么长的时间才写出来,记一下。

有如下的一颗完全二叉树:

先序遍历结果应该为:1  2  4  5  3  6  7

中序遍历结果应该为:4  2  5  1  6  3  7

后序遍历结果应该为:4  5  2  6  7  3  1

层序遍历结果应该为:1  2  3  4  5  6  7

二叉树的先序遍历、中序遍历、后序遍历其实都是一样的,都是执行递归操作。

我这记录一下层次遍历吧:层次遍历需要用到队列,先入队在出队,每次出队的元素检查是其是否有左右孩子,有则将其加入队列,由于利用队列的先进先出原理,进行层次遍历。

下面记录下完整代码(java实现),包括几种遍历方法:

import java.util.arraydeque;
import java.util.arraylist;
import java.util.list;
import java.util.queue;


/**
 * 定义二叉树节点元素
 * @author bubble
 *
 */
class node {  
  public node leftchild;
  public node rightchild;
  public int data;

  public node(int data) {
    this.data = data;
  }

}

public class testbintree {
  
  /**
   * 将一个arry数组构建成一个完全二叉树
   * @param arr 需要构建的数组
   * @return 二叉树的根节点
   */
  public node initbintree(int[] arr) {
    if(arr.length == 1) {
      return new node(arr[0]);
    }
    list<node> nodelist = new arraylist<>();
    
    for(int i = 0; i < arr.length; i++) {
      nodelist.add(new node(arr[i]));
    }
    int temp = 0;
    while(temp <= (arr.length - 2) / 2) { //注意这里,数组的下标是从零开始的
      if(2 * temp + 1 < arr.length) {
        nodelist.get(temp).leftchild = nodelist.get(2 * temp + 1);
      }
      if(2 * temp + 2 < arr.length) {
        nodelist.get(temp).rightchild = nodelist.get(2 * temp + 2);
      }
      temp++;
    }
    return nodelist.get(0);
    }
 
  /**
   * 层序遍历二叉树,,并分层打印
   * @param root 二叉树根节点
   *
   */
   public void trivalbintree(node root) {
    queue<node> nodequeue = new arraydeque<>(); 
    nodequeue.add(root);
    node temp = null;
    int currentlevel = 1;  //记录当前层需要打印的节点的数量
    int nextlevel = 0;//记录下一层需要打印的节点的数量
    while ((temp = nodequeue.poll()) != null) {
      if (temp.leftchild != null) {
        nodequeue.add(temp.leftchild);
        nextlevel++;
        
      }
      if (temp.rightchild != null) {
        nodequeue.add(temp.rightchild);
        nextlevel++;
      }
      system.out.print(temp.data + " ");
      currentlevel--;
      if(currentlevel == 0) {
        system.out.println();
        currentlevel = nextlevel;
        nextlevel = 0;
      }
    }
  }
  

   /**
    * 先序遍历
    * @param root 二叉树根节点
    */
    public void pretrival(node root) {
      if(root == null) {
        return;
      }
      system.out.print(root.data + " ");
      pretrival(root.leftchild);
      pretrival(root.rightchild);
    }
    /**
     * 中序遍历
     * @param root 二叉树根节点
     */
    public void midtrival(node root) {
      if(root == null) {
        return;
      }
      midtrival(root.leftchild);
      system.out.print(root.data + " ");
      midtrival(root.rightchild);
    }
    /**
     * 后序遍历
     * @param root 二叉树根节点
     */
    public void aftertrival(node root) {
      if(root == null) {
        return;
        
      }
      aftertrival(root.leftchild);
      aftertrival(root.rightchild);
      system.out.print(root.data + " ");
    }
    
    
    public static void main(string[] args) {
      testbintree btree = new testbintree();
      int[] arr = new int[] {1,2,3,4,5,6,7};
      node root = btree.initbintree(arr);
      system.out.println("层序遍历(分层打印):");
      btree.trivalbintree(root);
      system.out.println("\n先序遍历:");
      btree.pretrival(root);
      system.out.println("\n中序遍历:");
      btree.midtrival(root);
      system.out.println("\n后序遍历:");
      btree.aftertrival(root);
      
    }
    
   } 

遍历结果:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网