数据结构之关于树的操作(树的递归和非递归遍历)-(四补)

本文详细介绍了如何使用Java实现树的前序、中序、后序遍历,包括递归和非递归两种方法。通过实例代码展示了层次遍历、前序遍历(根左右)、中序遍历(左根右)、后序遍历(左右根)的不同实现方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

   前面写了一些关于树的操作,但是没有实现树的遍历的非递归写法。

  通常树有四种遍历方法:1.层次遍历(需要用到树的高度,此文没有考虑)

                 2.前序遍历(根左右);3.中序遍历(左根右);4.后序遍历(左右根)

  树的结构如下:

                      

  层次遍历:123456789

  前序遍历:124895367

  中序遍历:849251637

  后序遍历:894526731

    java代码实现三种遍历的递归和和非递归实现  

    

package com.lip.datastructure.tree;

import java.util.Stack;
/**
 * @author lip
 */
public class Tree
	{
		public static void main(String[] args)
			{
               Node<Integer>root=getNode();
               System.out.println("前序遍历(非递归)");
               preOrder(root);
               System.out.println("前序遍历(递归)");
               preOrderRecursive(root);
               System.out.println();
               System.out.println("中序遍历(非递归)");
               infixOrder(root);
               System.out.println("中序遍历(递归)");
               infixOrderRecursive(root);
               System.out.println();
               System.out.println("后序遍历(非递归)");
               postOrder(root);
               System.out.println("后序遍历(递归)");
               postOrderRecursive(root);

			}
		public static Node getNode()
		{
			Node<Integer>node1=new Node(1);
			Node<Integer>node2=new Node(2);
			Node<Integer>node3=new Node(3);
			node1.left=node2;
			node1.right=node3;
			Node<Integer>node4=new Node(4);
			Node<Integer>node5=new Node(5);
			node2.left=node4;
			node2.right=node5;
			Node<Integer>node6=new Node(6);
			Node<Integer>node7=new Node(7);
			node3.left=node6;
			node3.right=node7;
			Node<Integer>node8=new Node(8);
			Node<Integer>node9=new Node(9);
			node4.left=node8;
			node4.right=node9;
			return node1;
		}
		//前序遍历,非递归
		@SuppressWarnings("rawtypes")
		public static void preOrder(Node root)
		{
			Stack<Node>stack=new Stack<Node>();
			stack.push(root);
			while(stack.size()>0)
				{
					Node tempNode=stack.pop();
					if(tempNode!=null)
						{
					      System.out.print(tempNode.data);
					      stack.push(tempNode.right);
					      stack.push(tempNode.left);
						}
				}
			System.out.println();
		}
		//前序遍历(根左右),递归
		public static void preOrderRecursive(Node root)
		{
			if(root!=null)
				{
					System.out.print(root.data);
					preOrderRecursive(root.left);
					preOrderRecursive(root.right);
				}
		}
		//中序遍历(左根右),非递归
		public static void infixOrder(Node root)
		{
			Stack<Node>stack=new Stack<Node>();
			stack.push(root);
			while(stack.size()>0)
				{
					Node temp=stack.pop();
					if(temp!=null)
						{
							if((temp.left==null)&&(temp.right==null))
								System.out.print(temp.data);
							else
								{
									stack.push(temp.right);
									stack.push(new Node(temp.data));
									stack.push(temp.left);
								}
						}
				}
			System.out.println();
		}
		//中序遍历(左根右),递归
		public static void infixOrderRecursive(Node root)
		{
			if(root!=null)
				{
					infixOrderRecursive(root.left);
					System.out.print(root.data);
					infixOrderRecursive(root.right);
				}
		}
		//后序遍历(左右根),非递归
		public static void postOrder(Node root)
		{
			Stack<Node>stack=new Stack<Node>();
			stack.push(root);
			Node temp;
			while(stack.size()>0)
				{
					temp=stack.pop();
					if(temp!=null)
						{
							if(temp.left==null&&temp.right==null)
								System.out.print(temp.data);
							else {
								stack.push(new Node(temp.data));
								stack.push(temp.right);
								stack.push(temp.left);
							}
						}
				}
			System.out.println();
		}
		//后序遍历(左右根),递归
		public static void postOrderRecursive(Node root)
		{
			if(root!=null)
				{
					postOrderRecursive(root.left);
					postOrderRecursive(root.right);
					System.out.print(root.data);
				}
		}


	}
   class Node <T>
   {
	   public Node left;
	   public Node right;
	   public T data;
	   public Node(T data)
	   {
		   this.data=data;
	   }
   }

    其实递归和非递归的区别也就是非递归使用stack来回溯(也可以看做是一种特殊的递归)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值