Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.41 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 ConsoleApp2
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13.  
  14. }
  15. }
  16. class Węzeł
  17. {
  18. public Węzeł rodzic;
  19. public int wartość;
  20. public Węzeł lewe;
  21. public Węzeł prawe;
  22.  
  23. public Węzeł(int wartość)
  24. {
  25. this.wartość = wartość;
  26. }
  27. }
  28. class Drzewo //: IAISD wklej push i popa z zeszłych zajęć
  29. {
  30. public Węzeł korzeń;
  31. public int głębokość;
  32. public int length;
  33. public Drzewo(int wartość) //Drzewo d = new Drzewo;
  34. {
  35. this.korzeń = new Węzeł(wartość);
  36. this.głębokość = 0;
  37. this.length = 1;
  38. }
  39.  
  40. Węzeł ZnajdźRodzica(int numer)
  41. {
  42. List<int> droga = new List<int>();
  43. int tmp = numer;
  44. while (tmp > 0)
  45. {
  46. --tmp;
  47. tmp = tmp / 2;
  48. droga.Add(tmp);
  49. }
  50. droga.Reverse();
  51. var rodzic = this.korzeń;
  52. for (int i = 1; i < droga.Count; i++)
  53. {
  54. if (droga[i] % 2 == 1)
  55. {
  56. rodzic = rodzic.lewe;
  57. }
  58. else
  59. {
  60. rodzic = rodzic.prawe;
  61. }
  62. }
  63. return rodzic;
  64. }
  65. interface IAiSD
  66. {
  67. int Pop();
  68. void Push(int a);
  69. int Length { get; }
  70. }
  71. abstract class SD
  72. {
  73. protected int[] dane;
  74. public int Length
  75. {
  76. get;
  77. protected set;
  78. }
  79. /*public int Length
  80. {
  81. get
  82. {
  83. return dane.Count;
  84. }
  85. }*/
  86.  
  87. public SD()
  88. {
  89. this.Length = 0;
  90. this.dane = new int[this.Length];
  91. }
  92. public void Push(int element)
  93. {
  94. this.Length++;
  95. int[] tmp = this.Przepisz(this.dane);
  96. tmp[this.Length - 1] = element;
  97. this.dane = tmp;
  98. }
  99. protected int[] Przepisz(int[] stara)
  100. {
  101. int[] nowa = new int[stara.Length + 1];
  102. for (int i = 0; i < stara.Length; i++)
  103. {
  104. nowa[i] = stara[i];
  105. }
  106. return nowa;
  107. }
  108. protected int[] Zamien(int[] stara)
  109. {
  110. int[] nowa = new int[stara.Length - 1];
  111. for (int i = 0; i < stara.Length; i++)
  112. {
  113. nowa[i] = stara[i + 1];
  114. }
  115. return nowa;
  116. }
  117.  
  118. public int this[int index]
  119. {
  120. get
  121. {
  122. return this.dane[index];
  123. }
  124. set
  125. {
  126. this.dane[index] = value;
  127. }
  128. }
  129. }
  130. class Kolejka : SD, IAiSD
  131. {
  132. public Kolejka()
  133. {
  134. }
  135. public int Pop()
  136. {
  137. int[] nowa = new int[dane.Length - 1];
  138. for (int i = 0; i < nowa.Length; i++)
  139. {
  140. nowa[i] = dane[i + 1];
  141. }
  142. dane = nowa;
  143. return dane[0];
  144. }
  145.  
  146. public new int Length => throw new NotImplementedException();
  147.  
  148. public new void Push(int a)
  149. {
  150. throw new NotImplementedException();
  151. }
  152. }
  153. class Stos : SD, IAiSD
  154. {
  155. public int Pop()
  156. {
  157. int[] nowa = new int[dane.Length - 1];
  158. for (int i = 0; i < nowa.Length; i++)
  159. {
  160. nowa[i] = dane[i];
  161. }
  162. dane = nowa;
  163. return dane[dane.Length];
  164. }
  165. public new int Length => throw new NotImplementedException();
  166. public new void Push(int a)
  167. {
  168. throw new NotImplementedException();
  169. }
  170. }
  171.  
  172.  
  173. public void Push(int zmienna)
  174. {
  175. Węzeł a = new Węzeł(zmienna);
  176. a.rodzic = ZnajdźRodzica(zmienna);
  177. if (zmienna % 2 == 0)
  178. {
  179. ZnajdźRodzica(zmienna).lewe = a;
  180. }
  181. else
  182. ZnajdźRodzica(zmienna).prawe = a;
  183. }
  184. Węzeł Pop(Węzeł korzeń, int wartość)
  185. {
  186. if (korzeń == null) return korzeń;
  187.  
  188. if (wartość < korzeń.wartość)
  189. korzeń.lewe = Pop(korzeń.lewe, wartość);
  190. else if (wartość > korzeń.wartość)
  191. korzeń.prawe = Pop(korzeń.prawe, wartość);
  192. else
  193. {
  194. if (korzeń.lewe == null)
  195. return korzeń.prawe;
  196. else if (korzeń.prawe == null)
  197. return korzeń.lewe;
  198.  
  199.  
  200. korzeń.prawe = Pop(korzeń.prawe, korzeń.wartość);
  201. }
  202. return korzeń;
  203. }
  204.  
  205.  
  206. string ShowPre(Węzeł w)
  207. {
  208. if (w == null)
  209. return "";
  210. string wynik = "({0}" + w.wartość.ToString();
  211. wynik += ShowPre(w.lewe);
  212. wynik += ShowPre(w.prawe);
  213. ShowPre(w.prawe);
  214. return wynik;
  215. }
  216. string ShowIn(Węzeł w)
  217. {
  218. if (w == null)
  219. return "";
  220. wynik += ShowIn(w.lewe);
  221. string wynik = " " + w.wartość.ToString();
  222. wynik += ShowIn(w.prawe);
  223. ShowIn(w.prawe);
  224. return wynik;
  225. }
  226.  
  227. string ShowPost(Węzeł w)
  228. {
  229. if (w == null)
  230. return "";
  231. wynik += ShowPost(w.lewe);
  232. wynik += ShowPost(w.prawe);
  233. string wynik = "{0}" + w.wartość.ToString();
  234. ShowPost(w.prawe);
  235. return wynik;
  236. }
  237.  
  238. }
  239. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement