nassss

Untitled

Mar 22nd, 2020
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.75 KB | None | 0 0
  1. public List<int> Bfs()
  2. {
  3. var list = new List<int>();
  4. Bfs(this.Root, list);
  5. return list;
  6. }
  7. private void Bfs(BinaryTreeNode root, List<int> list)
  8. {
  9. if (root == null)
  10. {
  11. return;
  12. }
  13. Queue<BinaryTreeNode> queue = new Queue<BinaryTreeNode>();
  14. queue.Enqueue(root);
  15.  
  16. while (queue.Count > 0)
  17. {
  18. var node = queue.Dequeue();
  19. list.Add(node.Value);
  20.  
  21. if (node.LeftChild != null)
  22. queue.Enqueue(node.LeftChild);
  23.  
  24. if (node.RightChild != null)
  25. queue.Enqueue(node.RightChild);
  26.  
  27. }
  28. }
  29.  
  30. public int Height()
  31. {
  32. return Height(this.Root);
  33. }
  34. private int Height (BinaryTreeNode node)
  35. {
  36. if (node == null)
  37. {
  38. return -1;
  39. }
  40. return (1 + Math.Max(Height(node.LeftChild), Height(node.RightChild)));
  41. }
  42.  
  43. public BinaryTreeNode Remove(int value)
  44. {
  45. BinaryTreeNode current = this.Root;
  46. BinaryTreeNode parent = this.Root;
  47. bool isLeftChild = false;
  48.  
  49. if (current == null)
  50. {
  51. return null;
  52. }
  53.  
  54. while( current.Value != value)
  55. {
  56. parent = current;
  57. if (current == null)
  58. {
  59. return null;
  60. }
  61. if (value < current.Value)
  62. {
  63. current = current.LeftChild;
  64. isLeftChild = true;
  65. }
  66. else
  67. {
  68. current = current.RightChild;
  69. isLeftChild = false;
  70. }
  71. }
  72.  
  73. if (current.LeftChild == null && current.RightChild ==null)
  74. {
  75. if (current == this.Root)
  76. {
  77. this.Root = null;
  78. }
  79. else
  80. {
  81. if (isLeftChild)
  82. {
  83. parent.LeftChild = null;
  84. }
  85. else
  86. {
  87. parent.RightChild = null;
  88. }
  89. }
  90. }
  91. else if (current.RightChild==null)
  92. {
  93. if (current==this.Root)
  94. {
  95. this.Root = current.LeftChild;
  96. }
  97. else
  98. {
  99. if (isLeftChild)
  100. {
  101. parent.LeftChild = current.LeftChild;
  102. }
  103. else
  104. {
  105. parent.RightChild = current.LeftChild;
  106. }
  107. }
  108. }
  109. else if (current.LeftChild==null)
  110. {
  111. if(current == this.Root)
  112. {
  113. this.Root = current.RightChild;
  114. }
  115. else
  116. {
  117. if (isLeftChild)
  118. {
  119. parent.LeftChild = current.RightChild;
  120. }
  121. else
  122. {
  123. parent.RightChild = current.RightChild;
  124. }
  125. }
  126. }
  127. else
  128. {
  129. BinaryTreeNode successor = GetSuccessor(current);
  130.  
  131. if (current == this.Root)
  132. {
  133. this.Root = successor;
  134. }
  135. else if (isLeftChild)
  136. {
  137. parent.LeftChild = successor;
  138. }
  139. else
  140. {
  141. parent.RightChild = successor;
  142. }
  143. }
  144. return this.Root;
  145. }
  146. private BinaryTreeNode GetSuccessor(BinaryTreeNode node)
  147. {
  148. BinaryTreeNode successorParent = node;
  149. BinaryTreeNode successor= node;
  150. BinaryTreeNode current = node.RightChild;
  151.  
  152. while (current!=null)
  153. {
  154. successorParent = successor;
  155. successor = current;
  156. current = current.LeftChild;
  157. }
  158. if (successor!= node.RightChild)
  159. {
  160. successorParent.LeftChild = successor.RightChild;
  161. successor.RightChild = node.RightChild;
  162. }
  163. successor.LeftChild = node.LeftChild;
  164.  
  165. return successor;
  166. }
Advertisement
Add Comment
Please, Sign In to add comment