C#构建二叉查找树

来源:互联网 发布:修改手机机型软件 编辑:程序博客网 时间:2024/06/07 02:33

 

二叉查找树的 C# 实现
 
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace BinarySearchTree
  6. {
  7. publicclass Node
  8. {
  9. publicint Data;
  10. public Node Left;
  11. public Node Right;
  12. publicvoid DisplayNode()
  13. {
  14. Console.WriteLine(Data);
  15. }
  16. }
  17. publicclass BinarySearchTree
  18. {
  19. public Node root;
  20. public BinarySearchTree()
  21. {
  22. root =null;
  23. }
  24. publicvoid Insert(int i)
  25. {
  26. Node newNode =new Node();
  27. newNode.Data = i;
  28. if (root ==null)
  29. root = newNode;
  30. else
  31. {
  32. Node current = root;
  33. Node parent;
  34. while (true)
  35. {
  36. parent = current;
  37. if (i < current.Data)
  38. {
  39. current = current.Left;
  40. if (current ==null)
  41. {
  42. parent.Left = newNode;
  43. break;
  44. }
  45. }
  46. else
  47. {
  48. current = current.Right;
  49. if (current ==null)
  50. {
  51. parent.Right = newNode;
  52. break;
  53. }
  54. }
  55. }
  56. }
  57. }
  58. publicvoid InOrder(Node theRoot)
  59. {
  60. if (!(theRoot ==null))
  61. {
  62. InOrder(theRoot.Left);
  63. theRoot.DisplayNode();
  64. InOrder(theRoot.Right);
  65. }
  66. }
  67. publicint FindMax()
  68. {
  69. Node current = root;
  70. while (!(current.Right ==null))
  71. current = current.Right;
  72. return current.Data;
  73. }
  74. public Node Find(int key)
  75. {
  76. Node current = root;
  77. while (current.Data != key)
  78. {
  79. if (key < current.Data)
  80. current = current.Left;
  81. else
  82. current = current.Right;
  83. if (current ==null)
  84. returnnull;
  85. }
  86. return current;
  87. }
  88. publicbool Delete(int key)
  89. {
  90. Node current = root;
  91. Node parent = root;
  92. bool isLeftChild =true;
  93. while (current.Data != key)
  94. {
  95. parent = current;
  96. if (key < current.Data)
  97. {
  98. isLeftChild =true;
  99. current = current.Left;
  100. }
  101. else
  102. {
  103. isLeftChild = false;
  104. current = current.Right;
  105. }
  106. if (current ==null)
  107. returnfalse;
  108. }
  109. if ((current.Left ==null) && (current.Right == null))
  110. {
  111. if (current == root)
  112. root =null;
  113. elseif (isLeftChild)
  114. parent.Left =null;
  115. else
  116. parent.Right =null;
  117. }
  118. elseif (current.Right == null)
  119. if (current == root)
  120. root = current.Left;
  121. elseif (isLeftChild)
  122. parent.Left = current.Left;
  123. else
  124. parent.Right = current.Left;
  125. elseif (current.Left == null)
  126. if (current == root)
  127. root = current.Right;
  128. elseif (isLeftChild)
  129. parent.Left = current.Right;
  130. else
  131. parent.Right = current.Right;
  132. else
  133. {
  134. Node successor = GetSuccessor(current);
  135. if (current == root)
  136. root = successor;
  137. elseif (isLeftChild)
  138. parent.Left = successor;
  139. else
  140. parent.Right = successor;
  141. successor.Left = current.Left;
  142. }
  143. returntrue;
  144. }
  145. public Node GetSuccessor(Node delNode)
  146. {
  147. Node successorParent = delNode;
  148. Node successor = delNode;
  149. Node current = delNode.Right;
  150. while (current !=null)
  151. {
  152. successorParent = successor;
  153. successor = current;
  154. current = current.Left;
  155. }
  156. if (successor != delNode.Right)
  157. {
  158. successorParent.Left = successor.Right;
  159. successor.Right = delNode.Right;
  160. }
  161. return successor;
  162. }
  163. }
  164. }