Advertisement
Guest User

Untitled

a guest
May 20th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.09 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5.  
  6. //Full Name: Oded Balter
  7. //ID: 206913097
  8.  
  9. typedef struct listNode {
  10.  
  11. int data;
  12.  
  13. struct listNode* next;
  14.  
  15. } ListNode;
  16.  
  17. typedef struct list {
  18.  
  19. ListNode* head;
  20.  
  21. ListNode* tail;
  22.  
  23. } List;
  24.  
  25. typedef struct treeNode {
  26.  
  27. int data;
  28.  
  29. struct treeNode* parent;
  30.  
  31. struct treeNode* left;
  32.  
  33. struct treeNode* right;
  34.  
  35. } TreeNode;
  36.  
  37. typedef struct tree {
  38.  
  39. TreeNode* root;
  40.  
  41. List leafList; /*øùéîä î÷åùøú ùì ëì äòìéí áòõ*/
  42.  
  43. } Tree;
  44.  
  45.  
  46.  
  47. #define LEFT 0
  48.  
  49. #define RIGHT 1
  50.  
  51.  
  52. #define SIZE 100
  53.  
  54. Tree BuildTreeFromArrayWithLeafList(int *arr, int size);
  55. void BuildTreeFromArrayWithLeafListRec(TreeNode** node, int *arr, int size, TreeNode* p, List* leafList);
  56. TreeNode* findParent(Tree tr, int parentData, int branchSelect);
  57. TreeNode* findParentRec(TreeNode* node, int parentData, int branchSelect);
  58. Tree AddLeaf(Tree tr, TreeNode *p, int branchSelect, int data);
  59. void AddNewDataToListAccordingToPosition(List* lst, int position, int NewData, TreeNode* p, int branchSelect);
  60. void insertNodeToTail(List* lst, ListNode* newTail);
  61. void insertNodeAsHead(List* lst, ListNode* newHead);
  62. ListNode* createNode(int data);
  63. void FindThePositionBeforeNewNode(TreeNode* node, TreeNode* New, int *found, int *position);
  64. TreeNode* createNewTreeNode(char data, TreeNode* left, TreeNode* right, TreeNode *p);
  65. void printTreeInorder(Tree tr);
  66. void printTreeInorderRec(TreeNode* root);
  67. void printLeafList(Tree tr);
  68. void freeTree(Tree tr);
  69. void freeTreeRec(TreeNode *root);
  70. void freeList(List lst);
  71. void checkAllocation(void* ptr);
  72. List makeEmptyList();
  73.  
  74.  
  75. void main()
  76. {
  77.  
  78. int size, i;
  79.  
  80. int arr[SIZE];
  81.  
  82. Tree tr;
  83.  
  84. TreeNode * p;
  85.  
  86. int parentData, data, branchSelect;
  87.  
  88. scanf("%d", &size);
  89.  
  90. for (i = 0; i<size; i++)
  91. scanf("%d", &arr[i]);
  92.  
  93. scanf("%d%d%d", &parentData, &data, &branchSelect);
  94.  
  95. tr = BuildTreeFromArrayWithLeafList(arr, size);//the array is given as described in question 1
  96.  
  97. p = findParent(tr, parentData, branchSelect); //scan the tree inorder (LDR) and find the first parent (a node with parentData as data) that has no child in branchSelect
  98.  
  99. tr = AddLeaf(tr, p, branchSelect, data);
  100.  
  101. printTreeInorder(tr); //Print the tree in-order (LDR)
  102.  
  103. printLeafList(tr); //Print the leaves from left to right
  104.  
  105. freeTree(tr);
  106.  
  107. }
  108.  
  109. Tree BuildTreeFromArrayWithLeafList(int *arr, int size)
  110. {
  111. Tree tr;
  112. tr.leafList = makeEmptyList();
  113. tr.root = createNewTreeNode(0, NULL, NULL, NULL);
  114. BuildTreeFromArrayWithLeafListRec(&(tr.root), arr, size, NULL, &(tr.leafList));
  115.  
  116. return tr;
  117. }
  118.  
  119. void BuildTreeFromArrayWithLeafListRec(TreeNode** node, int *arr, int size, TreeNode* p, List* leafList)
  120. {
  121. if (arr[size / 2] == -1)
  122. *node = NULL;
  123.  
  124. else if (size == 3)
  125. {
  126. TreeNode* left;
  127. TreeNode* right;
  128.  
  129. *node = createNewTreeNode(arr[1], NULL, NULL, p);
  130.  
  131. if (arr[0] == -1)
  132. left = NULL;
  133. else
  134. {
  135. left = createNewTreeNode(arr[0], NULL, NULL, *node);
  136. insertNodeToTail(leafList,createNode(arr[0])); //creates a new listnode of the leave
  137. }
  138.  
  139. if (arr[2] == -1)
  140. right = NULL;
  141. else
  142. {
  143. right = createNewTreeNode(arr[2], NULL, NULL, *node);
  144. insertNodeToTail(leafList, createNode(arr[2])); //creates a new listnode of the leave
  145. }
  146.  
  147. (*node)->left = left;
  148. (*node)->right = right;
  149.  
  150. if((*node)->right==NULL && (*node)->left ==NULL)
  151. insertNodeToTail(leafList, createNode((*node)->data)); //creates a new listnode of the leave
  152. }
  153.  
  154.  
  155. else
  156. {
  157. *node = createNewTreeNode(arr[size / 2], NULL, NULL, p);
  158. BuildTreeFromArrayWithLeafListRec(&((*node)->left), arr, size / 2, *node, leafList);
  159. BuildTreeFromArrayWithLeafListRec(&((*node)->right), arr + (size / 2) + 1, size / 2, *node, leafList);
  160.  
  161. if ((*node)->right == NULL && (*node)->left == NULL)
  162. insertNodeToTail(leafList, createNode((*node)->data)); //creates a new listnode of the leave
  163. }
  164. }
  165.  
  166. TreeNode* findParent(Tree tr, int parentData, int branchSelect)
  167. {
  168. return findParentRec(tr.root, parentData, branchSelect);
  169. }
  170.  
  171. TreeNode* findParentRec(TreeNode* node, int parentData, int branchSelect)
  172. {
  173. TreeNode *left, *right;
  174.  
  175. if (node == NULL)
  176. return NULL;
  177.  
  178.  
  179. if (node->data == parentData && ((node->left == NULL && branchSelect == LEFT) || (node->right == NULL && branchSelect == RIGHT))) //checks if is the parent
  180. return node;
  181.  
  182. left = findParentRec(node->left, parentData, branchSelect);
  183. if (left != NULL && left->data == parentData)
  184. return left; //if found as a parent in later recursion stages, returns.
  185.  
  186. else
  187. {
  188. right = findParentRec(node->right, parentData, branchSelect);
  189. if (right != NULL && right->data == parentData)
  190. return right; //if found as a parent in later recursion stages, returns
  191. }
  192.  
  193. }
  194.  
  195. Tree AddLeaf(Tree tr, TreeNode *p, int branchSelect, int data)
  196. {
  197. int position = 0, found = 0;
  198. TreeNode *New = NULL;
  199. New = createNewTreeNode(data, NULL, NULL, p);
  200.  
  201. if (branchSelect == RIGHT)
  202. {
  203. p->right = New;
  204. }
  205.  
  206. if (branchSelect == LEFT)
  207. {
  208. p->left = New;
  209. }
  210.  
  211. //enters the node accordingly
  212.  
  213. FindThePositionBeforeNewNode(tr.root, New, &found, &position); //an algoritem that counts the number of leaves before the New
  214. AddNewDataToListAccordingToPosition(&(tr.leafList), position, New->data, p, branchSelect); //adds a node which represents New to the leafList
  215.  
  216.  
  217. return tr;
  218. }
  219.  
  220. void AddNewDataToListAccordingToPosition(List* lst, int position, int NewData, TreeNode* p, int branchSelect)
  221. {
  222. ListNode* curr = lst->head;
  223.  
  224. for (int i = 0; i <= position && curr != NULL; i++)
  225. {
  226. if (i == position)
  227. {
  228. if ((branchSelect == LEFT && p->right == NULL) || (branchSelect == RIGHT && p->left == NULL)) //at that case, curr is pointing in the parent if it was a leave
  229. {
  230. if (i == 0)
  231. curr->data = NewData; //in that case, the curr which is the parent, is the head, replaces the parent, which is not a leave anymore
  232.  
  233. else
  234. curr->next->data = NewData; //in that case, curr->next is the parent, since curr "goes forward" for every time i!=0 (see end of the while loop)
  235.  
  236. break;
  237. }
  238.  
  239. else //in another case, which the New node isnt supposed to replace the parent, positions accordingly
  240. {
  241. ListNode* New = createNode(NewData);
  242.  
  243. if (curr == lst->tail)
  244. {
  245. insertNodeToTail(lst, New);
  246. break;
  247. }
  248.  
  249. if (i == 0)
  250. {
  251. insertNodeAsHead(lst, New);
  252. break;
  253. }
  254.  
  255. else
  256. {
  257. New->next = curr->next;
  258. curr->next = New;
  259. break;
  260. }
  261. }
  262.  
  263. }
  264.  
  265. if (i != 0)
  266. curr = curr->next;
  267. }
  268. }
  269.  
  270. void insertNodeToTail(List* lst, ListNode* newTail)
  271. {
  272. newTail->next = NULL;
  273.  
  274. if (lst->head == NULL)
  275. lst->head = lst->tail = newTail;
  276. else
  277. {
  278. lst->tail->next = newTail;
  279. lst->tail = newTail;
  280. }
  281. }
  282.  
  283. void insertNodeAsHead(List* lst, ListNode* newHead)
  284. {
  285. if (lst->head == NULL)
  286. lst->head = lst->tail = newHead;
  287.  
  288. else
  289. {
  290. newHead->next = lst->head;
  291. lst->head = newHead;
  292. }
  293.  
  294. }
  295.  
  296. ListNode* createNode(int data)
  297. {
  298. ListNode *result;
  299.  
  300. result = (ListNode *)malloc(sizeof(ListNode));
  301. checkAllocation(result);
  302.  
  303. result->data = data;
  304. result->next = NULL;
  305.  
  306. return result;
  307. }
  308.  
  309. void FindThePositionBeforeNewNode(TreeNode* node, TreeNode* New, int *found, int *position)
  310. {
  311. if (node != NULL)
  312. {
  313. if (node->left == NULL && node->right == NULL && node == New)
  314. *found = 1; //marks as found
  315.  
  316. if (node->left == NULL && node->right == NULL && node != New)
  317. (*position)++; //is a leave which isnt New, then adds one as one leave more before the position of New
  318.  
  319.  
  320. FindThePositionBeforeNewNode(node->left, New, found, position);
  321. if (*found == 1)
  322. return;
  323.  
  324. FindThePositionBeforeNewNode(node->right, New, found, position);
  325.  
  326. if (*found == 1)
  327. return;
  328. }
  329.  
  330. } //an algoritem that counts the number of leaves before the New
  331.  
  332. TreeNode* createNewTreeNode(char data, TreeNode* left, TreeNode* right, TreeNode *p)
  333. {
  334. TreeNode* res = (TreeNode*)malloc(sizeof(TreeNode));
  335. checkAllocation(res);
  336.  
  337. res->data = data;
  338. res->left = left;
  339. res->right = right;
  340. res->parent = p;
  341.  
  342. return res;
  343. }
  344.  
  345. void printTreeInorder(Tree tr)
  346. {
  347. printTreeInorderRec(tr.root);
  348. printf("\n");
  349. }
  350.  
  351. void printTreeInorderRec(TreeNode* root)
  352. {
  353. if (root == NULL)
  354. return;
  355. else
  356. {
  357. printTreeInorderRec(root->left);
  358. printf("%d ", root->data);
  359. printTreeInorderRec(root->right);
  360. }
  361. }
  362.  
  363. void printLeafList(Tree tr)
  364. {
  365. ListNode* curr = (tr.leafList).head;
  366.  
  367. while (curr!=NULL)
  368. {
  369. printf("%d ", curr->data);
  370. curr = curr->next;
  371. }
  372. }
  373.  
  374. void freeTree(Tree tr)
  375. {
  376. freeList(tr.leafList);
  377. freeTreeRec(tr.root);
  378. }
  379.  
  380. void freeTreeRec(TreeNode *root)
  381. {
  382. if (root != NULL)
  383. {
  384. freeTreeRec(root->left);
  385. freeTreeRec(root->right);
  386. free(root);
  387. }
  388. }
  389.  
  390. void freeList(List lst)
  391. {
  392. ListNode* curr = lst.head, *saver;
  393.  
  394. while (curr != NULL)
  395. {
  396. saver = curr->next;
  397. free(curr);
  398. curr = saver;
  399. }
  400. }
  401.  
  402. void checkAllocation(void* ptr)
  403. {
  404. if (ptr == NULL)
  405. {
  406. printf("Allocation error\n");
  407. exit(-1);
  408. }
  409. }
  410.  
  411. List makeEmptyList()
  412. {
  413. List res;
  414.  
  415. res.head = res.tail = NULL;
  416.  
  417. return res;
  418. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement