using System;
using System.Collections.Generic;
using System.Collections;
/// <summary>
/// Abstract node
/// </summary>
/// <typeparam name="T"></typeparam>
abstract class TreeNode<T> where T : TreeNode<T>
{
public T LeftNode { get; set; }
public T RightNode { get; set; }
}
/// <summary>
/// Tree class
/// </summary>
/// <typeparam name="T"></typeparam>
class Tree<T> where T : TreeNode<T>
{
/// <summary>
/// Root node
/// </summary>
public T RootNode { get; set; }
public IEnumerator GetEnumerator()
{
//Reserve nodes which have right node.
Stack<T> stack = new Stack<T>();
//Current node instance
T currentNode = RootNode;
//While current node was set as the lat node's right node, exit the cycle
while (currentNode != null)
{
//Output current node
yield return currentNode;
//If current node has right node, add it to stack
if (currentNode.RightNode != null)
{
stack.Push(currentNode);
}
//If current node has left node, set rge current node as the left node
if (currentNode.LeftNode != null)
{
currentNode = currentNode.LeftNode;
}
else
{
//If current node doesn't has left node, go to stack to get the first right node, and go on the cycle
if (stack.Count != 0)
{
currentNode = (T)stack.Pop().RightNode;
}
else
{
break;
}
}
}
}
}
分享到:
相关推荐
C语言实现二叉树的前序遍历(非递归),下载下来看看哦!
链式二叉树的前序创建、递归遍历、利用堆栈的非递归遍历、前序销毁以及求二叉树的深度
该程序代码实现了二叉树的递归生成创建,递归前序遍历,递归中序遍历,递归后序遍历,非递归前序遍历,非递归中序遍历,非递归后序遍历,以及递归层次遍历,递归求度为0,1,2的节点数,非递归求度为0,1,2的节点数。...
二叉树深度 二叉树前序遍历 递归实现 二种非递归实现 二叉树中序遍历: 递归实现 非递归实现 二叉树后序遍历: 递归实现 非递归实现 二叉树层次遍历 二叉树层次创建,创建方法遵循卡特兰数 ...
非递归先序遍历二叉树: 非递归中序遍历二叉树: 非递归后序遍历二叉树: 非递归中序遍历二叉树(算法2): 层次遍历二叉树: 递归计算单分支结点: 递归计算双分支结点: 递归计算叶子数: 二叉数的深度:...
C语言二叉树遍历前序非递归算法,简单易懂,正确无误
二叉树的递归遍历、非递归遍历和层次遍历
关于二叉树前序和后序的非递归遍历算法.rar
二叉树先序、中序、后序遍历非递归算法,简述了二叉树的基本算法。
二叉树已知后序和中序遍历求前序遍历,C++编写已通过编译
二叉树的基本操作,包括前序、中序、后序遍历的递归和非递归算法,不得不下的资源
包含一下方法: 1.通过一个数组来构造一颗...6.使用非递归 先序遍历一棵二叉树 7.使用非递归 中序遍历一棵二叉树 8.使用非递归 后序遍历一棵二叉树 PS:代码为C++代码 可以直接下载使用!!! PS2:每句代码都有详细注释
链式二叉树的后序创建、递归后序遍历、非递归堆栈后序遍历、后序销毁
按前序遍历创建二叉树。输入一字符串序列,空格表示子树为空,然后自动创建二叉树;前序遍历二叉,中序遍历二叉树。
数据结构的代码实现,非递归算法,。
二叉树的遍历,前序遍历 中序遍历 后序遍历
二叉树的非递归中序遍历 C代码C代码C代码C代码C代码C代码C代码C代码C代码C代码C代码C代码C代码C代码C代码C代码
二叉树的建立以及二叉树的层次遍历、前序遍历、中序遍历、后序遍历
二叉树的非递归遍历,使用C++实现二叉树的非递归遍历,对正在学习算法的同学应该挺有帮助的
因此,满二叉树的前序遍历、中序遍历和后序遍历均可以通过递归方式实现。 以前序遍历为例,满二叉树的前序遍历顺序为:根、左、右。 在具体实现过程中,我们可以通过构造一个满二叉树,然后进行前序遍历来获取遍历...