a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 編程 > C# > 正文

C#非遞歸先序遍歷二叉樹實(shí)例

2019-10-29 21:40:54
字體:
供稿:網(wǎng)友

這篇文章主要介紹了C#非遞歸先序遍歷二叉樹的實(shí)現(xiàn)方法,具有一定參考借鑒價(jià)值,需要的朋友可以參考下

本文實(shí)例講述了C#非遞歸先序遍歷二叉樹的方法。分享給大家供大家參考。具體如下:

 

 
  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.Linq; 
  4. using System.Text; 
  5. using System.Threading.Tasks; 
  6. namespace ConsoleApplication5 
  7. class Program 
  8. static void Main(string[] args) 
  9. Node treeRoot = CreateTree(); 
  10. scanTree(treeRoot); 
  11. private static void scanTree(Node treeRoot) 
  12. List<Node> list = new List<Node>(); 
  13. list.Add(treeRoot); 
  14. Node point = treeRoot; 
  15. Write(treeRoot); 
  16. while (true
  17. if (!list.Contains(point)) 
  18. //上一輪是移除的操作 
  19. if (treeRoot.leftSon == point) 
  20. {//移除的是左結(jié)點(diǎn) 
  21. if (treeRoot.rightSon != null
  22. treeRoot = treeRoot.rightSon; 
  23. list.Add(treeRoot); 
  24. Write(treeRoot); 
  25. point = treeRoot; 
  26. continue
  27. list.Remove(treeRoot); 
  28. if (list.Count == 0) 
  29. break
  30. point = treeRoot; 
  31. treeRoot = list[list.Count - 1]; 
  32. else 
  33. {//移除的是右結(jié)點(diǎn) 
  34. list.Remove(treeRoot); 
  35. if (list.Count == 0) 
  36. break
  37. point = treeRoot; 
  38. treeRoot = list[list.Count - 1]; 
  39. continue
  40. if (treeRoot.leftSon != null
  41. treeRoot = treeRoot.leftSon; 
  42. Write(treeRoot); 
  43. list.Add(treeRoot); 
  44. point = treeRoot; 
  45. continue
  46. if (treeRoot.rightSon != null
  47. treeRoot = treeRoot.rightSon; 
  48. Write(treeRoot); 
  49. point = treeRoot; 
  50. list.Add(treeRoot); 
  51. continue
  52. if (treeRoot.leftSon == null && treeRoot.rightSon == null
  53. list.Remove(treeRoot); 
  54. if (list.Count == 0) 
  55. break
  56. point = treeRoot; 
  57. treeRoot = list[list.Count - 1]; 
  58. public static void Write(Node node) 
  59. Console.WriteLine(node.Data); 
  60. private static Node CreateTree() 
  61. Node a = new Node("A"); 
  62. a.leftSon = new Node("B"); 
  63. a.rightSon = new Node("C"); 
  64. a.leftSon.leftSon = new Node("D"); 
  65. a.leftSon.rightSon = new Node("E"); 
  66. a.rightSon.leftSon = new Node("F"); 
  67. a.rightSon.rightSon = new Node("G"); 
  68. a.leftSon.leftSon.leftSon = new Node("H"); 
  69. a.leftSon.leftSon.rightSon = new Node("I"); 
  70. return a; 
  71. class Node 
  72. public string Data { get; set; } 
  73. public Node leftSon { get; set; } 
  74. public Node rightSon { get; set; } 
  75. public Node(string data) 
  76. Data = data; 

希望本文所述對大家的C#程序設(shè)計(jì)有所幫助。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 成人看片在线观看 | www久久99| 成人av网站在线观看 | 亚洲一区二区日韩 | 久久九九| 亚洲精品一区二区另类图片 | 日本a视频| 国产精品久久久久久久久久免费 | 国产极品久久 | 国产1页| 天天舔夜夜 | 日本成人中文字幕在线观看 | 国产亚洲一区二区三区在线观看 | 美女超碰在线 | 欧美精品国产精品 | 一区二区中文字幕 | 欧美9999| 91国内外精品自在线播放 | 一区二区三区国产 | 亚洲一二三在线 | 欧美日韩成人在线 | 国产精品亚洲天堂 | 日韩在线播放欧美字幕 | 蜜桃av在线播放 | 秋霞在线一区 | 日韩久久精品 | 影音先锋国产 | 亚洲精品久久 | 久久亚洲一区二区三区四区五区高 | 露娜同人18av黄漫网站 | 国产精品污www在线观看 | 9色porny自拍视频一区二区 | 黄色毛片视频网站 | 偷拍亚洲精品 | 亚洲国产精品va在线看黑人 | 日韩有码在线播放 | 精产国产伦理一二三区 | 日韩精品免费在线观看 | 成人看片免费网站 | 久草电影网 | 亚洲精品视频在线观看免费视频 |