赞
踩
前序遍历(DLR,lchild,data,rchild),是二叉树遍历的一种,也叫做先根遍历、先序遍历、前序周游,可记做根左右。前序遍历首先访问根结点然后遍历左子树,最后遍历右子树。
其实在遍历二叉树的时候有三次遍历, 比如前序遍历:A->B->D->D(D左子节点并返回到D)->D(D右子节点并返回到D)->B->E->E(左)->E(右)->->B->A->C->F->F(左)->F(右)->C->C(右),所以可以用栈结构,把遍历到的节点压进栈,没子节点时再出栈。也可以用递归的方式,递归的输出当前节点,然后递归的输出左子节点,最后递归的输出右子节点。直接看代码更能理解:
- package test;
- //前序遍历的递归实现与非递归实现
- import java.util.Stack;
- public class Test
- {
- public static void main(String[] args)
- {
- TreeNode[] node = new TreeNode[10];//以数组形式生成一棵完全二叉树
- for(int i = 0; i < 10; i++)
- {
- node[i] = new TreeNode(i);
- }
- for(int i = 0; i < 10; i++)
- {
- if(i*2+1 < 10)
- node[i].left = node[i*2+1];
- if(i*2+2 < 10)
- node[i].right = node[i*2+2];
- }
-
- preOrderRe(node[0]);
- }
-
- public static void preOrderRe(TreeNode biTree)
- {//递归实现
- System.out.println(biTree.value);
- TreeNode leftTree = biTree.left;
- if(leftTree != null)
- {
- preOrderRe(leftTree);
- }
- TreeNode rightTree = biTree.right;
- if(rightTree != null)
- {
- preOrderRe(rightTree);
- }
- }
-
- public static void preOrder(TreeNode biTree)
- {//非递归实现
- Stack<TreeNode> stack = new Stack<TreeNode>();
- while(biTree != null || !stack.isEmpty())
- {
- while(biTree != null)
- {
- System.out.println(biTree.value);
- stack.push(biTree);
- biTree = biTree.left;
- }
- if(!stack.isEmpty())
- {
- biTree = stack.pop();
- biTree = biTree.right;
- }
- }
- }
- }
-
- class TreeNode//节点结构
- {
- int value;
- TreeNode left;
- TreeNode right;
-
- TreeNode(int value)
- {
- this.value = value;
- }
- }
-
-
-
- import java.util.Stack;
- public class Test
- {
- public static void main(String[] args)
- {
- TreeNode[] node = new TreeNode[10];//以数组形式生成一棵完全二叉树
- for(int i = 0; i < 10; i++)
- {
- node[i] = new TreeNode(i);
- }
- for(int i = 0; i < 10; i++)
- {
- if(i*2+1 < 10)
- node[i].left = node[i*2+1];
- if(i*2+2 < 10)
- node[i].right = node[i*2+2];
- }
-
- midOrderRe(node[0]);
- System.out.println();
- midOrder(node[0]);
- }
-
- public static void midOrderRe(TreeNode biTree)
- {//中序遍历递归实现
- if(biTree == null)
- return;
- else
- {
- midOrderRe(biTree.left);
- System.out.println(biTree.value);
- midOrderRe(biTree.right);
- }
- }
-
-
- public static void midOrder(TreeNode biTree)
- {//中序遍历费递归实现
- Stack<TreeNode> stack = new Stack<TreeNode>();
- while(biTree != null || !stack.isEmpty())
- {
- while(biTree != null)
- {
- stack.push(biTree);
- biTree = biTree.left;
- }
- if(!stack.isEmpty())
- {
- biTree = stack.pop();
- System.out.println(biTree.value);
- biTree = biTree.right;
- }
- }
- }
- }
-
- class TreeNode//节点结构
- {
- int value;
- TreeNode left;
- TreeNode right;
-
- TreeNode(int value)
- {
- this.value = value;
- }
- }
-
-
-
- import java.util.Stack;
- public class Test
- {
- public static void main(String[] args)
- {
- TreeNode[] node = new TreeNode[10];//以数组形式生成一棵完全二叉树
- for(int i = 0; i < 10; i++)
- {
- node[i] = new TreeNode(i);
- }
- for(int i = 0; i < 10; i++)
- {
- if(i*2+1 < 10)
- node[i].left = node[i*2+1];
- if(i*2+2 < 10)
- node[i].right = node[i*2+2];
- }
-
- postOrderRe(node[0]);
- System.out.println("***");
- postOrder(node[0]);
- }
-
-
-
- public static void postOrderRe(TreeNode biTree)
- {//后序遍历递归实现
- if(biTree == null)
- return;
- else
- {
- postOrderRe(biTree.left);
- postOrderRe(biTree.right);
- System.out.println(biTree.value);
- }
- }
-
- public static void postOrder(TreeNode biTree)
- {//后序遍历非递归实现
- int left = 1;//在辅助栈里表示左节点
- int right = 2;//在辅助栈里表示右节点
- Stack<TreeNode> stack = new Stack<TreeNode>();
- Stack<Integer> stack2 = new Stack<Integer>();//辅助栈,用来判断子节点返回父节点时处于左节点还是右节点。
-
- while(biTree != null || !stack.empty())
- {
- while(biTree != null)
- {//将节点压入栈1,并在栈2将节点标记为左节点
- stack.push(biTree);
- stack2.push(left);
- biTree = biTree.left;
- }
-
- while(!stack.empty() && stack2.peek() == right)
- {//如果是从右子节点返回父节点,则任务完成,将两个栈的栈顶弹出
- stack2.pop();
- System.out.println(stack.pop().value);
- }
-
- if(!stack.empty() && stack2.peek() == left)
- {//如果是从左子节点返回父节点,则将标记改为右子节点
- stack2.pop();
- stack2.push(right);
- biTree = stack.peek().right;
- }
-
- }
- }
- }
-
- class TreeNode//节点结构
- {
- int value;
- TreeNode left;
- TreeNode right;
-
- TreeNode(int value)
- {
- this.value = value;
- }
- }
-
-
3.重复以上操作直到队列为空
- public static void levelOrder(TreeNode biTree)
- {//层次遍历
- if(biTree == null)
- return;
- LinkedList<TreeNode> list = new LinkedList<TreeNode>();
- list.add(biTree);
- TreeNode currentNode;
- while(!list.isEmpty())
- {
- currentNode = list.poll();
- System.out.println(currentNode.value);
- if(currentNode.left != null)
- list.add(currentNode.left);
- if(currentNode.right != null)
- list.add(currentNode.right);
- }
- }
先序遍历特点:第一个值是根节点
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。