Tvor0zhok

СиАКОД Деревья 3

Feb 28th, 2022 (edited)
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.49 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3.  
  4. namespace ConsoleApp1
  5. {
  6. class Program
  7. {
  8. static void Main()
  9. {
  10. BinaryTree tree = new BinaryTree();
  11.  
  12. using (StreamReader fileIn = new StreamReader("C:/Users/Acer/Desktop/input.txt"))
  13. {
  14. string line = fileIn.ReadToEnd();
  15. string[] data = line.Split(' ');
  16.  
  17. foreach (string item in data)
  18. {
  19. tree.Add(int.Parse(item));
  20. }
  21. }
  22.  
  23. tree.Inorder();
  24.  
  25. if (tree.isBalanced()) Console.WriteLine("Дерево является сбалансированным");
  26. else Console.WriteLine("Дерево не является сбалансированным");
  27.  
  28. tree.Delete(15);
  29.  
  30. tree.Inorder();
  31. }
  32. };
  33.  
  34. public class BinaryTree
  35. {
  36. private class Node
  37. {
  38. public object inf;
  39. public int height;
  40. public Node left, right;
  41.  
  42. public Node(object nodeInf)
  43. {
  44. inf = nodeInf;
  45. height = 0;
  46. left = right = null;
  47. }
  48.  
  49. public static int Height(Node r)
  50. {
  51. int heightLeft = (r.left != null) ? r.left.height : -1;
  52. int heightRight = (r.right != null) ? r.right.height : -1;
  53.  
  54. return Math.Max(heightLeft, heightRight) + 1;
  55. }
  56.  
  57. public static void Add(ref Node r, object nodeInf)
  58. {
  59. if (r == null) r = new Node(nodeInf);
  60. else
  61. {
  62. if (((IComparable)(nodeInf)).CompareTo(r.inf) < 0) Add(ref r.left, nodeInf);
  63. else Add(ref r.right, nodeInf);
  64.  
  65. r.height = Height(r);
  66. }
  67. }
  68.  
  69. public static bool isBalanced(Node r)
  70. {
  71. if (r == null) return true;
  72. else
  73. {
  74. int heightLeft = (r.left != null) ? r.left.height : -1;
  75. int heightRight = (r.right != null) ? r.right.height : -1;
  76.  
  77. if (Math.Abs(heightLeft - heightRight) > 1) return false;
  78. else return isBalanced(r.left) && isBalanced(r.right);
  79. }
  80. }
  81.  
  82. public static void Inorder(Node r)
  83. {
  84. if (r != null)
  85. {
  86. Inorder(r.left);
  87. Console.Write("({0}, {1}) ", r.inf, r.height);
  88. Inorder(r.right);
  89. }
  90. }
  91.  
  92. private static void Del(Node t, ref Node tr)
  93. {
  94. if (tr.right != null)
  95. {
  96. Del(t, ref tr.right);
  97.  
  98. tr.height = Height(tr);
  99. }
  100. else
  101. {
  102. t.inf = tr.inf;
  103. tr = tr.left;
  104. }
  105. }
  106.  
  107. public static void Delete(ref Node t, object key)
  108. {
  109. if (t == null) throw new Exception("В дереве нет узла с заданным значением");
  110. else if (((IComparable)(key)).CompareTo(t.inf) < 0)
  111. {
  112. Delete(ref t.left, key);
  113.  
  114. t.height = Height(t);
  115. }
  116. else if (((IComparable)(key)).CompareTo(t.inf) > 0)
  117. {
  118. Delete(ref t.right, key);
  119.  
  120. t.height = Height(t);
  121. }
  122. else
  123. {
  124. if (t.left == null) t = t.right;
  125. else if (t.right == null) t = t.left;
  126. else
  127. {
  128. Del(t, ref t.left);
  129.  
  130. t.height = Height(t);
  131. }
  132. }
  133. }
  134. };
  135.  
  136. Node tree;
  137.  
  138. public BinaryTree() { tree = null; }
  139.  
  140. public void Add(object nodeInf)
  141. {
  142. Node.Add(ref tree, nodeInf);
  143. }
  144.  
  145. public bool isBalanced()
  146. {
  147. return Node.isBalanced(tree);
  148. }
  149.  
  150. public void Inorder()
  151. {
  152. Node.Inorder(tree);
  153. Console.Write("\n");
  154. }
  155.  
  156. public void Delete(object key)
  157. {
  158. Node.Delete(ref tree, key);
  159. }
  160. };
  161. }
Add Comment
Please, Sign In to add comment