1.结点类
package edu.tcu.soft.binary;
public class BinaryNode {
private int value;
private BinaryNode left;
private BinaryNode right;
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public BinaryNode getLeft() {
return left;
}
public void setLeft(BinaryNode left) {
this.left = left;
}
public BinaryNode getRight() {
return right;
}
public void setRight(BinaryNode right) {
this.right = right;
}
public BinaryNode(int value, BinaryNode left, BinaryNode right) {
super();
this.value = value;
this.left = left;
this.right = right;
}
public BinaryNode() {
}
public BinaryNode(int value) {
this.left=null;
this.right=null;
this.value=value;
}
}
2.二叉树类
<pre name="code" class="java">package edu.tcu.soft.binary;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
public class BinaryTree {
public BinaryTree() {
}
// 插入结点
public void insert(BinaryNode root, int data) {
if (data > root.getValue()) // 二叉树的左节点都比根节点小
{
if (root.getRight() == null) {
root.setRight(new BinaryNode(data));
} else {
this.insert(root.getRight(), data);
}
} else { // 二叉树的右节点都比根节点大
if (root.getLeft() == null) {
root.setLeft(new BinaryNode(data));
} else {
this.insert(root.getLeft(), data);
}
}
}
// 前序遍历
public void preOrder(BinaryNode root) {
// -------------------1.递归
// if (root == null) {
// return;
// } else {
// System.out.print(root.getValue() + " ");// 输出结点的数据域
// preOrder(root.getLeft()); // 遍历左子树
// preOrder(root.getRight());//遍历右子树
// }
// -----------------2.非递归
Stack<BinaryNode> stack = new Stack<BinaryNode>();
while (root != null || !stack.empty())// 当根节点为空或者栈为空才跳出循环
{
while (root != null) {
System.out.print(root.getValue() + " ");
stack.push(root);
root = root.getLeft();
}
if (!stack.empty()) {
root = stack.pop();
root = root.getRight();
}
}
}
// 中序遍历
public void inOrder(BinaryNode root) {
// ----------------1.递归
// if (root == null)
// return;
// else {
// inOrder(root.getLeft());
// System.out.print(root.getValue() + " ");
// inOrder(root.getRight());
// }
// -----------------2.非递归
Stack<BinaryNode> stack = new Stack<BinaryNode>();
BinaryNode s = new BinaryNode();
while (root != null || !stack.empty()) {
while (root != null) {
stack.push(root);
root = root.getLeft();
}
if (!stack.empty()) {
s = stack.pop();
System.out.print(s.getValue() + " ");
root = s.getRight();
}
}
}
// 后序遍历
public void postOrder(BinaryNode root) {
//--------------递归
// if(root==null)
// return;
// else{
// postOrder(root.getLeft());
// postOrder(root.getRight());
// System.out.println(root.getValue());
// }
// ---------------非递归 ----使用了两个栈
Stack<BinaryNode> stack = new Stack<BinaryNode>();
Stack<Integer> stack2 = new Stack<Integer>();
Integer i = new Integer(1);
while (root != null || !stack.empty()) {
while (root != null) {
stack.push(root);
stack2.push(new Integer(0));
root = root.getLeft();
}
while (!stack.empty() && stack2.peek().equals(i)) {
stack2.pop();
System.out.print(stack.pop().getValue() + " ");
}
if (!stack.empty()) {
stack2.pop();
stack2.push(new Integer(1));
root = stack.peek();
root = root.getRight();
}
}
// ---------------------非递归 --------使用一个栈
//还没实现 待续
}
// 层序遍历
public void levelOrder(BinaryNode root) {
if (root == null)
return;
else {
Queue<BinaryNode> queue = new LinkedList<BinaryNode>();
queue.add(root);
while (!queue.isEmpty()) {
BinaryNode node = queue.remove();
System.out.print(node.getValue() + "-");
if (node.getLeft() != null)
queue.add(node.getLeft());
if (node.getRight() != null)
queue.add(node.getRight());
}
}
}
// 释放结点
public void release(BinaryNode node) {
if (node == null)
return;
else {
release(node.getLeft());
release(node.getRight());
node = null;
}
}
}
3.测试类
package edu.tcu.soft.binary;
public class Test {
public static void main(String[] args) {
int a[]={10,12,9,8,15,14};
BinaryNode root=new BinaryNode(a[0]);
BinaryTree tree=new BinaryTree();
for(int i=1;i<a.length;i++){
tree.insert(root, a[i]); //向二叉树中插入数据
}
// tree.preOrder(root);
// System.out.println();
// tree.inOrder(root);
// System.out.println();
tree.levelOrder(root);
}
}