- /**
- * 实现二叉树的创建、前序遍历、中序遍历和后序遍历
- **/
- package DataStructure;
- /**
- * Copyright 2014 by Ruiqin Sun
- * All right reserved
- * created on 2014-9-9 下午2:34:15
- **/
- public class BinTreeInt {
- private Node root;
- /**
- * 创建内部节点类
- **/
- private class Node{
- // 左节点
- private Node leftChild;
- // 右节点
- private Node rightChild;
- // 节点对应的值
- private int data;
- public Node(int data){
- this.leftChild = null;
- this.rightChild = null;
- this.data = data;
- }
- }// class Node
- public BinTreeInt(){
- root = null;
- }
- /*
- *递归的创建二叉树
- * */
- public void buildTree(Node node ,int data){
- if (root == null){// 如果根节点为空,创建根节点
- root = new Node(data);
- }else{
- if(data <node.data){//插入到左子树
- if(node.leftChild == null){//左节点为空,直接创建值为data的左节点
- node.leftChild = new Node(data);
- }else{//左节点不为空,调用buildTree函数插到左子树中
- buildTree(node.leftChild,data);
- }
- }else{
- if(node.rightChild == null){
- node.rightChild = new Node(data);
- }else{
- buildTree(node.rightChild,data);
- }
- }
- }
- }//end buildTree
- /*
- *前序遍历二叉树
- * */
- public void preOrder(Node node){
- if(node != null){
- System.out.print(node.data);
- preOrder(node.leftChild);
- preOrder(node.rightChild);
- }
- }
- /*
- *中序遍历二叉树
- * */
- public void inOrder(Node node){
- if(node != null){
- inOrder(node.leftChild);
- System.out.print(node.data);
- inOrder(node.rightChild);
- }
- }
- /*
- *后序遍历二叉树
- * */
- public void postOrder(Node node){
- if(node != null){
- postOrder(node.leftChild);
- postOrder(node.rightChild);
- System.out.print(node.data);
- }
- }
- public static void main(String ars[]){
- int[] a={2,4,12,45,21,6,111};
- BinTreeInt binTree = new BinTreeInt();
- for(int i = 0; i<a.length; i++){
- binTree.buildTree(binTree.root, a[i]);
- }
- System.out.print("前序遍历");
- binTree.preOrder(binTree.root);
- System.out.println("");
- System.out.print("中序遍历");
- binTree.inOrder(binTree.root);
- System.out.println("");
- System.out.print("后序遍历");
- binTree.postOrder(binTree.root);
- }
- }
//非递归先序遍历
public static void preTraverse(BinaryTree root) {
Stack s = new Stack();
s.push(root);
while(!s.isEmpty()) {
BinaryTree bt = (BinaryTree)s.pop();
visit(bt);
if(bt.rchild != null) s.push(bt.rchild);
if(bt.lchild != null) s.push(bt.lchild);
}
}
//非递归中序遍历
public static void inTraverse(BinaryTree root) {
Stack s = new Stack();
BinaryTree p = root;
while(p!=null || !s.isEmpty()) {
if(p!=null) {
s.push(p);
p = p.lchild;
}
else {
p = (BinaryTree)s.pop();
visit(p);
p = p.rchild;
}
}
}
//非递归后序遍历
public static void postTraverse(BinaryTree root) {
Stack s = new Stack();
BinaryTree p = root;
//pre标记最近出栈的节点,用于判断是否是p节点的右孩子,如果是的话,就可以访问p节点
BinaryTree pre = p;
//flag标记是出栈还是继续将左孩子进栈
boolean flag = true;
while(p!=null || !s.isEmpty()) {
if(p!=null && flag) {
s.push(p);
p = p.lchild;
}
else {
if(s.isEmpty()) return;
p = (BinaryTree)s.peek();
if(p.rchild != null && p.rchild!=pre) {
p = p.rchild;
flag = true;
}
else {
p = (BinaryTree)s.pop();
visit(p);
flag = false;
pre = p;
}
}
}
}
//非递归后序遍历2
public static void postTraverse2(BinaryTree root) {
Stack s = new Stack();
BinaryTree p = root;
//pre标记最近出栈的节点,用于判断是否是p节点的右孩子,如果是的话,就可以访问p节点
BinaryTree pre = p;
while(p!=null || !s.isEmpty()) {
if(p!=null) {
s.push(p);
p = p.lchild;
}
else {
if(s.isEmpty()) return;
p = (BinaryTree)s.peek();
if(p.rchild != null && p.rchild!=pre) {
p = p.rchild;
}
else {
p = (BinaryTree)s.pop();
visit(p);
pre = p;
p = null;
}
public static void preTraverse(BinaryTree root) {
Stack s = new Stack();
s.push(root);
while(!s.isEmpty()) {
BinaryTree bt = (BinaryTree)s.pop();
visit(bt);
if(bt.rchild != null) s.push(bt.rchild);
if(bt.lchild != null) s.push(bt.lchild);
}
}
//非递归中序遍历
public static void inTraverse(BinaryTree root) {
Stack s = new Stack();
BinaryTree p = root;
while(p!=null || !s.isEmpty()) {
if(p!=null) {
s.push(p);
p = p.lchild;
}
else {
p = (BinaryTree)s.pop();
visit(p);
p = p.rchild;
}
}
}
//非递归后序遍历
public static void postTraverse(BinaryTree root) {
Stack s = new Stack();
BinaryTree p = root;
//pre标记最近出栈的节点,用于判断是否是p节点的右孩子,如果是的话,就可以访问p节点
BinaryTree pre = p;
//flag标记是出栈还是继续将左孩子进栈
boolean flag = true;
while(p!=null || !s.isEmpty()) {
if(p!=null && flag) {
s.push(p);
p = p.lchild;
}
else {
if(s.isEmpty()) return;
p = (BinaryTree)s.peek();
if(p.rchild != null && p.rchild!=pre) {
p = p.rchild;
flag = true;
}
else {
p = (BinaryTree)s.pop();
visit(p);
flag = false;
pre = p;
}
}
}
}
//非递归后序遍历2
public static void postTraverse2(BinaryTree root) {
Stack s = new Stack();
BinaryTree p = root;
//pre标记最近出栈的节点,用于判断是否是p节点的右孩子,如果是的话,就可以访问p节点
BinaryTree pre = p;
while(p!=null || !s.isEmpty()) {
if(p!=null) {
s.push(p);
p = p.lchild;
}
else {
if(s.isEmpty()) return;
p = (BinaryTree)s.peek();
if(p.rchild != null && p.rchild!=pre) {
p = p.rchild;
}
else {
p = (BinaryTree)s.pop();
visit(p);
pre = p;
p = null;
}