- 首先要创建二叉树,将数组以节点的形式存到LinkedList中,然后再根据数组转化为二叉树时节点之间的数字关系构造二叉树.
- 实现数组的先序遍历、中序遍历、后续遍历以及广度优先搜索、深度优先搜索等。
- 实现广度优先搜索即是层次遍历,需要借用队列先进先出的特点。
- 实现深度优先搜索类似于先序遍历,借用栈先进后出的特点。
- 实现代码如下:
package tree;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Stack;
/**
*@author xpengfei
*@creat 10:04:02 AM Sep 1, 2017
*/
public class Array_To_BTree {
private static int[]array={1,2,3,4,5,6,7,8,9};
private static List<Node>nodelist=null;
class Node{
Node lchild=null;
Node rchild=null;
int data;
Node(){}
Node(int data){
this.data=data;
}
}
public void createTree(){
nodelist=new LinkedList();
for(int i=0;i<array.length;i++){
nodelist.add(new Node(array[i]));
}
int lastParentNode=array.length/2-1;
for (int parentIndex = 0; parentIndex < lastParentNode; parentIndex++) {
nodelist.get(parentIndex).lchild=nodelist.get(parentIndex*2+1);
nodelist.get(parentIndex).rchild=nodelist.get(parentIndex*2+2);
}
nodelist.get(lastParentNode).lchild=nodelist.get(lastParentNode*2+1);
if(array.length%2==1){
nodelist.get(lastParentNode).rchild=nodelist.get(lastParentNode*2+2);
}
}
/**
* 先序遍历
* @param node
*/
public void preOrderTraverse(Node node){
if(node==null){
return;
}
System.out.print(node.data+"\t");
preOrderTraverse(node.lchild);
preOrderTraverse(node.rchild);
}
/**
* 中序遍历
* @param node
*/
public void midOrderTraverse(Node node){
if(node==null){
return;
}
midOrderTraverse(node.lchild);
System.out.print(node.data+"\t");
midOrderTraverse(node.rchild);
}
/**
* 后序遍历
* @param node
*/
public void lastOrderTraverse(Node node){
if(node==null){
return;
}
lastOrderTraverse(node.lchild);
lastOrderTraverse(node.rchild);
System.out.print(node.data+"\t");
}
/**
* 广度优先遍历即层次遍历
* 使用队列
* 广度优先遍历 1 2 3 4 5 6 7 8 9
* @param root 根节点
*/
public void widthOrderTraverse(Node root){
if(root==null){
System.out.println("树为空!");
return;
}
Queue<Node>queue=new LinkedList<Node>();
queue.offer(root);
System.out.println("广度优先遍历:");
while(!queue.isEmpty()){
Node firstNode=queue.poll();
System.out.print(firstNode.data+"\t");
if(firstNode.lchild!=null)
queue.offer(firstNode.lchild);
if(firstNode.rchild!=null)
queue.offer(firstNode.rchild);
}
System.out.println();
}
/**
* 深度优先遍历类似于先序遍历
* 使用栈
* 深度优先遍历 1 2 4 8 9 5 3 6 7
* @param root
*/
public void depthOrderTraverse(Node root){
Stack<Node>stack=new Stack();
if(root==null){
System.out.println("树为空!");
return;
}
stack.push(root);
System.out.println("深度优先遍历:");
while(stack.isEmpty()==false){
Node node=stack.pop();
System.out.print(node.data+"\t");
if(node.rchild!=null)
stack.push(node.rchild);
if(node.lchild!=null)
stack.push(node.lchild);
}
System.out.println();
}
public static void main(String[] args) {
Array_To_BTree array_To_BTree=new Array_To_BTree();
array_To_BTree.createTree();
Node root =nodelist.get(0);
System.out.println("先序遍历:");
array_To_BTree.preOrderTraverse(root);
System.out.println();
System.out.println("中序遍历:");
array_To_BTree.midOrderTraverse(root);
System.out.println();
System.out.println("后序遍历:");
array_To_BTree.lastOrderTraverse(root);
System.out.println();
array_To_BTree.widthOrderTraverse(root);
array_To_BTree.depthOrderTraverse(root);
}
}
- 在代码中使用LinkedList而不是List或ArrayList的原因是:LinkedList添加了可以用作栈、队列或双端队列的方法,在实现插入和删除的时候会更高效。而在实现随机访问的时候ArrayList会更加的高效。
- 队列中peek()和element()方法都将在不删除的情况下返回队头,但peek()方法在队列为空时返回的是null而element()方法则是抛出NoSuchElementException异常.
- poll()和remove()方法都将移除并返回队头,但poll()在队列为空时返回null,而remove()方法则是抛出NoSuchElementException异常
- offer()方法在允许的情况下将一个元素插入到队尾,或者返回false.