Advertisement
Ilya_Bykonya

Untitled

Oct 8th, 2022
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.50 KB | None | 0 0
  1. using System;
  2.  
  3.  
  4. namespace Lab3
  5. {
  6. class TreeNode
  7. {
  8. static private Random Generator { get; } = new();
  9.  
  10. public Int32 Value { get; }
  11. public Int32 Count => 1 + (Left?.Count ?? 0) + (Right?.Count ?? 0);
  12. public TreeNode Left { get; private set; }
  13. public TreeNode Right { get; private set; }
  14.  
  15. public TreeNode(Int32 value) => Value = value;
  16.  
  17. static public TreeNode Search(TreeNode tree, Int32 value)
  18. {
  19. if (tree is null)
  20. return null;
  21.  
  22. if(tree.Value == value)
  23. return tree;
  24. else if(tree.Value > value)
  25. return Search(tree.Left, tree.Value);
  26. else if(tree.Value < value)
  27. return Search(tree.Right, tree.Value);
  28. else
  29. return null;
  30. }
  31.  
  32. static public TreeNode InsertIntoRoot(TreeNode tree, Int32 value)
  33. {
  34. if (tree is null)
  35. return new TreeNode(value);
  36.  
  37. if(value < tree.Value)
  38. {
  39. tree.Left = InsertIntoRoot(tree.Left, value);
  40. return RotateRight(tree);
  41. }
  42. else if(value > tree.Value)
  43. {
  44. tree.Right = InsertIntoRoot(tree.Right, value);
  45. return RotateLeft(tree);
  46. }
  47. else
  48. {
  49. return tree;
  50. }
  51. }
  52. static public TreeNode Insert(TreeNode tree, Int32 value)
  53. {
  54. if (tree is null)
  55. return new TreeNode(value);
  56.  
  57. if (Generator.Next(tree.Count + 1) == 0)
  58. return InsertIntoRoot(tree, value);
  59.  
  60. if (value < tree.Value)
  61. tree.Left = Insert(tree.Left, value);
  62. else if (value > tree.Value)
  63. tree.Right = Insert(tree.Right, value);
  64.  
  65. return tree;
  66. }
  67.  
  68. static public TreeNode RotateRight(TreeNode tree)
  69. {
  70. TreeNode newRoot = tree.Left;
  71. if (newRoot is null)
  72. return tree;
  73.  
  74. tree.Left = newRoot.Right;
  75. newRoot.Right = tree;
  76. return newRoot;
  77. }
  78. static public TreeNode RotateLeft(TreeNode tree)
  79. {
  80. TreeNode newRoot = tree.Right;
  81. if (newRoot is null)
  82. return tree;
  83.  
  84. tree.Right = newRoot.Left;
  85. newRoot.Left = tree;
  86. return newRoot;
  87. }
  88. }
  89.  
  90.  
  91. class Program
  92. {
  93. static void SymmetricPrint(TreeNode tree, int offset = 0)
  94. {
  95. if (tree is null)
  96. return;
  97.  
  98. SymmetricPrint(tree.Left, offset + 1);
  99. Console.WriteLine(new string('=', offset) + tree.Value);
  100. SymmetricPrint(tree.Right, offset + 1);
  101. }
  102. static void BackwardPrint(TreeNode tree, int offset = 0)
  103. {
  104. if (tree is null)
  105. return;
  106.  
  107. BackwardPrint(tree.Left, offset + 1);
  108. BackwardPrint(tree.Right, offset + 1);
  109. Console.WriteLine(new string('=', offset) + tree.Value);
  110. }
  111. static TreeNode ForwardCopy(TreeNode left, TreeNode right)
  112. {
  113. if (right is null)
  114. return left;
  115.  
  116. left = TreeNode.Insert(left, right.Value);
  117. left = ForwardCopy(left, right.Left);
  118. left = ForwardCopy(left, right.Right);
  119. return left;
  120. }
  121. public static void Main(string[] args)
  122. {
  123. Random generator = new Random();
  124.  
  125. TreeNode tree_1 = new TreeNode(generator.Next(100));
  126. for(int i = 0; i < 10; ++i)
  127. tree_1 = TreeNode.Insert(tree_1, generator.Next(100));
  128.  
  129. TreeNode tree_2 = new TreeNode(generator.Next(100));
  130. for (int i = 0; i < 10; ++i)
  131. tree_2 = TreeNode.Insert(tree_2, generator.Next(100));
  132.  
  133.  
  134. Console.WriteLine("======================");
  135. SymmetricPrint(tree_1, 0);
  136. Console.WriteLine("======================");
  137. Console.WriteLine("======================");
  138. SymmetricPrint(tree_2, 0);
  139. Console.WriteLine("======================");
  140. tree_1 = ForwardCopy(tree_1, tree_2);
  141. Console.WriteLine("======================");
  142. SymmetricPrint(tree_1, 0);
  143. Console.WriteLine("======================");
  144. }
  145. }
  146. }
  147.  
  148.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement