Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.71 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace ConsoleApp1
  5. {
  6. class Program
  7. {
  8. static void Main(string[] args)
  9. {
  10. DrzewoBST drzewoBST = new DrzewoBST(0);
  11. drzewoBST.Push(10);
  12. drzewoBST.Push(11);
  13. drzewoBST.Push(12);
  14. drzewoBST.Push(13);
  15. drzewoBST.Push(14);
  16. drzewoBST.Push(15);
  17. drzewoBST.Push(16);
  18. drzewoBST.Push(17);
  19. drzewoBST.Push(18);
  20. drzewoBST.Push(19);
  21. Console.WriteLine("SHOWIN");
  22. drzewoBST.SHOWINBST(drzewoBST.korzenbst);
  23. Console.WriteLine("SHOWPRE");
  24. drzewoBST.SHOWPREBST(drzewoBST.korzenbst);
  25. Console.WriteLine("SHOWPOST");
  26. drzewoBST.SHOWPOSTBST(drzewoBST.korzenbst);
  27. Console.ReadKey();
  28. }
  29. }
  30. class Wezel
  31. {
  32. public Wezel rodzic;
  33. public Wezel lewe;
  34. public Wezel prawe;
  35. public int wartosc;
  36. public Wezel(int wartosc)
  37. {
  38. this.wartosc = wartosc;
  39. }
  40. }
  41. class DrzewoBST : Wezel
  42. {
  43. public Wezel korzenbst;
  44. public int lengthbst;
  45. public int glebokoscbst;
  46. public DrzewoBST(int wartosc) : base(wartosc)
  47. {
  48. this.korzenbst = new Wezel(wartosc);
  49. this.lengthbst = 1;
  50. this.glebokoscbst = 0;
  51.  
  52. }
  53. public Wezel znajdzRodzica(int numer)
  54. {
  55. List<int> droga = new List<int>();
  56. while (numer > 0)
  57. {
  58. --numer;
  59. numer = numer / 2;
  60. droga.Add(numer);
  61. }
  62. droga.Reverse();
  63. var rodzic = this.korzenbst;
  64. for (int i = 1; i < droga.Count; i++)
  65. {
  66. if (droga[i] % 2 == 1)
  67. {
  68. rodzic = rodzic.lewe;
  69. }
  70. else
  71. {
  72. rodzic = rodzic.prawe;
  73. }
  74. }
  75. return rodzic;
  76. }
  77. public void Push(int wartosc)
  78. {
  79. var dziecko = new Wezel(wartosc);
  80. var rodzic = this.znajdzRodzica(this.lengthbst);
  81. dziecko.rodzic = rodzic;
  82. if (rodzic.wartosc <= wartosc)
  83. {
  84. if (rodzic.lewe == null)
  85. {
  86. rodzic.lewe = new Wezel(wartosc);
  87. }
  88. else
  89. {
  90. rodzic.lewe = dziecko;
  91. }
  92. }
  93. else
  94. {
  95. if(rodzic.prawe == null)
  96. {
  97. rodzic.prawe = new Wezel(wartosc);
  98. }
  99. else
  100. {
  101. rodzic.prawe = dziecko;
  102. }
  103.  
  104. }
  105. this.lengthbst++;
  106. }
  107. public void SHOWINBST(Wezel w)
  108. {
  109. if (w == null)
  110. {
  111. return;
  112. }
  113. SHOWINBST(w.lewe);
  114. Console.WriteLine(w.wartosc + " ");
  115. SHOWINBST(w.prawe);
  116. }
  117. public void SHOWPREBST(Wezel w)
  118. {
  119. if (w == null)
  120. {
  121. return;
  122. }
  123. Console.WriteLine(w.wartosc + " ");
  124. SHOWPREBST(w.lewe);
  125. SHOWPREBST(w.prawe);
  126. }
  127. public void SHOWPOSTBST(Wezel w)
  128. {
  129. if (w == null)
  130. {
  131. return;
  132. }
  133. SHOWPOSTBST(w.lewe);
  134. SHOWPOSTBST(w.prawe);
  135. Console.WriteLine(w.wartosc + " ");
  136. }
  137. }
  138.  
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement