Advertisement
Guest User

Untitled

a guest
Mar 30th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.85 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ATSD2
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. BalancedBST b = new BalancedBST();
  14. b.AddItem(25);
  15. b.AddItem(17);
  16. b.AddItem(35);
  17. b.AddItem(10);
  18. b.AddItem(20);
  19. b.Delete(35);
  20. b.inorder();
  21. }
  22. }
  23. class Node
  24. {
  25. public int Key { get; set; }
  26. public Node Left;
  27. public Node Right;
  28. public Node Parent;
  29. public int Height;
  30. public int Balance;
  31. public Node(int k, Node p=null, int b=0)
  32. {
  33. Key = k;
  34. Left = null;
  35. Right = null;
  36. Parent = p;
  37. Balance = b;
  38. if (Parent != null)
  39. Height = Parent.Height - 1;
  40. else
  41. Height = 1;
  42. }
  43.  
  44. }
  45.  
  46. class BalancedBST
  47. {
  48. public Node Root;
  49. public BalancedBST()
  50. {
  51. Root = null;
  52. }
  53. public BalancedBST(int key)
  54. {
  55. Root = new Node(key);
  56. }
  57. private Node RotateLeft(Node node)
  58. {
  59. Node right = node.Right;
  60. Node rightLeft = right.Left;
  61. Node parent = node.Parent;
  62.  
  63. right.Parent = parent;
  64. right.Left = node;
  65. node.Right = rightLeft;
  66. node.Parent = right;
  67.  
  68. if (rightLeft != null)
  69. {
  70. rightLeft.Parent = node;
  71. }
  72.  
  73. if (node == Root)
  74. {
  75. Root = right;
  76. }
  77. else if (parent.Right == node)
  78. {
  79. parent.Right = right;
  80. }
  81. else
  82. {
  83. parent.Left = right;
  84. }
  85.  
  86. return right;
  87. }
  88.  
  89. private Node RotateRight(Node node)
  90. {
  91. Node left = node.Left!=null?node.Left:null;
  92. Node leftRight = left.Right!=null?left.Right:null;
  93. Node parent = node.Parent;
  94.  
  95. left.Parent = parent;
  96. left.Right = node;
  97. node.Left = leftRight;
  98. node.Parent = left;
  99.  
  100. if (leftRight != null)
  101. {
  102. leftRight.Parent = node;
  103. }
  104.  
  105. if (node == Root)
  106. {
  107. Root = left;
  108. }
  109. else if (parent.Left == node)
  110. {
  111. parent.Left = left;
  112. }
  113. else
  114. {
  115. parent.Right = left;
  116. }
  117.  
  118. return left;
  119. }
  120. public void FixHeight(Node p)
  121. {
  122. int hl = 0;
  123. int hr = 0;
  124. if (p.Left != null)
  125. FixHeight(p.Left);
  126. if (p.Right != null)
  127. FixHeight(p.Right);
  128. if (p.Left != null)
  129. hl = p.Left.Height;
  130. if (p.Right != null)
  131. hr = p.Right.Height;
  132. p.Height = (hl > hr ? hl : hr) + 1;
  133. }
  134. public void RecountBalance(Node p)
  135. {
  136. int hl = 0;
  137. int hr = 0;
  138. if (p.Left != null)
  139. {
  140. RecountBalance(p.Left);
  141. hl = p.Left.Height;
  142. }
  143. if (p.Right != null)
  144. {
  145. RecountBalance(p.Right);
  146. hr = p.Right.Height;
  147. }
  148. p.Balance = hl - hr;
  149. }
  150. public void AddBalanceTree(Node p)
  151. {
  152. if (p.Left != null)
  153. AddBalanceTree(p.Left);
  154. if (p.Right != null)
  155. AddBalanceTree(p.Right);
  156. if (p.Balance == 2)
  157. {
  158. if (p.Left.Balance == 1)
  159. RotateRight(p);
  160. else
  161. {
  162. RotateLeft(p.Left);
  163. RotateRight(p);
  164. }
  165. }
  166. if (p.Balance == -2)
  167. {
  168. if (p.Right.Balance == -1)
  169. RotateLeft(p);
  170. else
  171. {
  172. RotateRight(p.Right);
  173. RotateLeft(p.Left);
  174. }
  175. }
  176. }
  177. public void AddItem(int k)
  178. {
  179. Insert(Root, k);
  180. }
  181. public void Insert(Node p, int k)
  182. {
  183. if (Root == null)
  184. Root = new Node(k);
  185. else if (p.Left == null && k < p.Key)
  186. p.Left = new Node(k, p);
  187. else if (p.Right == null && k >= p.Key)
  188. p.Right = new Node(k, p);
  189. else if (k < p.Key)
  190. Insert(p.Left, k);
  191. else if (k >= p.Key)
  192. Insert(p.Right, k);
  193. FixHeight(Root);
  194. RecountBalance(Root);
  195. AddBalanceTree(Root);
  196. }
  197. public void DelBalanceTree(Node p)
  198. {
  199. if (p.Balance == 2)
  200. RotateRight(p);
  201. if (p.Balance == -2)
  202. RotateLeft(p);
  203. if (p.Left != null)
  204. DelBalanceTree(p.Left);
  205. if (p.Right != null)
  206. DelBalanceTree(p.Right);
  207. }
  208. public void Delete(int key)
  209. {
  210. DeleteKey(ref Root, key);
  211. FixHeight(Root);
  212. RecountBalance(Root);
  213. DelBalanceTree(Root);
  214. FixHeight(Root);
  215. }
  216. public void DeleteKey(ref Node p, int key)
  217. {
  218. if (p.Key == key && p.Left == null && p.Right == null)
  219. {
  220. if(p == Root)
  221. Root = null;
  222. else if (p.Parent.Left == p)
  223. p.Parent.Left = null;
  224. else if (p.Parent.Right == p)
  225. p.Parent.Right = null;
  226.  
  227. }
  228. else if (p.Key == key && p.Left != null && p.Right == null)
  229. {
  230. if (p == Root)
  231. Root = p.Left;
  232. else
  233. {
  234. if (p.Parent.Right == p)
  235. p.Parent.Right = p.Left;
  236. else if (p.Parent.Left == p)
  237. p.Parent.Left = p.Left;
  238. }
  239. }
  240. else if (p.Key == key && p.Right != null)
  241. {
  242. Node min = findmin(p.Right);
  243. p.Key = min.Key;
  244. if (min.Parent.Left == min)
  245. {
  246. if (min.Right == null)
  247. min.Parent.Left = null;
  248. else
  249. min.Parent.Left = min.Right;
  250. }
  251. else if (min.Parent.Right == min)
  252. {
  253. if (min.Right == null)
  254. min.Parent.Right = null;
  255. else
  256. min.Parent.Right = min.Right;
  257. }
  258. }
  259. else if (p.Left != null && key < p.Key)
  260. DeleteKey(ref p.Left, key);
  261. else if (p.Right != null && key >= p.Key)
  262. DeleteKey(ref p.Right, key);
  263. }
  264.  
  265. public Node findmin(Node p)
  266. {
  267. return p.Left!=null ? findmin(p.Left) : p;
  268. }
  269.  
  270. public void inorder()
  271. {
  272. Console.WriteLine("inorder sequence of nodes:");
  273. Console.WriteLine(Root.Key);
  274. inorder_rec(Root);
  275. Console.WriteLine(";");
  276. }
  277. protected void inorder_rec(Node p)
  278. {
  279. if (p != null)
  280. {
  281. inorder_rec(p.Left);
  282. Console.Write(p.Key + ", " + p.Height + ", ");
  283. inorder_rec(p.Right);
  284. }
  285. }
  286.  
  287.  
  288. }
  289. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement