zoukankan      html  css  js  c++  java
  • 二叉树遍历(层序,前中后非递归)

    1 using System;
    2  using System.Collections.Generic;
    3 //using System.Linq;
    4 //using System.Text;
    5
    6 namespace BinaryTree
    7 {
    8 /// <summary>
    9 /// BinaryTree Node
    10 /// </summary>
    11 class BTreeNode
    12 {
    13 public int Data;
    14 public BTreeNode Lchild;
    15 public BTreeNode Rchild;
    16
    17 public BTreeNode()
    18 {
    19 Data = 0;
    20 Lchild = null;
    21 Rchild = null;
    22 }
    23
    24 public void Visit()
    25 {
    26 Console.Write(Data+" ");
    27 }
    28 }
    29 /// <summary>
    30 /// Binary Tree
    31 /// </summary>
    32 class BinaryTree
    33 {
    34 private BTreeNode _root;
    35 public BTreeNode Root
    36 {
    37 get
    38 {
    39 return _root;
    40 }
    41 set
    42 {
    43 _root = value;
    44 }
    45 }
    46
    47 public BinaryTree()
    48 {
    49 _root = null;
    50 }
    51
    52 /// <summary>
    53 /// LevelOrder, Queue
    54 /// </summary>
    55 /// <param name="t"></param>
    56 public void LevelOrder(BTreeNode t)
    57 {
    58 Queue<BTreeNode> queue=new Queue<BTreeNode>();
    59
    60 if(t != null)
    61 {
    62 queue.Enqueue(t);
    63 while (queue.Count != 0)
    64 {
    65 t=queue.Dequeue();
    66 t.Visit();
    67 if(t.Lchild!=null)
    68 queue.Enqueue(t.Lchild);
    69 if(t.Rchild!=null)
    70 queue.Enqueue(t.Rchild);
    71 }
    72 }
    73
    74 }
    75 /// <summary>
    76 /// PreOrder,InOrder Not Recursive Version, Stack
    77 /// </summary>
    78 /// <param name="t"></param>
    79 public void PreOrderNotRecursive(BTreeNode t)
    80 {
    81 Stack<BTreeNode> stack=new Stack<BTreeNode>();
    82
    83 while (t!=null||stack.Count!=0)
    84 {
    85 while (t != null)
    86 {
    87 t.Visit();
    88 stack.Push(t);
    89 t = t.Lchild;
    90 }
    91 if(stack.Count!=0)
    92 {
    93 t=stack.Pop().Rchild;
    94 }
    95 }
    96 }
    97 public void InOrderNotRecursive(BTreeNode t)
    98 {
    99 Stack<BTreeNode> stack=new Stack<BTreeNode>();
    100
    101 while (t != null || stack.Count != 0)
    102 {
    103 while (t != null)
    104 {
    105 stack.Push(t);
    106 t = t.Lchild;
    107 }
    108 if (stack.Count != 0)
    109 {
    110 t = stack.Pop();
    111 t.Visit();
    112 t = t.Rchild;
    113 }
    114 }
    115 }
    116
    117 /// <summary>
    118 /// PostOrder , Not Recursive Version
    119 /// </summary>
    120
    121 public void PostOrderNotRecursive(BTreeNode t)
    122 {
    123 // stack栈存储遍历过的结点
    124 Stack<BTreeNode> stack=new Stack<BTreeNode>();
    125 // stackint栈存储0和1,判断将要遍历 Lchild(0表示) 还是Rchild(1表示)
    126 Stack<int> stackint=new Stack<int>();
    127
    128 do
    129 {
    130 while (t != null) //遍历左子树
    131 {
    132 stack.Push(t);
    133 stackint.Push(0); //标记为左子树
    134 t = t.Lchild;
    135 }
    136
    137 while (stack.Count!=0 && stackint.Peek()==1)
    138 {
    139 t = stack.Pop();
    140 stackint.Pop();
    141 t.Visit(); //tag为1,表示右子树访问完毕,故访问根结点
    142 }
    143
    144 if (stack.Count!=0)
    145 { //遍历右子树
    146 stackint.Pop();
    147 stackint.Push(1);
    148 t = stack.Peek().Rchild;
    149 }
    150 } while (stack.Count!=0);
    151
    152 }
    153
    154 }//class BinaryTree
    155
    156 }

  • 相关阅读:
    TTreeView.OnCustomDrawItem
    xe Style
    delphi 加密 XOR
    ReportMachine 自定义代码 画细线
    XE 安装后C盘占用太大,C盘空间清理
    FireFox 书签 缓存 路径设置
    Android 照相
    Android手机与服务器(案例一) webservice
    win10/win7 笔记本 开启虚拟无线 批处理
    Delphi XE6打电话
  • 原文地址:https://www.cnblogs.com/zhaos/p/1945905.html
Copyright © 2011-2022 走看看