/**
* 二叉树的建立(广度优先遍历)
*/
package com.wang.tree;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
import java.util.Stack;
public class ErCharTree<T> {
// 建立内部类作为二叉树的节点
private class ErCharTreeNode {
T data;
ErCharTreeNode llink, rlink;
public ErCharTreeNode(T data) {
this.data = data;
llink = null;
rlink = null;
}
}
ErCharTreeNode root = null;
public ErCharTreeNode getRoot() {
return root;
}
@SuppressWarnings("unchecked")
public void create() {
/* 建立根节点 */
Scanner scan = new Scanner(System.in);
ErCharTreeNode q = new ErCharTreeNode((T) new Integer(scan.nextInt()));
Queue<ErCharTreeNode> queue = new LinkedList<ErCharTreeNode>();
queue.offer(q);
root = q;
while (!queue.isEmpty()) {
ErCharTreeNode p = queue.poll();
int num = scan.nextInt();
if ((Integer) num == -1) {
break;
}
if (p.llink == null) {
q = new ErCharTreeNode((T) new Integer(num));
p.llink = q;
queue.offer(q);
}
int num1 = scan.nextInt();
if ((Integer) num1 == -1) {
break;
}
if (p.rlink == null) {
q = new ErCharTreeNode((T) new Integer(num1));
p.rlink = q;
queue.offer(q);
}
}
}
public void print() {
System.out.print("广度遍历顺序:");
Queue<ErCharTreeNode> queue = new LinkedList<ErCharTreeNode>();
if (root != null) {
ErCharTreeNode q = root;
System.out.print(q.data + " ");
queue.offer(q);
}
while (!queue.isEmpty()) {
ErCharTreeNode p = queue.poll();
if (p.llink != null) {
System.out.print(p.llink.data + " ");
ErCharTreeNode q = p.llink;
queue.offer(q);
}
if (p.rlink != null) {
System.out.print(p.rlink.data + " ");
ErCharTreeNode q = p.rlink;
queue.offer(q);
}
}
System.out.println();
}
// 先序遍历非递归算法(用栈)
public void perOrder(ErCharTreeNode root) {
System.out.print("先序遍历顺序:");
Stack<ErCharTreeNode> stack = new Stack<ErCharTreeNode>();
if (root != null) {
stack.push(root);
}
while (!stack.isEmpty()) {
ErCharTreeNode p = stack.pop();
System.out.print(p.data + " ");
if (p.rlink != null) {
stack.push(p.rlink);
}
if (p.llink != null) {
stack.push(p.llink);
}
}
System.out.println();
}
// 中序遍历非递归算法
public void midOrder1(ErCharTreeNode root) {
System.out.print("中序遍历顺序:");
ErCharTreeNode p = root;
Stack<ErCharTreeNode> stack = new Stack<ErCharTreeNode>();
while (p != null) {
while (p != null) {
if (p.rlink != null) {
stack.push(p.rlink);
}
stack.push(p);
p = p.llink;
}
p = stack.pop();
while (!stack.isEmpty() && p.rlink == null) {
System.out.print(p.data + " ");
p = stack.pop();
}
System.out.print(p.data + " ");
if (!stack.isEmpty()) {
p = stack.pop();
} else {
p = null;
}
}
System.out.println();
}
// 后序遍历非递归算法
void postOrder(ErCharTreeNode p) {
System.out.print("后序遍历顺序:");
Stack<ErCharTreeNode> stack = new Stack<ErCharTreeNode>();
ErCharTreeNode prev = p;
while (p != null || !stack.isEmpty()) {
while (p != null) {
stack.push(p);
p = p.llink;
}
if (!stack.isEmpty()) {
ErCharTreeNode temp = stack.peek().rlink;
if (temp == null || temp == prev) {
p = stack.pop();
System.out.print(p.data + " ");
prev = p;
p = null;
} else {
p = temp;
}
}
}
}
// 先序遍历递归算法
// public void perOrder1(ErCharTreeNode root) {
// if (root != null) {
// System.out.print(root.data + " ");
// perOrder1(root.llink);
// perOrder1(root.rlink);
// }
// }
// 中序遍历递归算法
// public void midOrder(ErCharTreeNode root) {
// if (root != null) {
// midOrder(root.llink);
// System.out.print(root.data + " ");
// midOrder(root.rlink);
// }
// }
public static void main(String[] args) {
ErCharTree<Integer> tree = new ErCharTree<Integer>();
tree.create();
tree.print();
// tree.perOrder1(tree.getRoot());
tree.perOrder(tree.getRoot());
// tree.midOrder(tree.getRoot());
tree.midOrder1(tree.getRoot());
tree.postOrder(tree.getRoot());
}
}
* 二叉树的建立(广度优先遍历)
*/
package com.wang.tree;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
import java.util.Stack;
public class ErCharTree<T> {
// 建立内部类作为二叉树的节点
private class ErCharTreeNode {
T data;
ErCharTreeNode llink, rlink;
public ErCharTreeNode(T data) {
this.data = data;
llink = null;
rlink = null;
}
}
ErCharTreeNode root = null;
public ErCharTreeNode getRoot() {
return root;
}
@SuppressWarnings("unchecked")
public void create() {
/* 建立根节点 */
Scanner scan = new Scanner(System.in);
ErCharTreeNode q = new ErCharTreeNode((T) new Integer(scan.nextInt()));
Queue<ErCharTreeNode> queue = new LinkedList<ErCharTreeNode>();
queue.offer(q);
root = q;
while (!queue.isEmpty()) {
ErCharTreeNode p = queue.poll();
int num = scan.nextInt();
if ((Integer) num == -1) {
break;
}
if (p.llink == null) {
q = new ErCharTreeNode((T) new Integer(num));
p.llink = q;
queue.offer(q);
}
int num1 = scan.nextInt();
if ((Integer) num1 == -1) {
break;
}
if (p.rlink == null) {
q = new ErCharTreeNode((T) new Integer(num1));
p.rlink = q;
queue.offer(q);
}
}
}
public void print() {
System.out.print("广度遍历顺序:");
Queue<ErCharTreeNode> queue = new LinkedList<ErCharTreeNode>();
if (root != null) {
ErCharTreeNode q = root;
System.out.print(q.data + " ");
queue.offer(q);
}
while (!queue.isEmpty()) {
ErCharTreeNode p = queue.poll();
if (p.llink != null) {
System.out.print(p.llink.data + " ");
ErCharTreeNode q = p.llink;
queue.offer(q);
}
if (p.rlink != null) {
System.out.print(p.rlink.data + " ");
ErCharTreeNode q = p.rlink;
queue.offer(q);
}
}
System.out.println();
}
// 先序遍历非递归算法(用栈)
public void perOrder(ErCharTreeNode root) {
System.out.print("先序遍历顺序:");
Stack<ErCharTreeNode> stack = new Stack<ErCharTreeNode>();
if (root != null) {
stack.push(root);
}
while (!stack.isEmpty()) {
ErCharTreeNode p = stack.pop();
System.out.print(p.data + " ");
if (p.rlink != null) {
stack.push(p.rlink);
}
if (p.llink != null) {
stack.push(p.llink);
}
}
System.out.println();
}
// 中序遍历非递归算法
public void midOrder1(ErCharTreeNode root) {
System.out.print("中序遍历顺序:");
ErCharTreeNode p = root;
Stack<ErCharTreeNode> stack = new Stack<ErCharTreeNode>();
while (p != null) {
while (p != null) {
if (p.rlink != null) {
stack.push(p.rlink);
}
stack.push(p);
p = p.llink;
}
p = stack.pop();
while (!stack.isEmpty() && p.rlink == null) {
System.out.print(p.data + " ");
p = stack.pop();
}
System.out.print(p.data + " ");
if (!stack.isEmpty()) {
p = stack.pop();
} else {
p = null;
}
}
System.out.println();
}
// 后序遍历非递归算法
void postOrder(ErCharTreeNode p) {
System.out.print("后序遍历顺序:");
Stack<ErCharTreeNode> stack = new Stack<ErCharTreeNode>();
ErCharTreeNode prev = p;
while (p != null || !stack.isEmpty()) {
while (p != null) {
stack.push(p);
p = p.llink;
}
if (!stack.isEmpty()) {
ErCharTreeNode temp = stack.peek().rlink;
if (temp == null || temp == prev) {
p = stack.pop();
System.out.print(p.data + " ");
prev = p;
p = null;
} else {
p = temp;
}
}
}
}
// 先序遍历递归算法
// public void perOrder1(ErCharTreeNode root) {
// if (root != null) {
// System.out.print(root.data + " ");
// perOrder1(root.llink);
// perOrder1(root.rlink);
// }
// }
// 中序遍历递归算法
// public void midOrder(ErCharTreeNode root) {
// if (root != null) {
// midOrder(root.llink);
// System.out.print(root.data + " ");
// midOrder(root.rlink);
// }
// }
public static void main(String[] args) {
ErCharTree<Integer> tree = new ErCharTree<Integer>();
tree.create();
tree.print();
// tree.perOrder1(tree.getRoot());
tree.perOrder(tree.getRoot());
// tree.midOrder(tree.getRoot());
tree.midOrder1(tree.getRoot());
tree.postOrder(tree.getRoot());
}
}