二叉搜索树类的C#实现

实现:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. /**
  7. * 二叉搜索树
  8. * 一棵二叉搜索树是满足以下条件的二叉树
  9. * 1.所有左节点的值都小于本节点的值
  10. * 2.所有节点的值都小于右节点的值
  11. **/
  12. namespace Algorithm {
  13. class BSTreeNode <T> where T:IComparable {
  14. public BSTreeNode<T> left { get; set; }
  15. public BSTreeNode<T> right { get; set; }
  16. public BSTreeNode<T> parent { get; set; }
  17. public T key { get; set; }
  18. public BSTreeNode(T k) {
  19. key = k;
  20. }
  21. }
  22. class BSTree<T> where T : IComparable {
  23. public BSTreeNode<T> Root;
  24. public int Count = 0;
  25. public BSTree(T key) {
  26. Root = new BSTreeNode<T>(key);
  27. Count++;
  28. }
  29. public BSTree(T[] keys) {
  30. if (keys.Length == 0) {
  31. return;
  32. }
  33. Root = new BSTreeNode<T>(keys[0]);
  34. for (int i = 1; i < keys.Length; i++) {
  35. Insert(keys[i]);
  36. }
  37. }
  38. /**
  39. * 插入
  40. **/
  41. public BSTreeNode<T> Insert(T key) {
  42. BSTreeNode<T> node = Root;
  43. BSTreeNode<T> newNode = new BSTreeNode<T>(key);
  44. BSTreeNode<T> parent = null;
  45. while (node != null) {
  46. parent = node;
  47. if (key.CompareTo(node.key) > 0) {
  48. node = node.right;
  49. } else {
  50. node = node.left;
  51. }
  52. }
  53. newNode.parent = parent;
  54. if (parent == null) {
  55. return newNode;
  56. } else if (key.CompareTo(parent.key) > 0) {
  57. parent.right = newNode;
  58. } else {
  59. parent.left = newNode;
  60. }
  61. Count++;
  62. return newNode;
  63. }
  64. /**
  65. * 递归遍历
  66. * */
  67. public void Walk(BSTreeNode<T> node, Action<BSTreeNode<T>> func, string type = "mid") {
  68. if (node != null) {
  69. if (type == "pre") func(node);
  70. Walk(node.left, func, type);
  71. if (type == "mid") func(node);
  72. Walk(node.right, func, type);
  73. if (type == "post") func(node);
  74. }
  75. }
  76. /**
  77. * 非递归 中序遍历
  78. * **/
  79. public void InOrderTreeWalk(BSTreeNode<T> root, Action<BSTreeNode<T>> func) {
  80. if (root == null) {
  81. return;
  82. }
  83. Stack<BSTreeNode<T>> stack = new Stack<BSTreeNode<T>>((int)(2 * Math.Log(Count + 1)));
  84. BSTreeNode<T> current = root;
  85. while (current != null) {
  86. stack.Push(current);
  87. current = current.left;
  88. }
  89. while (stack.Count != 0) {
  90. current = stack.Pop();
  91. func(current);
  92. BSTreeNode<T> node = current.right;
  93. while (node != null) {
  94. stack.Push(node);
  95. node = node.left;
  96. }
  97. }
  98. }
  99. /**
  100. * 搜索
  101. * **/
  102. public BSTreeNode<T> Search(BSTreeNode<T> node, T key) {
  103. while (node != null && key.CompareTo(node.key) != 0) {
  104. if (key.CompareTo(node.key) < 0) {
  105. node = node.left;
  106. } else {
  107. node = node.right;
  108. }
  109. }
  110. return node;
  111. }
  112. /**
  113. * 最小值
  114. * **/
  115. public BSTreeNode<T> Min(BSTreeNode<T> node) {
  116. while (node != null && node.left != null) {
  117. node = node.left;
  118. }
  119. return node;
  120. }
  121. /**
  122. * 最大值
  123. * **/
  124. public BSTreeNode<T> Max(BSTreeNode<T> node) {
  125. while (node != null && node.right != null) {
  126. node = node.right;
  127. }
  128. return node;
  129. }
  130. /**
  131. * 后继元素
  132. * **/
  133. public BSTreeNode<T> Succ(BSTreeNode<T> node) {
  134. if (node.right != null) {
  135. return Min(node.right);
  136. }
  137. BSTreeNode<T> parent = node.parent;
  138. while (parent != null && node != parent.left) {
  139. node = parent;
  140. parent = node.parent;
  141. }
  142. return parent;
  143. }
  144. /**
  145. * 前驱元素
  146. * **/
  147. public BSTreeNode<T> Pred(BSTreeNode<T> node) {
  148. if (node.left != null) {
  149. return Max(node.left);
  150. }
  151. BSTreeNode<T> parent = node.parent;
  152. while (parent != null && node != parent.right) {
  153. node = parent;
  154. parent = node.parent;
  155. }
  156. return parent;
  157. }
  158. /**
  159. * 删除节点
  160. * */
  161. public BSTreeNode<T> Delete(BSTreeNode<T> t ,BSTreeNode<T> x) {
  162. if (x == null) return Root;
  163. BSTreeNode<T> root = t;
  164. BSTreeNode<T> oldX= x;
  165. BSTreeNode<T> parent = x.parent;
  166. if (x.left == null) {
  167. x = x.right;
  168. } else if (x.right == null) {
  169. x = x.left;
  170. } else {
  171. BSTreeNode<T> y = Min(x.right);
  172. x.key = y.key;
  173. if (y.parent != x) {
  174. y.parent.left = y.right;
  175. } else {
  176. x.right = y.right;
  177. }
  178. return root;
  179. }
  180. if (x != null) {
  181. x.parent = parent;
  182. }
  183. if (parent == null) {
  184. root = x;
  185. } else {
  186. if (parent.left == oldX) {
  187. parent.left = x;
  188. } else {
  189. parent.right = x;
  190. }
  191. }
  192. return Root;
  193. }
  194. public List<T> ToList() {
  195. List<T> list = new List<T>();
  196. Walk(Root, (BSTreeNode<T> node) => {
  197. list.Add(node.key);
  198. }, "mid");
  199. return list;
  200. }
  201. }
  202. }





posted @ 2017-03-20 00:09  xiejunzhao  阅读(487)  评论(0编辑  收藏  举报