Advertisement
Guest User

Untitled

a guest
Dec 18th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.04 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3.  
  4. using namespace std;
  5.  
  6. class BST{
  7. private:
  8. struct node{
  9. int index;
  10.  
  11. node *left;
  12. node *right;
  13.  
  14. };
  15. node *root;
  16. void AddLeafPrivate(int index, node *curr);
  17. void printInOrderPrivate(node *curr);
  18. node *returnNodePrivate(int index, node*curr);
  19. int getLowestPrivate(node *curr);
  20. void deleteNodePrivate(int index,node *parent);
  21. void deleteRoot();
  22. void deleteMatch(node *parent, node *match, bool left);
  23.  
  24. public:
  25.  
  26. BST();
  27. node *createLeaf(int index);
  28. void AddLeaf(int index);
  29. void printInOrder();
  30. node *returnNode(int index);
  31. void printChild(int index);
  32. int getRoot();
  33. int getLowest();
  34. void deleteNode(int index);
  35. //void deleteRoot();
  36. };
  37.  
  38. BST::BST(){
  39. root = NULL;
  40. }
  41. //TWORZENIE
  42. BST::node *BST::createLeaf(int index){
  43. node *temp = new node;
  44. temp->index = index;
  45.  
  46. temp->left = NULL;
  47. temp->right = NULL;
  48.  
  49. return temp;
  50. }
  51. //DODAWANIE
  52. void BST::AddLeaf(int index){
  53. AddLeafPrivate(index, root);
  54. }
  55. void BST::AddLeafPrivate(int index, node *curr){
  56. //Puste drzewo
  57. if(root == NULL){
  58. root = createLeaf(index);
  59. }
  60. //< root
  61. else if(index < curr->index){
  62. if(curr->left != NULL){
  63. AddLeafPrivate(index, curr->left);
  64. }
  65. else{
  66. curr->left = createLeaf(index);
  67. }
  68. }
  69. //> root
  70. else if(index > curr->index){
  71. if(curr->right != NULL){
  72. AddLeafPrivate(index, curr->right);
  73. }
  74. else{
  75. curr->right = createLeaf(index);
  76. }
  77. }
  78. else{
  79. cout << "Klucz o wartosci: " << index << " juz istnieje" << endl;
  80. }
  81. }
  82.  
  83. void BST::printInOrder(){
  84. printInOrderPrivate(root);
  85. }
  86. void BST::printInOrderPrivate(node *curr){
  87. if(root != NULL){
  88. if(curr->left !=NULL){
  89. printInOrderPrivate(curr->left);
  90.  
  91. }
  92. cout << curr->index << " ";
  93. if(curr->right != NULL){
  94. printInOrderPrivate(curr->right);
  95. }
  96. }
  97. else{
  98. cout << "Drzewo jest puste" << endl;
  99. }
  100. }
  101.  
  102. BST::node* BST::returnNode(int index){
  103. return returnNodePrivate(index, root);
  104. }
  105.  
  106. BST::node* BST::returnNodePrivate(int index, node *curr){
  107. if(curr != NULL){
  108. if(curr->index == index){
  109. return curr;
  110. }
  111. else{
  112. if(index < curr->index){
  113. return returnNodePrivate(index, curr->left);
  114. }
  115. else{
  116. return returnNodePrivate(index, curr->right);
  117. }
  118. }
  119. }
  120. else{
  121. return NULL;
  122. }
  123. }
  124. int BST::getRoot(){
  125. if(root != NULL){
  126. return root->index;
  127. }
  128. else{
  129. return -2000000;
  130. }
  131. }
  132. void BST::printChild(int index){
  133. node *curr = returnNode(index);
  134.  
  135. if(curr != NULL){
  136. cout <<"Wartosc wezla rodzica = " <<curr->index << endl;
  137.  
  138. if(curr->left == NULL){
  139. cout << "Lewy potomek nie istnieje" << endl;
  140. }
  141. else{
  142. cout << "Lewy potomek ma wartosc klucza = " << curr->left->index << endl;
  143. }
  144.  
  145. if(curr->right == NULL){
  146. cout << "Prawy potomek nie istnieje" << endl;
  147. }
  148. else{
  149. cout << "Prawy potomek ma wartosc klucza = " << curr->right->index << endl;
  150. }
  151.  
  152. }
  153. else{
  154. cout << "Wezel z kluczem " << index << " nie istnieje" << endl;
  155. }
  156. }
  157.  
  158. int BST::getLowest(){
  159. return getLowestPrivate(root);
  160. }
  161. int BST::getLowestPrivate(node *curr){
  162. if(root == NULL){
  163. cout << "Drzewo jest puste" << endl;
  164. return -2000000;
  165. }
  166. else{
  167. if(curr->left != NULL){
  168. return getLowestPrivate(curr->left);
  169. }
  170. else{
  171. return curr->index;
  172. }
  173. }
  174. }
  175.  
  176. void BST::deleteNode(int index){
  177. deleteNodePrivate(index, root);
  178. }
  179.  
  180. void BST::deleteNodePrivate(int index, node *parent){
  181. if(root != NULL){
  182. if(root->index == index){
  183. deleteRoot();
  184. }
  185. else{
  186. if(index < parent->index && parent->left != NULL){
  187. if(parent->left->index == index){
  188. deleteMatch(parent, parent->left, true);
  189. }
  190. else{
  191. deleteNodePrivate(index, parent->left);
  192. }
  193. }
  194. else if(index > parent->index && parent->right != NULL){
  195. if(parent->right->index == index){
  196. deleteMatch(parent, parent->left, true);
  197. }
  198. else{
  199. deleteNodePrivate(index, parent->right);
  200. }
  201. }
  202. else{
  203. cout << "Szukany wezel nie istnieje" << endl;
  204. }
  205. }
  206. }
  207. else{
  208. cout << "Drzewo jest puste" << endl;
  209. }
  210. }
  211.  
  212. void BST::deleteRoot(){
  213. if(root != NULL){
  214. node *curr = root;
  215. int rootIndex = root->index;
  216. int lowestSub;
  217.  
  218. //0 potomkow
  219. if(root->left == NULL && root->right == NULL){
  220. root = NULL;
  221. delete curr;
  222. }
  223. //1 potomek
  224. else if(root -> left == NULL && root->right != NULL){
  225. root = root->right;
  226. curr->right = NULL;
  227.  
  228. delete curr;
  229. cout << "Korzen z kluczem: " << rootIndex << " zostal usuniety" << endl;
  230. cout << "Nowym korzeniem jest wezel z wartoscia: " << root->index <<endl;
  231.  
  232. }
  233. else if(root -> left != NULL && root->right == NULL){
  234. root = root->left;
  235. curr->left = NULL;
  236.  
  237. delete curr;
  238. cout << "Korzen z kluczem: " << rootIndex << " zostal usuniety" << endl;
  239. cout << "Nowym korzeniem jest wezel z wartoscia: " << root->index <<endl;
  240.  
  241. }
  242. //2 potomkow - bierzemy najmniejszy wezel z prawego poddrzewa
  243. else{
  244. lowestSub = getLowestPrivate(root->right);
  245. deleteNodePrivate(lowestSub, root);
  246. root->index = lowestSub;
  247.  
  248. cout << "Korzen z kluczem" << rootIndex << " zostal usuniety" << endl;
  249. cout << "Zostal zastapiony wezlem z kluczem: " << root->index << endl;
  250. }
  251.  
  252. }
  253. else{
  254. cout << "Root nie istnieje" << endl;
  255. }
  256. }
  257.  
  258. void BST::deleteMatch(node *parent, node *match, bool left){
  259. if(root != NULL){
  260. node *curr;
  261. //int matchIndex = match->index;
  262. int lowestSub;
  263.  
  264. //0 potomkow
  265. if(match->left == NULL && match->right == NULL){
  266. curr = match;
  267. left == true ? parent->left = NULL : parent->right = NULL;
  268. delete curr;
  269. cout << "usunieto" << endl;
  270. }
  271. //1 potomek
  272. else if(match->left == NULL && match->right != NULL){
  273. if(left == true){
  274. parent->left = match->right;
  275. match->right = NULL;
  276. curr = match;
  277. delete curr;
  278. cout << "usunieto" << endl;
  279. }
  280. else{
  281. parent->right = match->right;
  282. match->right = NULL;
  283. curr = match;
  284. delete curr;
  285. cout << "usunieto" << endl;
  286. }
  287. }
  288. else if(match->left != NULL && match->right == NULL){
  289. if(left == true){
  290. parent->left = match->left;
  291. match->left = NULL;
  292. curr = match;
  293. delete curr;
  294. cout << "usunieto" << endl;
  295. }
  296. else{
  297. parent->right = match->left;
  298. match->left = NULL;
  299. curr = match;
  300. delete curr;
  301. cout << "usunieto" << endl;
  302. }
  303. }
  304. //2 potomkow
  305. else{
  306. lowestSub = getLowestPrivate(match->right);
  307. deleteNodePrivate(lowestSub, match);
  308. match->index = lowestSub;
  309. }
  310. }
  311. else{
  312. cout << "Drzewo jest juz puste" << endl;
  313. }
  314. }
  315. int main()
  316. {
  317. int TreeKeys[13] = {3,6,34,123,5,33,11,2,78,7,9,10,99};
  318. BST bstTree;
  319. int input = 0;
  320.  
  321. cout << "Wyswietlanie inOrder puste: " << endl;
  322. bstTree.printInOrder();
  323.  
  324. for(int i = 0; i < 13; i++){
  325. bstTree.AddLeaf(TreeKeys[i]);
  326. }
  327. cout << "Wyswietlanie inOrder z liczbami: " << endl;
  328. bstTree.printInOrder();
  329.  
  330. cout << endl;
  331.  
  332. bstTree.printChild(bstTree.getRoot());
  333. cout << "Najmniejsza wartosc w drzewie to = " << bstTree.getLowest() << endl << endl;
  334.  
  335. while(input != -1){
  336. cout << "Usun wybrana wartosc: ";
  337. cin >> input;
  338. {
  339. if(input != -1){
  340. cout << endl;
  341. bstTree.deleteNode(input);
  342. bstTree.printInOrder();
  343. cout << endl;
  344. }
  345. }
  346. }
  347.  
  348. return 0;
  349. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement