数据结构中的二叉树遍历2024年8月28日 | 阅读16分钟 树可以被定义为一种非线性数据结构,它以节点的形式存储数据,节点之间通过边相互连接。在所有节点中,有一个主节点称为根节点,所有其他节点都是这些节点的子节点。 在任何数据结构中,遍历都是一项重要的操作。在遍历操作中,我们走遍整个数据结构,至少访问其每个元素一次。在对数据结构执行各种其他操作时,遍历操作扮演着非常重要的角色,例如搜索操作,我们需要至少访问数据结构中的每个元素一次,以便可以将数据结构中的每个传入元素与我们想在数据结构中查找的键进行比较。因此,像任何其他数据结构一样,树数据结构也需要被遍历以访问其每个元素,也称为树数据结构的节点。 根据访问树节点的顺序和用于遍历树的数据结构类型,有不同的遍历树的方法。遍历树涉及到多种数据结构,因为遍历树需要以某种方式迭代所有节点。 由于从一个给定节点出发,可能有不止一种方式来遍历或访问树的下一个节点,因此存储其中一个节点以便进一步遍历,并存储其余具有可能路径的节点以便在需要时回溯树,就变得很重要。回溯不是一种线性方法,所以我们需要不同的数据结构来遍历整棵树。栈和队列是用于遍历树的主要数据结构。 遍历是一种访问树的所有节点并打印其值的技术。遍历树涉及以某种方式迭代所有节点。我们总是从根(头)节点开始,因为所有节点都通过边(链接)连接。由于树不是线性数据结构,从一个给定节点出发,可能存在多个可能的下一个节点,所以一些节点必须被延迟处理,即以某种方式存储以便后续访问。 二叉树的遍历类型二叉树的遍历有三种类型。
中序树遍历在这种遍历策略中,首先访问左子树,然后是根节点,最后是右子树。始终要记住,任何节点本身都可以是一个子树。二叉树的中序遍历输出会按升序产生排序后的键值。 C 代码 让我们编写一个用于二叉搜索树中序遍历的基本 C 语言程序。 输出 上述 C 代码产生以下输出。 Select one of the operations:: 1. To insert a new node in the Binary Tree 2. To display the nodes of the Binary Tree (via Inorder Traversal). 1 Enter the value to be inserted 12 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Binary Tree 2. To display the nodes of the Binary Tree(via Inorder Traversal). 1 Enter the value to be inserted 98 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Binary Tree 2. To display the nodes of the Binary Tree(via Inorder Traversal). 1 Enter the value to be inserted 23 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Binary Tree 2. To display the nodes of the Binary Tree(via Inorder Traversal). 1 Enter the value to be inserted 78 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Binary Tree 2. To display the nodes of the Binary Tree(via Inorder Traversal). 1 Enter the value to be inserted 45 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Binary Tree 2. To display the nodes of the Binary Tree(via Inorder Traversal). 1 Enter the value to be inserted 87 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Binary Tree 2. To display the nodes of the Binary Tree(via Inorder Traversal). 2 Inorder Traversal of the Binary Tree:: 12 23 45 78 87 98 Do you want to continue (Type y or n) n 前序树遍历在这种遍历方法中,首先访问根节点,然后是左子树,最后是右子树。 代码 让我们为二叉搜索树的前序遍历编写一段 C 代码。 输出 Select one of the operations:: 1. To insert a new node in the Binary Tree 2. To display the nodes of the Binary Tree(via Preorder Traversal). 1 Enter the value to be inserted 45 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Binary Tree 2. To display the nodes of the Binary Tree(via Preorder Traversal). 1 Enter the value to be inserted 53 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Binary Tree 2. To display the nodes of the Binary Tree(via Preorder Traversal). 1 Enter the value to be inserted 1 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Binary Tree 2. To display the nodes of the Binary Tree(via Preorder Traversal). 1 Enter the value to be inserted 2 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Binary Tree 2. To display the nodes of the Binary Tree(via Preorder Traversal). 1 Enter the value to be inserted 97 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Binary Tree 2. To display the nodes of the Binary Tree(via Preorder Traversal). 1 Enter the value to be inserted 22 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Binary Tree 2. To display the nodes of the Binary Tree(via Preorder Traversal). 2 Preorder Traversal of the Binary Tree:: 45 1 2 22 53 97 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Binary Tree 2. To display the nodes of the Binary Tree(via Preorder Traversal). 1 Enter the value to be inserted 76 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Binary Tree 2. To display the nodes of the Binary Tree(via Preorder Traversal). 1 Enter the value to be inserted 30 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Binary Tree 2. To display the nodes of the Binary Tree(via Preorder Traversal). 1 Enter the value to be inserted 67 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Binary Tree 2. To display the nodes of the Binary Tree(via Preorder Traversal). 1 Enter the value to be inserted 4 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Binary Tree 2. To display the nodes of the Binary Tree(via Preorder Traversal). 2 Preorder Traversal of the Binary Tree:: 45 1 2 22 4 30 53 97 76 67 Do you want to continue (Type y or n) n 后序树遍历在这种遍历方法中,根节点是最后被访问的,因此得名。我们首先遍历左子树,然后是右子树,最后是根节点。 代码 让我们编写一个用于二叉搜索树后序遍历的程序。 输出 Select one of the operations:: 1. To insert a new node in the Binary Tree 2. To display the nodes of the Binary Tree(via Postorder Traversal). 1 Enter the value to be inserted 12 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Binary Tree 2. To display the nodes of the Binary Tree(via Postorder Traversal). 1 Enter the value to be inserted 31 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Binary Tree 2. To display the nodes of the Binary Tree(via Postorder Traversal). 24 Wrong Entry Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Binary Tree 2. To display the nodes of the Binary Tree(via Postorder Traversal). 1 Enter the value to be inserted 24 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Binary Tree 2. To display the nodes of the Binary Tree(via Postorder Traversal). 1 Enter the value to be inserted 88 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Binary Tree 2. To display the nodes of the Binary Tree(via Postorder Traversal). 1 Enter the value to be inserted 67 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Binary Tree 2. To display the nodes of the Binary Tree(via Postorder Traversal). 1 Enter the value to be inserted 56 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Binary Tree 2. To display the nodes of the Binary Tree(via Postorder Traversal). 1 Enter the value to be inserted 90 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Binary Tree 2. To display the nodes of the Binary Tree(via Postorder Traversal). 1 Enter the value to be inserted 44 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Binary Tree 2. To display the nodes of the Binary Tree(via Postorder Traversal). 1 Enter the value to be inserted 71 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Binary Tree 2. To display the nodes of the Binary Tree(via Postorder Traversal). 1 Enter the value to be inserted 38 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Binary Tree 2. To display the nodes of the Binary Tree(via Postorder Traversal). 1 Enter the value to be inserted 29 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Binary Tree 2. To display the nodes of the Binary Tree(via Postorder Traversal). 2 Postorder Traversal of the Binary Tree:: 29 24 38 44 56 71 67 90 88 31 12 Do you want to continue (Type y or n) n 我们已经看到了实现二叉树节点中序、前序和后序遍历的不同 C 语言程序。现在让我们编写一个程序,在单个程序中执行所有三种类型的遍历。 代码 输出 Select one of the operations:: 1. To insert a new node in the Binary Tree 2. To display the nodes of the Binary Tree(via Preorder Traversal). 3. To display the nodes of the Binary Tree(via Inorder Traversal). 4. To display the nodes of the Binary Tree(via Postorder Traversal). 1 Enter the value to be inserted 2 Leaf node created. Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Binary Tree 2. To display the nodes of the Binary Tree(via Preorder Traversal). 3. To display the nodes of the Binary Tree(via Inorder Traversal). 4. To display the nodes of the Binary Tree(via Postorder Traversal). 1 Enter the value to be inserted 5 Directed to right link. Leaf node created. Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Binary Tree 2. To display the nodes of the Binary Tree(via Preorder Traversal). 3. To display the nodes of the Binary Tree(via Inorder Traversal). 4. To display the nodes of the Binary Tree(via Postorder Traversal). 1 Enter the value to be inserted 7 Directed to right link. Directed to right link. Leaf node created. Do you want to continue (Type y or n) Y Select one of the operations:: 1. To insert a new node in the Binary Tree 2. To display the nodes of the Binary Tree(via Preorder Traversal). 3. To display the nodes of the Binary Tree(via Inorder Traversal). 4. To display the nodes of the Binary Tree(via Postorder Traversal). 1 Enter the value to be inserted 9 Directed to right link. Directed to right link. Directed to right link. Leaf node created. Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Binary Tree 2. To display the nodes of the Binary Tree(via Preorder Traversal). 3. To display the nodes of the Binary Tree(via Inorder Traversal). 4. To display the nodes of the Binary Tree(via Postorder Traversal). 1 Enter the value to be inserted 31 Directed to right link. Directed to right link. Directed to right link. Directed to right link. Leaf node created. Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Binary Tree 2. To display the nodes of the Binary Tree(via Preorder Traversal). 3. To display the nodes of the Binary Tree(via Inorder Traversal). 4. To display the nodes of the Binary Tree(via Postorder Traversal). 1 Enter the value to be inserted 78 Directed to right link. Directed to right link. Directed to right link. Directed to right link. Directed to right link. Leaf node created. Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Binary Tree 2. To display the nodes of the Binary Tree(via Preorder Traversal). 3. To display the nodes of the Binary Tree(via Inorder Traversal). 4. To display the nodes of the Binary Tree(via Postorder Traversal). 2 Preorder Traversal of the Binary Tree:: 2 5 7 9 31 78 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Binary Tree 2. To display the nodes of the Binary Tree(via Preorder Traversal). 3. To display the nodes of the Binary Tree(via Inorder Traversal). 4. To display the nodes of the Binary Tree(via Postorder Traversal). 3 Inorder Traversal of the Binary Tree:: 2 5 7 9 31 78 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Binary Tree 2. To display the nodes of the Binary Tree(via Preorder Traversal). 3. To display the nodes of the Binary Tree(via Inorder Traversal). 4. To display the nodes of the Binary Tree(via Postorder Traversal). 4 Postorder Traversal of the Binary Tree:: 78 31 9 7 5 2 Do you want to continue (Type y or n) n 结论因此,在本文中,我们理解了二叉树遍历以及不同类型的二叉树遍历,如中序二叉树遍历、前序二叉树遍历和后序二叉树遍历。我们还对所有这些类型的树遍历进行了编程实现。在文章的最后一部分,我们还理解了如何实现一个基本的 C 语言程序,该程序可用于在单个文件中实现所有三种类型的树遍历。 下一个主题数据结构中的队列操作 |
我们请求您订阅我们的新闻通讯以获取最新更新。