C#构建二叉查找树

16 篇文章 0 订阅

 

二叉查找树的 C# 实现
 
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace BinarySearchTree
  6. {
  7. public class Node
  8. {
  9. public int Data;
  10. public Node Left;
  11. public Node Right;
  12. public void DisplayNode()
  13. {
  14. Console.WriteLine(Data);
  15. }
  16. }
  17. public class BinarySearchTree
  18. {
  19. public Node root;
  20. public BinarySearchTree()
  21. {
  22. root = null;
  23. }
  24. public void 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. public void InOrder(Node theRoot)
  59. {
  60. if (!(theRoot == null))
  61. {
  62. InOrder(theRoot.Left);
  63. theRoot.DisplayNode();
  64. InOrder(theRoot.Right);
  65. }
  66. }
  67. public int 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. return null;
  85. }
  86. return current;
  87. }
  88. public bool 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. return false;
  108. }
  109. if ((current.Left == null) && (current.Right == null))
  110. {
  111. if (current == root)
  112. root = null;
  113. else if (isLeftChild)
  114. parent.Left = null;
  115. else
  116. parent.Right = null;
  117. }
  118. else if (current.Right == null)
  119. if (current == root)
  120. root = current.Left;
  121. else if (isLeftChild)
  122. parent.Left = current.Left;
  123. else
  124. parent.Right = current.Left;
  125. else if (current.Left == null)
  126. if (current == root)
  127. root = current.Right;
  128. else if (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. else if (isLeftChild)
  138. parent.Left = successor;
  139. else
  140. parent.Right = successor;
  141. successor.Left = current.Left;
  142. }
  143. return true;
  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. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值