c - 不使用递归的二叉树遍历

标签 c data-structures tree binary-tree

任何人都可以帮助创建一个二叉树并在 c 中对二叉树进行非递归前序遍历吗?

最佳答案

您可以使用 threaded binary tree 的变体;使用叶节点的 right 链接指向遍历中的下一个节点,类似于

                    +---+
                    | A |
                    +---+
                   /     \
              +---+       +---+
              | B |       | E |
              +---+       +---+
             /     \      ^
        +---+       +---+ |
        | C |       | D | |
        +---+       +---+ |
            |       ^   | |
            +-------+   +-+

叶节点C显式指向D,即前序遍历的下一个节点,D显式指向E。这使得插入和删除操作更加痛苦,但它为您提供了一种简单的前序遍历,无需递归且无需辅助堆栈。

关于c - 不使用递归的二叉树遍历,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29264905/

相关文章:

c - D3D11CreateDeviceAndSwapChain 软件参数

algorithm - 如何根据二叉搜索树(BST)中的后继者计算 parent

java - 为什么Java标准库中没有栈集合类型的接口(interface)?

data-structures - 什么是树中根节点的级别?

c - Printf问题与Kernighan和Ritchie问题1-17

c - 通过信号中断两个阻塞线程

python - Python 中的合并排序 - RuntimeError : maximum recursion depth exceeded

php - 有人可以解释这 2 个引用用法在 PHP 中的区别吗?

scala - 如何使用 Scala 运行具有分类特征集的 Spark 决策树?

c++ - Windows 虚拟内存和内核模式