Java构建二叉树

Java构建二叉树

二叉树节点类定义:

public class Node{
    int data;
    Node leftChild;
    Node rightChild;
    
    //构造方法
    Node(int data){
        this.data = data;
        leftChild = null;
        rightChild = null;
    }
}
(1)直接构造二叉树(略)
(2)通过数组来构造二叉树
思路:1)将数组元素转换成Node节点。
          2)用LinkedList数据结构存储Node节点。
          3)分别处理前n-1个父节点和最后1个父节点,因为最后一个父节点可能没有右孩子。
          4)父节点索引与左右孩子索引的关系:
                左孩子索引=父节点索引*2+1;

                右孩子索引=父节点索引*2+2;

具体java实现:

public class BinTreeTraverse2 {  
	   
	     private int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };  
	     private static List<Node> nodeList = null;  
	   
	     /** 
	      * 内部类:节点 
	      *  
	      * @author ocaicai@yeah.net @date: 2011-5-17 
	      *  
	     */  
	     private static class Node {  
	         Node leftChild;  
	         Node rightChild;  
	         int data;  
	   
	         Node(int newData) {  
	             leftChild = null;  
	             rightChild = null;  
	             data = newData;  
	         }  
	     }  
	   
	     public void createBinTree() {  
	         nodeList = new LinkedList<Node>();  
	         // 将一个数组的值依次转换为Node节点  
	         for (int nodeIndex = 0; nodeIndex < array.length; nodeIndex++) {  
	             nodeList.add(new Node(array[nodeIndex]));  
	         }  
	         // 对前lastParentIndex-1个父节点按照父节点与孩子节点的数字关系建立二叉树  
	         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);  
	         }  
	     } 
	 } 

java使用递归,非递归方式实现二叉树的三种常见遍历方式

//二叉树的遍历 递归非递归 思路和 java实现:http://blog.csdn.net/clam_clam/article/details/6845399
	    /** 非递归实现后序遍历 单栈法*/ 
	     protected static void iterativePostorder3(Node p) {    
	         Stack<Node> stack = new Stack<Node>();    
	         Node node = p, prev = p;    
	         while (node != null || stack.size() > 0) {    
	             while (node != null) {    
	                 stack.push(node);    
	                 node = node.getLeft();    
	             }    
	             if (stack.size() > 0) {    
	                 Node temp = stack.peek().getRight();    
	                 if (temp == null || temp == prev) {    
	                     node = stack.pop();    
	                     visit(node);    
	                     prev = node;    
	                     node = null;    
	                 } else {    
	                     node = temp;    
	                 }    
	             }    
	     
	         }    
	     }    
	     
	     /** 非递归实现后序遍历4 双栈法*/    
	     protected static void iterativePostorder4(Node p) {    
	         Stack<Node> stack = new Stack<Node>();    
	         Stack<Node> temp = new Stack<Node>();    
	         Node node = p;    
	         while (node != null || stack.size() > 0) {    
	             while (node != null) {    
	                 temp.push(node);    
	                 stack.push(node);    
	                 node = node.getRight();    
	             }    
	             if (stack.size() > 0) {    
	                 node = stack.pop();    
	                 node = node.getLeft();    
	             }    
	         }    
	         while (temp.size() > 0) {//把插入序列都插入到了temp。  
	             node = temp.pop();    
	             visit(node);    
	         }    
	     }    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值