首先我们定义一下树的数据结构
@Data
public class BiTree {
private Integer data;//数据
private BiTree lchild;//结点左孩子
private BiTree rchild;//结点右孩子
public BiTree() {
}
public BiTree(Integer data) {
this.data = data;
}
}
有了树的结构那么我们可以初始化树了,我们初始化得树的结构如下图:
//初始化树的的方法
private static BiTree init() {
BiTree biTree = new BiTree(1);
BiTree tow = new BiTree(2);
BiTree three = new BiTree(3);
BiTree four = new BiTree(4);
BiTree five = new BiTree(5);
BiTree six = new BiTree(6);
BiTree seven = new BiTree(7);
biTree.setLchild(tow);
biTree.setRchild(three);
tow.setLchild(four);
tow.setRchild(five);
three.setLchild(six);
three.setRchild(seven);
return biTree;
}
栈实现树的前序遍历:
public class PreOrderWithStack {
private static int top = -1;
private static BiTree[] stack = new BiTree[20];
private BiTree getTop() {
return stack[top];
}
private void push(BiTree tree) {
stack[++top] = tree;
}
private void pop() {
if (top > -1)
top--;
}
private void print(BiTree node) {
System.out.print(node.getData() + " ");
}
public void preOrder(BiTree biTree) {
push(biTree);//将根节点入栈
while (top != -1) {//栈不为空时
BiTree p = getTop();//取出栈顶元素
pop();//指针下移一位
while (p != null) {
print(p);//答应根节点
if (p.getRchild() != null) {//将右孩子压栈
push(p.getRchild());
}
p = p.getLchild();//处理左孩子
}
}
}
}
栈实现树的中序遍历:
public class InOrderWithStack {
public static int top = -1;
public static BiTree[] stack = new BiTree[20];
private BiTree getTop() {
return stack[top];
}
private void push(BiTree tree) {
stack[++top] = tree;
}
private void pop() {
if (top > -1)
top--;
}
private void print(BiTree node) {
System.out.print(node.getData() + " ");
}
/**
* 中序遍历用栈实现方法一
* @param tree
*/
public void doInOrderMethodOne(BiTree tree) {
BiTree p = tree;
while (p != null || top != -1) {
if (p != null) {
push(p);
p = p.getLchild();
} else {
p = getTop();
pop();
print(p);
p = p.getRchild();
}
}
}
/**
* 中序遍历用栈实现方法二
* @param tree
*/
public void doInOrderMethodTwo(BiTree tree) {
push(tree);
while (top != -1) {
BiTree p = getTop();
while (p != null) {
push(p.getLchild());
p = getTop();
}
pop();
if (top != -1) {
p = getTop();
pop();
print(p);
push(p.getRchild());
}
}
}
}
栈实现树的后序遍历:
public class PostOrderWithStack {
private static int top = -1;
private static SNode[] sNodes = new SNode[20];
private void push(SNode sNode) {
sNodes[++top] = sNode;
}
private static SNode getTop() {
return sNodes[top];
}
private void pop() {
if (top > -1)
top--;
}
private void print(BiTree node) {
System.out.print(node.getData() + " ");
}
public void inOrder(BiTree tree) {
BiTree p = tree;
while (p != null || top != -1) {
while (p != null) {
SNode sNode = new SNode(p, 0);
push(sNode);
p = p.getLchild();
}
SNode top = getTop();
pop();
if (top.getTag() == 0) {
top.setTag(1);
push(top);
p = top.getP().getRchild();
} else {
print(top.getP());
}
}
}
@Data
@AllArgsConstructor
@NoArgsConstructor
class SNode {
private BiTree p;
private int tag;//用来标记该节点是否已经处理过
}
}
层级遍历
public class Hierarchy {
private static int front = 0;
private static int rear = 0;
private static BiTree[] queue = new BiTree[20];
private static void enQueue(BiTree node) {//入队函数
queue[rear++] = node;
}
private static BiTree deQueue() { //出队函数
return queue[front++];
}
private static void print(BiTree node) {
System.out.print(node.getData() + " ");
}
public static void hierarchy(BiTree tree) {
enQueue(tree);
while (front < rear) {
BiTree biTree = deQueue();
print(biTree);
if (biTree.getLchild() != null) {
enQueue(biTree.getLchild());
}
if (biTree.getRchild() != null) {
enQueue(biTree.getRchild());
}
}
}
}