非递归遍历

class Program
{
    static void Main(string[] args)
    {

    }

        //TreeNode node1 = new TreeNode();
        //node1.Name = "a";
        //IList<TreeNode> list = new List<TreeNode>();
        //for (int i = 0; i < 3; i++)
        //{
        //    TreeNode node = new TreeNode() { Name = "TreeNode" + i };
        //    list.Add(node);
        //    node.List = new List<TreeNode>();
        //    for (int j = 0; j < 3; j++)
        //    {
        //        TreeNode node2 = new TreeNode() { Name = "TreeNode" + i + "//" + j };
        //        node.List.Add(node2);
        //        node2.List = new List<TreeNode>();
        //        node2.List.Add(new TreeNode(){ Name="xxx"});
        //    }
        //}
        //node1.List = list;
        //FindNode(node1);

    private static int Step = 0;
    static IList<TreeNode> list = new List<TreeNode>();
    
    public static void FindNode(TreeNode node)
    {
        IList<TreeNode> templist=null;
        TreeNode currentNode = node;
        Queue<TreeNode> queue = new Queue<TreeNode>();
        switch (Step)
        {
            case 0:
                list.Add(currentNode);
                templist = currentNode.List;
                goto case 1;
            case 1:
                if (templist != null)
                {
                    foreach (var item in templist)
                    {
                        queue.Enqueue(item);
                        Console.WriteLine(item.Name);
                    }
                }
                goto case 2;
            case 2:
                if (queue.Count > 0)
                {
                    currentNode = queue.Dequeue();
                    goto case 0;
                }
                break;
        }
    }
}

public class TreeNode
{
    public string Name { get; set; }

    public IList<TreeNode> List { get; set; }
}

以免忘记
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1.先序遍历非递归算法#define maxsize 100typedef struct{ Bitree Elem[maxsize]; int top;}SqStack;void PreOrderUnrec(Bitree t){ SqStack s; StackInit(s); p=t; while (p!=null || !StackEmpty(s)) { while (p!=null) //遍历左子树 { visite(p->data); push(s,p); p=p->lchild; }//endwhile if (!StackEmpty(s)) //通过下一次循环中的内嵌while实现右子树遍历 { p=pop(s); p=p->rchild; }//endif }//endwhile }//PreOrderUnrec2.中序遍历非递归算法#define maxsize 100typedef struct{ Bitree Elem[maxsize]; int top;}SqStack;void InOrderUnrec(Bitree t){ SqStack s; StackInit(s); p=t; while (p!=null || !StackEmpty(s)) { while (p!=null) //遍历左子树 { push(s,p); p=p->lchild; }//endwhile if (!StackEmpty(s)) { p=pop(s); visite(p->data); //访问根结点 p=p->rchild; //通过下一次循环实现右子树遍历 }//endif }//endwhile}//InOrderUnrec3.后序遍历非递归算法#define maxsize 100typedef enum{L,R} tagtype;typedef struct { Bitree ptr; tagtype tag;}stacknode;typedef struct{ stacknode Elem[maxsize]; int top;}SqStack;void PostOrderUnrec(Bitree t){ SqStack s; stacknode x; StackInit(s); p=t; do { while (p!=null) //遍历左子树 { x.ptr = p; x.tag = L; //标记为左子树 push(s,x); p=p->lchild; } while (!StackEmpty(s) && s.Elem[s.top].tag==R) { x = pop(s); p = x.ptr; visite(p->data); //tag为R,表示右子树访问完毕,故访问根结点 } if (!StackEmpty(s)) { s.Elem[s.top].tag =R; //遍历右子树 p=s.Elem[s.top].ptr->rchild; } }while (!StackEmpty(s));}//PostOrderUnrec
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值