Java二叉树的遍历方式有几种?

本文详细介绍了二叉树的三种基本遍历方式——前序、中序和后序,通过代码示例展示了如何在Java中实现这些遍历,并提供了API设计。

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

二叉树的基础遍历

1:前序遍历:先访问根结点,然后再访问左子树,最后访问右子树;

2:中序遍历:先访问左子树,然后再访问根结点,最后访问右子树;

3:后序遍历:先访问左子树,然后再访问右子树,最后访问根结点;

示例如下:

前序遍历的API设计:

<pre class="prettyprint hljs vim" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">public Queue<Key> preErgodic():使用前序遍历,获取整个树中的所有键
private void preErgodic(Node x,Queue<Key> keys):使用前序遍历,把指定树x中的所有键放入到keys队列中</pre>

实现步骤:

  1. 把当前结点的key放入到队列中;
  2. 找到当前结点的左子树,如果不为空,递归遍历左子树;
  3. 找到当前结点的右子树,如果不为空,递归遍历右子树;

代码实现:

<pre class="prettyprint hljs java" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">//使用前序遍历获取整个树中所有的键
    public  Queue<Key>  preErgoidic(){ 
        Queue<Key> keys = new Queue<>();
        preErgoidic(root,keys);
        return keys;
    }

    //使用前序遍历获取指定树x的所有键,并放到Keys队列中
    private  void  preErgoidic(Node x,  Queue<Key> keys){ 
        if(x == null){ 
            return;
        }
        //把x结点的key放入到keys中
        keys.enQueue(x.key);

        //递归遍历x结点的左子树
        if(x.left != null){ 
            preErgoidic(x.left,keys);
        }

        //递归遍历x结点的右子树
        if(x.right != null){ 
            preErgoidic(x.right,keys);
        }

    }</pre>

中序遍历的API设计:

<pre class="prettyprint hljs fsharp" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">public Queue midErgodic():使用中序遍历,获取整个树中的所有键
private void midErgodic(Node x,Queue keys):使用中序遍历,把指定树x中的所有键放入到keys队列中</pre>

实现步骤:

  1. 找到当前结点的左子树,如果不为空,递归遍历左子树;
  2. 把当前结点的key放入到队列中;
  3. 找到当前结点的右子树,如果不为空,递归遍历右子树;

代码实现:

<pre class="prettyprint hljs java" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">//使用中序遍历获取整个树中所有的键
    public  Queue<Key>  midErgoidic(){ 
        Queue<Key> keys = new Queue<>();
        midErgoidic(root,keys);
        return keys;
    }

    //使用中序遍历获取指定树x的所有键,并放到Keys队列中
    private  void  midErgoidic(Node x,  Queue<Key> keys)  { 
        if(x == null){ 
            return;
        }

        //递归遍历x结点的左子树
        if(x.left != null){ 
            midErgoidic(x.left,keys);
        }

        //把x结点的key放入到keys中
        keys.enQueue(x.key);

        //递归遍历x结点的右子树
        if(x.right != null){ 
            midErgoidic(x.right,keys);
        }
    }</pre>

后序遍历的API设计:

<pre class="prettyprint hljs fsharp" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">public Queue afterErgodic():使用中序遍历,获取整个树中的所有键
private void afterErgodic(Node x,Queue keys):使用中序遍历,把指定树x中的所有键放入到keys队列中</pre>

实现步骤:

  1. 找到当前结点的左子树,如果不为空,递归遍历左子树;
  2. 找到当前结点的右子树,如果不为空,递归遍历右子树;
  3. 把当前结点的key放入到队列中;

代码实现:

<pre class="prettyprint hljs java" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">//后续遍历
    public  Queue<Key>  afterErgoidic(){ 
        Queue<Key> keys = new Queue<>();
        afterErgoidic(root,keys);
        return keys;
    }

    //使用中序遍历获取指定树x的所有键,并放到Keys队列中
    private  void  afterErgoidic(Node x,  Queue<Key> keys)  { 
        if(x == null){ 
            return;
        }

        //递归遍历x结点的左子树
        if(x.left != null){ 
            afterErgoidic(x.left,keys);
        }

        //递归遍历x结点的右子树
        if(x.right != null){ 
            afterErgoidic(x.right,keys);
        }

        //把x结点的key放入到keys中
        keys.enQueue(x.key);
    }</pre>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值