Java实现二叉树的多种遍历 - 执念saying
Java实现二叉树的多种遍历
简述:
用Java实现二叉树的前序,中序,后序,层序遍历, S型层序遍历
算法简述:
前三个算法在于输出当前节点的位置,
1 前序: 在递归左右儿子之前,输出当前节点
[java] view plain copy
- void PreOrderPrint(){
- System.out.print(value.toString() + " ");
- if(left != null)
- left.PreOrderPrint();
- if(right != null)
- right.PreOrderPrint();
- }
2 中序:在递归左右儿子中间,输出
[java] view plain copy
- void InOrderPrint(){
- if(left != null)
- left.InOrderPrint();
- System.out.print(value.toString() + " ");
- if(right != null)
- right.InOrderPrint();
- }
后序:在递归左右儿子之后输出
[java] view plain copy
- void PostOrderPrint(){
- if(left != null)
- left.PostOrderPrint();
- if(right != null)
- right.PostOrderPrint();
- System.out.print(value.toString() + " ");
- }
3 层序遍历,这里的实现方式类似于两个簸箕(queue1 和 queue2)之间互相倒,知道谁都没有后继节点位置,即两个簸箕都为空,此处是两个队列都为空
[java] view plain copy
- void LevelOrderPrint(){
- if(this == null)
- throw new IllegalArgumentException("null node !");
- Queue<Node<E>> queue1 = new LinkedList<Node<E>>();
- Queue<Node<E>> queue2 = new LinkedList<Node<E>>();
- queue1.add(this);
- while(!queue1.isEmpty() || !queue2.isEmpty()){
- if(queue2.isEmpty()){
- while(!queue1.isEmpty()){
- Node<E> currentNode = queue1.poll();
- System.out.print(currentNode.value.toString() + " ");
- if(currentNode.left != null){
- queue2.add(currentNode.left);
- }
- if(currentNode.right != null){
- queue2.add(currentNode.right);
- }
- }
- }
- else{
- while(!queue2.isEmpty()){
- Node<E> currentNode = queue2.poll();
- System.out.print(currentNode.value.toString() + " ");
- if(currentNode.left != null){
- queue1.add(currentNode.left);
- }
- if(currentNode.right != null){
- queue1.add(currentNode.right);
- }
- }
- }
- System.out.println();
- }
- }
4 S型层序遍历,就是把上面使用的queue换为stack,注意左右子节点添加顺序,就可以了
[java] view plain copy
- //Print By Level S-style
- public void S_LevelOrderPrint(){
- Stack<Node<E>> stack1 = new Stack<Node<E>>();
- Stack<Node<E>> stack2 = new Stack<Node<E>>();
- stack1.add(this);
- while(!stack1.isEmpty() || !stack2.isEmpty()){
- if(stack1.isEmpty()){
- while(!stack2.isEmpty()){
- Node<E> currentNode = stack2.pop();
- System.out.print(currentNode.value + " ");
- if(currentNode.left != null)
- stack1.push(currentNode.left);
- if(currentNode.right != null)
- stack1.push(currentNode.right);
- }
- }else{
- while(!stack1.isEmpty()){
- Node<E> currentNode = stack1.pop();
- System.out.print(currentNode.value + " ");
- if(currentNode.right != null)
- stack2.add(currentNode.right);
- if(currentNode.left != null)
- stack2.add(currentNode.left);
- }
- }
- System.out.println();
- }
- }
下面是主要代码,包括测试代码
代码:
[java] view plain copy
- package offer;
- import java.util.LinkedList;
- import java.util.Queue;
- import java.util.Stack;
- class Node<E extends Comparable<E>>{
- Node<E> left;
- Node<E> right;
- E value;
- Node(){
- left = null;
- right = null;
- value = null;
- }
- Node(E value){
- this.value = value;
- left = null;
- right = null;
- }
- void PreOrderPrint(){
- System.out.print(value.toString() + " ");
- if(left != null)
- left.PreOrderPrint();
- if(right != null)
- right.PreOrderPrint();
- }
- void InOrderPrint(){
- if(left != null)
- left.InOrderPrint();
- System.out.print(value.toString() + " ");
- if(right != null)
- right.InOrderPrint();
- }
- void PostOrderPrint(){
- if(left != null)
- left.PostOrderPrint();
- if(right != null)
- right.PostOrderPrint();
- System.out.print(value.toString() + " ");
- }
- //Print By Level
- void LevelOrderPrint(){
- if(this == null)
- throw new IllegalArgumentException("null node !");
- Queue<Node<E>> queue1 = new LinkedList<Node<E>>();
- Queue<Node<E>> queue2 = new LinkedList<Node<E>>();
- queue1.add(this);
- while(!queue1.isEmpty() || !queue2.isEmpty()){
- if(queue2.isEmpty()){
- while(!queue1.isEmpty()){
- Node<E> currentNode = queue1.poll();
- System.out.print(currentNode.value.toString() + " ");
- if(currentNode.left != null){
- queue2.add(currentNode.left);
- }
- if(currentNode.right != null){
- queue2.add(currentNode.right);
- }
- }
- }
- else{
- while(!queue2.isEmpty()){
- Node<E> currentNode = queue2.poll();
- System.out.print(currentNode.value.toString() + " ");
- if(currentNode.left != null){
- queue1.add(currentNode.left);
- }
- if(currentNode.right != null){
- queue1.add(currentNode.right);
- }
- }
- }
- System.out.println();
- }
- }
- //Print By Level S-style
- public void S_LevelOrderPrint(){
- Stack<Node<E>> stack1 = new Stack<Node<E>>();
- Stack<Node<E>> stack2 = new Stack<Node<E>>();
- stack1.add(this);
- while(!stack1.isEmpty() || !stack2.isEmpty()){
- if(stack1.isEmpty()){
- while(!stack2.isEmpty()){
- Node<E> currentNode = stack2.pop();
- System.out.print(currentNode.value + " ");
- if(currentNode.left != null)
- stack1.push(currentNode.left);
- if(currentNode.right != null)
- stack1.push(currentNode.right);
- }
- }else{
- while(!stack1.isEmpty()){
- Node<E> currentNode = stack1.pop();
- System.out.print(currentNode.value + " ");
- if(currentNode.right != null)
- stack2.add(currentNode.right);
- if(currentNode.left != null)
- stack2.add(currentNode.left);
- }
- }
- System.out.println();
- }
- }
- }
- class BinarySearchTree<E extends Comparable<E>>{
- private Node<E> root;
- public Node<E> getRoot(){
- return root;
- }
- BinarySearchTree(){
- root = null;
- }
- public void InsertNode(E value){
- if(root == null){
- root = new Node<E>(value);
- return;
- }
- Node<E> currentNode = root;
- while(true){
- if(value.compareTo(currentNode.value) > 0){
- if(currentNode.right == null){
- currentNode.right = new Node<E>(value);
- break;
- }
- currentNode = currentNode.right;
- }else{
- if(currentNode.left == null){
- currentNode.left = new Node<E>(value);
- break;
- }
- currentNode = currentNode.left;
- }
- }
- }
- }
- public class LevelPrintOfBinaryTree<E extends Comparable<E>> {
- public static void main(String[] args) {
- BinarySearchTree<Integer> tree = new BinarySearchTree<Integer>();
- {
- tree.InsertNode(4);
- tree.InsertNode(2);
- tree.InsertNode(1);
- tree.InsertNode(3);
- tree.InsertNode(6);
- tree.InsertNode(5);
- tree.InsertNode(7);
- tree.InsertNode(8);
- }
- System.out.print("PreOrderPrint: ");
- tree.getRoot().PreOrderPrint();
- System.out.print("\nInOrderPrint: ");
- tree.getRoot().InOrderPrint();
- System.out.print("\nPostOrderPrint: ");
- tree.getRoot().PostOrderPrint();
- System.out.println("\nLevelOrderPrint: ");
- tree.getRoot().LevelOrderPrint();
- System.out.println("\nS_LevelOrderPrint: ");
- tree.getRoot().S_LevelOrderPrint();
- }
- }
输入测试的树:
输出:
来源: http://blog.csdn.net/anialy/article/details/8145114
以上是 Java实现二叉树的多种遍历 - 执念saying 的全部内容, 来源链接: utcz.com/z/391109.html