Advertisement
Guest User

tree.c

a guest
May 23rd, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.09 KB | None | 0 0
  1. #include "tree.h"
  2. #include "mymalloc.h"
  3.  
  4. bool isLeaf(TreeNode* node){
  5. if(!node->left && !node->right){
  6. return true;
  7. }
  8. return false;
  9. }
  10.  
  11. bool Tree_Init( Tree * const root ){
  12. if(!root){
  13. return false;
  14. }
  15. root->itemsCount = 0;
  16. root->root = NULL;
  17. return true;
  18. }
  19.  
  20.  
  21. void Tree_Clear( Tree* const root ){
  22. if(!root){
  23. return;
  24. }
  25. else{
  26. Tree_Clear_Rec(root->root);
  27. root->root = NULL;
  28. root->itemsCount = 0;
  29. }
  30.  
  31. }
  32.  
  33. void Tree_Clear_Rec(TreeNode* root){
  34. if(!root){
  35. return;
  36. }
  37. Tree_Clear_Rec(root->left);
  38. root->left = NULL;
  39. Tree_Clear_Rec(root->right);
  40. root->right = NULL;
  41. myFree(root);
  42. }
  43.  
  44.  
  45. bool Tree_Insert( Tree* const root, const Data_t data){
  46. if(!root || Tree_Find_Node(*root, data)){
  47. return false;
  48. }
  49. else{
  50. TreeNode* newnode = myMalloc(sizeof(TreeNode));
  51. if(!newnode){
  52. return false;
  53. }
  54. newnode->data = data;
  55. newnode->left = NULL;
  56. newnode->right = NULL;
  57.  
  58. if(!root->root){
  59. root->root = newnode;
  60. }
  61. else{
  62. Tree_Insert_Rec(root->root, newnode);
  63. }
  64.  
  65. root->itemsCount++;
  66. return true;
  67. }
  68. }
  69.  
  70. void Tree_Insert_Rec(TreeNode* root, TreeNode* newnode){
  71. if(Data_Cmp(&newnode->data, &root->data) < 0){
  72. if(root->left){
  73. Tree_Insert_Rec(root->left, newnode);
  74. }
  75. else{
  76. root->left = newnode;
  77. }
  78. }
  79. else if(Data_Cmp(&newnode->data, &root->data) > 0){
  80. if(root->right){
  81. Tree_Insert_Rec(root->right, newnode);
  82. }
  83. else{
  84. root->right = newnode;
  85. }
  86. }
  87. }
  88.  
  89. TreeNode * minValueNode(TreeNode* root)
  90. {
  91. TreeNode* current = root;
  92.  
  93. while (current->right != NULL)
  94. current = current->right;
  95.  
  96. return current;
  97. }
  98.  
  99.  
  100. TreeNode* Tree_Delete_Rec(TreeNode* root, Data_t data)
  101. {
  102. if (!root){
  103. return root;
  104. }
  105.  
  106. else if (Data_Cmp(&data, &root->data) < 0){
  107. root->left = Tree_Delete_Rec(root->left, data);
  108. }
  109.  
  110. else if (Data_Cmp(&data, &root->data) > 0){
  111. root->right = Tree_Delete_Rec(root->right, data);
  112. }
  113.  
  114. else
  115. {
  116. if (!root->left)
  117. {
  118. TreeNode *temp = root->right;
  119. myFree(root);
  120. return temp;
  121. }
  122. else if (!root->right)
  123. {
  124. TreeNode *temp = root->left;
  125. myFree(root);
  126. return temp;
  127. }
  128.  
  129. TreeNode* temp = minValueNode(root->left);
  130. root->data = temp->data;
  131. root->left = Tree_Delete_Rec(root->left, temp->data);
  132. }
  133. return root;
  134. }
  135.  
  136.  
  137. void Tree_Delete( Tree* const root, const Data_t data ){
  138. if(!root || !Tree_Find_Node(*root, data)){
  139. return;
  140. }
  141. else{
  142. root->root = Tree_Delete_Rec(root->root, data);
  143. root->itemsCount--;
  144. }
  145. }
  146.  
  147.  
  148. const Data_t *Tree_Get_Data( const TreeNode* const node ){
  149. if(!node){
  150. return NULL;
  151. }
  152. return &node->data;
  153. }
  154.  
  155.  
  156. TreeNode* Tree_Find_Node( Tree root, const Data_t data ){
  157. return Tree_Find_Node_Rec(data, root.root);
  158. }
  159.  
  160. TreeNode* Tree_Find_Node_Rec(const Data_t data, TreeNode* root){
  161. if(!root){
  162. return NULL;
  163. }
  164. if(Data_Cmp(&data, &root->data) < 0){
  165. return Tree_Find_Node_Rec(data, root->left);
  166. }
  167. else if(Data_Cmp(&data, &root->data) > 0){
  168. return Tree_Find_Node_Rec(data, root->right);
  169. }
  170. else{
  171. return root;
  172. }
  173. }
  174.  
  175. size_t Tree_Get_Count( Tree root ){
  176. return root.itemsCount;
  177. }
  178.  
  179. void Tree_Process( Tree root, TreeNodeProc proc, TreeProcessMode mode ){
  180. if(!root.root){
  181. return;
  182. }
  183. switch(mode){
  184. case procPREORDER:
  185. Tree_Process_Preorder(proc, root.root);
  186. break;
  187.  
  188. case procINORDER:
  189. Tree_Process_Inorder(proc, root.root);
  190. break;
  191.  
  192. case procPOSTORDER:
  193. Tree_Process_Postorder(proc, root.root);
  194. break;
  195. }
  196. }
  197.  
  198. void Tree_Process_Preorder(TreeNodeProc proc, TreeNode* root){
  199. (*proc)(root);
  200. if(root->left){
  201. Tree_Process_Preorder(proc, root->left);
  202. }
  203. if(root->right){
  204. Tree_Process_Preorder(proc, root->right);
  205. }
  206. }
  207.  
  208. void Tree_Process_Inorder(TreeNodeProc proc, TreeNode* root){
  209. if(root->left){
  210. Tree_Process_Inorder(proc, root->left);
  211. }
  212. (*proc)(root);
  213. if(root->right){
  214. Tree_Process_Inorder(proc, root->right);
  215. }
  216. }
  217.  
  218. void Tree_Process_Postorder(TreeNodeProc proc, TreeNode* root){
  219. if(root->left){
  220. Tree_Process_Postorder(proc, root->left);
  221. }
  222. if(root->right){
  223. Tree_Process_Postorder(proc, root->right);
  224. }
  225. (*proc)(root);
  226. }
  227.  
  228. int Tree_Get_Height(Tree* tree){
  229. if(!tree){
  230. return -1;
  231. }
  232.  
  233. int maxheight = 0;
  234. Tree_Get_Height_Recursive(tree->root, 1, &maxheight);
  235.  
  236. return maxheight;
  237. }
  238.  
  239. void Tree_Get_Height_Recursive(TreeNode* root, int currentlevel, int* maxheight){
  240. if(!root){
  241. return;
  242. }
  243. if(currentlevel > *maxheight){
  244. *maxheight = currentlevel;
  245. }
  246. Tree_Get_Height_Recursive(root->left, currentlevel + 1, maxheight);
  247. Tree_Get_Height_Recursive(root->right, currentlevel + 1, maxheight);
  248. }
  249.  
  250. unsigned int findMax(int* widtharray, unsigned int length){
  251. int maxvalue = 0;
  252. for(unsigned int i = 0; i < length; i++){
  253. if (widtharray[i] > maxvalue){
  254. maxvalue = widtharray[i];
  255. }
  256. }
  257. return maxvalue;
  258. }
  259.  
  260. int Tree_Get_Width(Tree* tree){
  261. if(!tree){
  262. return -1;
  263. }
  264. int widtharray[255] = {0};
  265. Tree_Get_Width_Recursive(tree->root, 0, widtharray);
  266.  
  267. return findMax(widtharray, 255);
  268. }
  269.  
  270. void Tree_Get_Width_Recursive(TreeNode* root, int currentlevel, int* widtharray){
  271. if(!root){
  272. return;
  273. }
  274. widtharray[currentlevel] ++;
  275. Tree_Get_Width_Recursive(root->left, currentlevel+1, widtharray);
  276. Tree_Get_Width_Recursive(root->right, currentlevel+1, widtharray);
  277. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement