二叉树的创建及前中后层遍历
package com.data.tree; /** * ClassName TreeTest Description TODO * @author Payphone * @date 2018年12月24日 下午1:44:07 * @version V1.0 */ public class TreeTest { public static int maxsize = 100; public static void main(String[] args) { String[] arr = { "A", "B", "C", "D", "E", "#", "#", "#", "#", "#", "#" }; TreeTest ts = new TreeTest(); BiTree tree = ts.createTree(new BiTree(), arr); ts.levelOrder(tree); System.out.println(tree.toString()); }
package com.data.tree; public class BiTree { String data;//数据域 BiTree left,right;//下一个结点 int leval;//层数 }
/** * @Title createTree * @Description 层序建二叉树 * @param bt * @param node * @return BiTree */ BiTree createTree(BiTree bt, String[] node) { int rear = -1, front = -1; BiTree[] queue = new BiTree[maxsize];// 定义队列 int flag = 0;// 数组下标 bt = new BiTree(); bt.data = node[flag++];// 给根节点赋值 queue[++rear] = bt;// 根节点入队 // 数组未读取完毕且队列非空时 while (flag < node.length && !(rear == front)) { BiTree b = queue[++front]; // 给出队的节点 创建左子树 if (node[flag] == "#") { b.left = null; flag++; } else { b.left = new BiTree(); b.left.data = node[flag++]; // 左子树非空则入队 queue[++rear] = b.left; } // end if // 给出队的节点创建右子树 if (node[flag] == "#") { b.right = null; flag++; } else { b.right = new BiTree(); b.right.data = node[flag++]; // 右子树非空入队 queue[++rear] = b.right; } // end if } return bt; } /** * @Title levelOrder * @Description * 二叉树的层序遍历 * 节点出队打印 左右子树入队 * @param tree * @return void */ public void levelOrder(BiTree tree) { int rear = -1,front = -1; BiTree[] queue = new BiTree[maxsize]; queue[++rear] = tree; while(rear!=front) { BiTree bt = queue[++front]; System.out.println(bt.data); if(bt.left!=null) { queue[++rear] = bt.left; } if(bt.right!=null) { queue[++rear] = bt.right; } } } //先序遍历 public void PreNode(BiTree tree) { if (tree != null) { System.out.println(tree.data); PreNode(tree.left); PreNode(tree.right); } } /* * 先序遍历思想: 先把左子树一直入栈 左子树到头了在从栈中弹出元素 看弹出的元素是否有右节点 有就继续按先序来 */ public void PreNodeUn(BiTree tree) { int top = -1; BiTree[] stack = new BiTree[maxsize]; while (top != -1 || tree != null) { if (tree != null) { System.out.println(tree.data); stack[++top] = tree; tree = tree.left; } else { tree = stack[top--]; tree = tree.right; } } } public void PreNodeUn1(BiTree tree) { int top = -1; BiTree[] stack = new BiTree[maxsize]; while (top != -1 || tree != null) { while (tree != null) { System.out.println(tree.data); stack[++top] = tree; tree = tree.left; } if (tree == null) { tree = stack[top--]; tree = tree.right; } } } // 中序遍历 public void InOrder(BiTree tree) { InOrder(tree.left); System.out.println(tree.data); InOrder(tree.right); } // 中序遍历 public void InOrderUn(BiTree tree) { int top = -1; BiTree[] stack = new BiTree[maxsize];// 定义栈 while (top != -1 || tree != null) { while (tree != null) { stack[++top] = tree; tree = tree.left; } if (tree == null) { tree = stack[top--]; System.out.println(tree.data); tree = tree.right; } } } // 后序遍历 void PostOrder(BiTree tree) { PostOrder(tree.left); PostOrder(tree.right); System.out.println(tree.data); } // 后序非递归 void PostOrderUn(BiTree tree) { BiTree flag = null; int top = -1; BiTree[] stack = new BiTree[maxsize]; while (top != -1 || tree != null) { while (tree != null) {// 左子树一直入栈 stack[++top] = tree; tree = tree.left; } // 左子树访问到头了 if (tree == null) { tree = stack[top];// 获取栈顶 看其是否有右子树 if (tree.right != null && tree.right != flag) {// 有右子树且未被访问过 tree = tree.right;// 以右子树为根继续进行其左子树的入栈操作 } else {// 左右都已经访问完毕了。[第一次访问的一定是叶子节点,它没有左右子树] System.out.println(tree.data); top--;// 元素出栈 flag = tree;// flag表示已经访问了。 tree = null;// 让下一个元素出栈 } } } } }