树型结构是一类重要的非线性数据结构。其中以树和二叉树最为常用,直观看来,树是以分支关系定义的层次结构。树结构在客观世界中广泛存在,树在计算机领域中也有广泛的应用,如在编译程序中,可用树来表示源程序的语法结构。
树作为非常基础的数据结构,有必要了解它的基本原理和使用场合,复习二叉树的数据结构,要分几个部分循序渐进地进行,这篇博客只涉及二叉树的构造和遍历。
首先类用于表示树的一个节点:
package algorithm.binarytree;
/**
* <p>Description: Test</p>
* <p>Copyright (c) 2010</p>
* <p>Department: </p>
* <p>Company: </p>
* <p>Project:Test</p>
*
* @author Tomy Zhou
* @since 1.0
* @version algorithm.binarytree; BinaryTreeNode.java
* 2010-4-26
**/
public class BinaryTreeNode
<T> {
public T data;
public BinaryTreeNode leftChild, rightChild;
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public BinaryTreeNode getLeftChild() {
return leftChild;
}
public void setLeftChild(BinaryTreeNode leftChild) {
this.leftChild = leftChild;
}
public BinaryTreeNode getRightChild() {
return rightChild;
}
public void setRightChild(BinaryTreeNode rightChild) {
this.rightChild = rightChild;
}
}
package algorithm.binarytree;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* <p>Description: Test</p>
* <p>Copyright (c) 2010</p>
* <p>Department: </p>
* <p>Company: </p>
* <p>Project:Test</p>
*
* @author Tomy Zhou
* @since 1.0
* @version algorithm.binarytree; BinaryTree.java
* 2010-4-26
**/
public class BinaryTree<T> {
BinaryTreeNode<T> root;
public BinaryTree(){
}
/**
* 二叉树的创建
* @param list
*/
public void createBiTree(List<T> list){
Iterator<T> iterator = list.iterator();
while(iterator.hasNext()){
BinaryTreeNode<T> node = new BinaryTreeNode<T>();
node.setData(iterator.next());
insertTree(node);
}
}
/**
* 插入时,从根节点开始判断它是否有Child,如果没有,则将节点的孩子
* 指向为传入参数的引用
*
* @param node
*/
public void insertTree(BinaryTreeNode<T> node) {
if (root == null) {
root = node;
} else {
BinaryTreeNode<T> current = root;
while (true) {
if (current.leftChild == null) {
current.leftChild = node;
return;
} else if (root.rightChild == null) {
current.rightChild = node;
return;
} else {
current = current.leftChild;
}
}
}
}
/**
* 先序递归遍历-按照root,left子树和right子树顺序遍历
*
*/
public void preOrderTraverse(BinaryTreeNode node){
System.out.println(node.getData());
if(node.getLeftChild()!=null){
preOrderTraverse(node.getLeftChild());
}
if(node.getRightChild()!=null){
preOrderTraverse(node.getRightChild());
}
}
/**
* 先序循环遍历-按照root,left子树和right子树顺序遍历
*
*/
public void preOrderTraverseLoop(BinaryTreeNode node){
//System.out.println(node.getData());
Stack<BinaryTreeNode> stack = new Stack<BinaryTreeNode>();
stack.push(node);
while(!stack.isEmpty()){
BinaryTreeNode biNode = stack.pop();
System.out.println(biNode.getData());
BinaryTreeNode left = biNode.leftChild;
if(left!=null){
stack.push(left);
}
BinaryTreeNode right = biNode.rightChild;
if(right!=null){
stack.push(right);
}
}
}
public static void main(String[] args) {
Integer[] ints = new Integer[]{1,3,9,8,7,6,2,98,76};
List list = new ArrayList<Integer>(ints.length);
for (int i = 0; i < ints.length; i++) {
list.add(ints[i]);
}
BinaryTree<Integer> binaryTree = new BinaryTree<Integer>();
binaryTree.createBiTree(list);
binaryTree.preOrderTraverse(binaryTree.root);
}
}