Advertisement
krzb

Untitled

Jan 23rd, 2020
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.66 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 Zad1
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. Kopiec K1 = new Kopiec();
  14. K1.Dodaj(1);
  15. K1.Dodaj(3);
  16. K1.Dodaj(5);
  17. K1.Dodaj(1);
  18. K1.Dodaj(7);
  19. K1.Dodaj(2);
  20. K1.Dodaj(9);
  21. K1.Dodaj(12);
  22. K1.Dodaj(0);
  23.  
  24. K1.wyświetl();
  25.  
  26. if(K1.sprawdzCzyKopiecMinimalny())
  27. Console.WriteLine("Kopiec minimalny");
  28. else
  29. Console.WriteLine("To sie nie wyświetli");
  30.  
  31. Console.ReadKey();
  32. }
  33.  
  34. class Kopiec
  35. {
  36. int wielkość = 0;
  37. int[] tablica = new int[30];
  38.  
  39. public Kopiec()
  40. {
  41. }
  42.  
  43. public void Dodaj(int war)
  44. {
  45. if (wielkość + 1 < tablica.Length)
  46. {
  47. tablica[wielkość] = war;
  48. wielkość++;
  49. }
  50. buduj();
  51. }
  52.  
  53. public void napraw(int i)
  54. {
  55. int d1 = i * 3 + 1;
  56. int d2 = i * 3 + 2;
  57. int d3 = i * 3 + 3;
  58. int naj = i;
  59. if (d1 < wielkość && tablica[d1] < tablica[naj])
  60. {
  61. naj = d1;
  62. }
  63. if (d2 < wielkość && tablica[d2] < tablica[naj])
  64. {
  65. naj = d2;
  66. }
  67. if (d3 < wielkość && tablica[d3] < tablica[naj])
  68. {
  69. naj = d3;
  70. }
  71. if (naj != i)
  72. {
  73. int tmp = tablica[i];
  74. tablica[i] = tablica[naj];
  75. tablica[naj] = tmp;
  76. napraw(naj);
  77. }
  78. }
  79.  
  80. public void buduj()
  81. {
  82. for (int i = wielkość / 2; i >= 0; i--)
  83. {
  84. napraw(i);
  85. }
  86. }
  87.  
  88. public void wyświetl()
  89. {
  90. for (int i = 0; i < wielkość; i++)
  91. {
  92. Console.Write(tablica[i] + " ");
  93. }
  94. Console.WriteLine();
  95. }
  96.  
  97. public bool sprawdzCzyKopiecMinimalny()
  98. {
  99. for (int i = wielkość / 2; i >= 0; i--)
  100. {
  101. int d1 = i * 3 + 1;
  102. int d2 = i * 3 + 2;
  103. int d3 = i * 3 + 3;
  104. if (d1 < wielkość && tablica[d1] < tablica[i])
  105. {
  106. return false;
  107. }
  108. if (d2 < wielkość && tablica[d2] < tablica[i])
  109. {
  110. return false;
  111. }
  112. if (d3 < wielkość && tablica[d3] < tablica[i])
  113. {
  114. return false;
  115. }
  116. }
  117. return true;
  118. }
  119. }
  120. }
  121. }
  122.  
  123. ///////
  124.  
  125. using System;
  126. using System.Collections.Generic;
  127. using System.Linq;
  128. using System.Text;
  129. using System.Threading.Tasks;
  130.  
  131. namespace Zad2_Y1_
  132. {
  133. class Program
  134. {
  135. static void Main(string[] args)
  136. {
  137. int[] t = { 4, 2, 3, 7, 1, 5, 6 };
  138. DrzewoBST<int> d0 = CreateTreeFromArray(t);
  139. Console.ReadLine();
  140. d0.ShowInOrder();
  141. Console.WriteLine("Skoro inorder to posortowana tablica, ");
  142. List<int> list = new List<int>();
  143. foreach (var VARIABLE in t)
  144. {
  145. list.Add(VARIABLE);
  146. }
  147. list.Sort();
  148. int a = 3;
  149. int b = 6; // tu liczymy ile elementow znajduje sie w przedziale [a,b] Dokończyć samemu
  150.  
  151. Console.ReadKey();
  152. }
  153. public static DrzewoBST<T> CreateTreeFromArray<T>(T[] array) where T : IComparable<T>
  154. {
  155. DrzewoBST<T> tree = new DrzewoBST<T>();
  156. foreach (T item in array)
  157. {
  158. tree.Insert(item);
  159. }
  160. return tree;
  161. }
  162. }
  163.  
  164. class DrzewoBST<T> where T : IComparable<T>
  165. {
  166. class Node
  167. {
  168. public T value;
  169. public Node left;
  170. public Node right;
  171.  
  172. public Node(T value)
  173. {
  174. this.value = value;
  175. }
  176. }
  177.  
  178. Node root;
  179.  
  180. public DrzewoBST()
  181. {
  182. this.root = null;
  183. }
  184.  
  185. // in-order
  186. public void ShowInOrder()
  187. {
  188. this.ShowInOrder(this.root);
  189. }
  190. private void ShowInOrder(Node node)
  191. {
  192. if (node == null) return;
  193. ShowInOrder(node.left);
  194. Console.Write(node.value + " ");
  195. ShowInOrder(node.right);
  196. }
  197.  
  198.  
  199. // Wstawianie rekurencyjne
  200. public void Insert(T value)
  201. {
  202. Node node = new Node(value);
  203. if (this.root == null)
  204. {
  205. this.root = node;
  206. }
  207. else
  208. {
  209. Insert(this.root, node);
  210. }
  211. }
  212. private void Insert(Node root, Node node)
  213. {
  214. if (root.value.CompareTo(node.value) > 0)
  215. {
  216. if (root.left == null)
  217. {
  218. root.left = node;
  219. }
  220. else
  221. {
  222. Insert(root.left, node);
  223. }
  224. }
  225. else
  226. {
  227. if (root.right == null)
  228. {
  229. root.right = node;
  230. }
  231. else
  232. {
  233. Insert(root.right, node);
  234. }
  235. }
  236. }
  237. }
  238.  
  239. }
  240.  
  241. /////
  242.  
  243. static void Main( string[] args )
  244. {
  245. List<List<int>> graf = new List<List<int>>();
  246. graf.Add( new List<int> { 2, 5 } ); // graf[ 0 ] zawiera sasiadow wierzcholka 1
  247. graf.Add( new List<int> { 1, 3, 5 } );
  248. graf.Add( new List<int> { 2, 4 } );
  249. graf.Add( new List<int> { 3, 5, 6 } );
  250. graf.Add( new List<int> { 1, 2, 4 } );
  251. graf.Add( new List<int> { 4 } );
  252.  
  253. // Zaczynajac od wierzcholka 3, znajdz dlugosc drog do reszty wierzcholkow
  254. int v = 3;
  255. List<int> s = SciezkiDFS( graf, v );
  256.  
  257. for( int i = 0; i < s.Count; i++ )
  258. {
  259. Console.WriteLine( $"Droga od {v} do {i+1} wynosi: {s[i]}" );
  260.  
  261. }
  262.  
  263. Console.ReadKey();
  264. }
  265.  
  266. static List<int> SciezkiDFS( List<List<int>> graf, int v )
  267. {
  268. List<int> sciezki = new List<int>();
  269.  
  270. int pierwszy;
  271. if( v != 1 ) pierwszy = 1;
  272. else pierwszy = 2;
  273.  
  274. for( int i = pierwszy; i < graf.Count + 1; i++ )
  275. {
  276. sciezki.Add( DlugoscSciezkiDFS( graf, v, i ) );
  277. }
  278.  
  279. return sciezki;
  280. }
  281.  
  282. static int DlugoscSciezkiDFS( List<List<int>> graf, int p, int k )
  283. {
  284. List<int> sciezka = new List<int>();
  285. Stack<int> stack = new Stack<int>();
  286. bool[] odwiedzone = new bool[ graf.Count ];
  287.  
  288. int obecny = p;
  289. stack.Push( obecny );
  290. odwiedzone[ obecny - 1 ] = true;
  291.  
  292. while( stack.Count > 0 )
  293. {
  294. obecny = stack.Pop();
  295.  
  296. if( obecny == k ) return sciezka.Count;
  297.  
  298. sciezka.Add( obecny );
  299.  
  300. for( int i = graf[ obecny - 1 ].Count - 1; i >= 0; --i )
  301. {
  302. if( !odwiedzone[ graf[ obecny - 1 ][ i ] - 1 ] )
  303. {
  304. stack.Push( graf[ obecny - 1 ][ i ] );
  305. odwiedzone[ graf[ obecny - 1 ][ i ] - 1 ] = true;
  306. }
  307. }
  308. }
  309.  
  310. return 0;
  311. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement