Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h> //imported for malloc(), free(), realloc(), calloc()
  3. #include "declaration.h" //import info from Y2.h file
  4. //Load data into tree,insert it into correct place, display tree:: Key (Left:Key)(Right:Key) in traversal order
  5.  
  6.  
  7.  
  8. node* addnode(node* rootnode, int info) { //takes info to be added, and address of root node.
  9.  
  10. // printf("DEBUG: addnode function is running\n"); //debugging helper
  11.  
  12. //printf("While loop runs"); //debugging helper
  13.  
  14. if(rootnode == NULL){ //if rootnode doesn't exist
  15. rootnode = makenode(info);//make this the rootnode
  16. printf("DEBUG: Node placed!\n"); //debugging helper
  17. }
  18. else{
  19.  
  20. node* previous; //create a pointer that points to the previous node traverse
  21. node* new = rootnode; //set current node to rootnode (this is used for comparison later on)
  22.  
  23. while(new != NULL) { //while the current node has a value
  24.  
  25. if(info >= new->info){ //if the new node info is more than the current node info...
  26. previous = new; //previous node = node that this one is being added to
  27. if(previous->right == NULL){
  28. previous->right = makenode(info); //Add newnode into right subtree
  29. new = new->right; //make right pointer = new nodes
  30. }
  31. else{
  32. addnode(new, new->info);
  33. }
  34.  
  35. printf("DEBUG: node placed right\n");//debugging helper
  36. if(new->right ==NULL){ return rootnode;}
  37. }
  38. else /*(info <= rootnode->info)*/ { //if the new node info is less than the current node info...
  39. previous = new; //previous node = node that this one is being added to
  40. previous->left = makenode(info); //Add newnode into the left subtree
  41. new = new->left; //make right pointer = new nodes
  42. printf("DEBUG: node placed left\n");//debugging helper
  43. if(new->left ==NULL){ return rootnode;}
  44. }
  45. }
  46. }
  47. return rootnode; //return roots address
  48. }
  49.  
  50. node* makenode(int info) { //gets info to make a new node
  51.  
  52. //printf("DEBUG: makenode function is running\n"); //debugging helper
  53. //Pointers are being used to access the info fron the node as it is currently in the Heap
  54. node* newnode = (node*)malloc(sizeof(node)); //allocate enough memory bytes as the size of the rootnode
  55. newnode->left = NULL;
  56. newnode->right = NULL;
  57. newnode->info = info;
  58. printf("Value is: %d\n", newnode->info);
  59.  
  60. return newnode;
  61. }
  62.  
  63.  
  64. void presentTree(node* nodeInfo){ //key, left key right key
  65.  
  66. if(nodeInfo == NULL) { //If the nodesInfo is empty
  67. //printf("DEBUG: TREE IS EMPTY"); //debugging helper
  68. return; //stop presenting the Tree
  69. }
  70.  
  71.  
  72. printf("%d ->", nodeInfo->info);
  73. presentTree(nodeInfo->left);
  74. presentTree(nodeInfo->right);
  75.  
  76.  
  77. } //closes function
  78.  
  79. int main(){
  80.  
  81. node* rootnode = NULL; //initialized root node to be NULL [Emtpy Tree]
  82. rootnode = addnode(rootnode, 40); //Add another node with info of 40
  83. rootnode = addnode(rootnode, 45); //Add another node with info of 30
  84. rootnode = addnode(rootnode, 30); //Add another node with info of 45
  85. rootnode = addnode(rootnode, 25); //Add another node with info of 45
  86.  
  87.  
  88. presentTree(rootnode);
  89.  
  90.  
  91. return 0;
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement