二叉树的三种排序Java实现

本文介绍如何创建二叉树并实现先序、中序和后序遍历,通过实例代码演示了遍历过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package test3;


import java.util.LinkedList;
import java.util.List;


public class BinTreeTraverse {
   private int []array={1,5,6,66,88,456,223,20,693,123};
   private static List<Node> nodeList=null;
   
   class Node{ 
  Node leftChild;
  Node rightChild;
  int data;
  Node(int data){ 
  leftChild=null;
  rightChild=null;
  data=data;
  }
   }
   
   public void createBinTree(){ 
  nodeList=new LinkedList<Node>();
  for(int nodeIndex=0; nodeIndex<array.length;nodeIndex++){ 
  nodeList.add(new Node(array[nodeIndex]));
  }
  for(int parentIndex=0;parentIndex<array.length/2-1;parentIndex++){ 
  nodeList.get(parentIndex).leftChild = nodeList  
                   .get(parentIndex * 2 + 1);  
           // 右孩子  
           nodeList.get(parentIndex).rightChild = nodeList  
                   .get(parentIndex * 2 + 2);  
  }
  int lastParentIndex = array.length / 2 - 1;  
  nodeList.get(lastParentIndex).leftChild = nodeList  
               .get(lastParentIndex * 2 + 1);  
       // 右孩子,如果数组的长度为奇数才建立右孩子  
       if (array.length % 2 == 1) {  
           nodeList.get(lastParentIndex).rightChild = nodeList  
                   .get(lastParentIndex * 2 + 2);  
       }  
   }
   /** 
    * 先序遍历 
    *  
    * 这三种不同的遍历结构都是一样的,只是先后顺序不一样而已 
    *  
    * @param node 
    *            遍历的节点 
    */  
   public static void preOrderTraverse(Node node) {  
       if (node == null)  
           return;  
       System.out.print(node.data + " ");  
       preOrderTraverse(node.leftChild);  
       preOrderTraverse(node.rightChild);  
   }  
 
   /** 
    * 中序遍历 
    *  
    * 这三种不同的遍历结构都是一样的,只是先后顺序不一样而已 
    *  
    * @param node 
    *            遍历的节点 
    */  
   public static void inOrderTraverse(Node node) {  
       if (node == null)  
           return;  
       inOrderTraverse(node.leftChild);  
       System.out.print(node.data + " ");  
       inOrderTraverse(node.rightChild);  
   }  
 
   /** 
    * 后序遍历 
    *  
    * 这三种不同的遍历结构都是一样的,只是先后顺序不一样而已 
    *  
    * @param node 
    *            遍历的节点 
    */  
   public static void postOrderTraverse(Node node) {  
       if (node == null)  
           return;  
       postOrderTraverse(node.leftChild);  
       postOrderTraverse(node.rightChild);  
       System.out.print(node.data + " ");  
   }  
 
   public static void main(String[] args) {  
       BinTreeTraverse binTree = new BinTreeTraverse();  
       binTree.createBinTree();  
       // nodeList中第0个索引处的值即为根节点  
       Node root = nodeList.get(0);  
 
       System.out.println("先序遍历:");  
       preOrderTraverse(root);  
       System.out.println();  
 
       System.out.println("中序遍历:");  
       inOrderTraverse(root);  
       System.out.println();  
 
       System.out.println("后序遍历:");  
       postOrderTraverse(root);  
   }  
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值