Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h> //imported for malloc(), free(), realloc(), calloc()
- #include "declaration.h" //import info from Y2.h file
- //Load data into tree,insert it into correct place, display tree:: Key (Left:Key)(Right:Key) in traversal order
- node* addnode(node* rootnode, int info) { //takes info to be added, and address of root node.
- // printf("DEBUG: addnode function is running\n"); //debugging helper
- //printf("While loop runs"); //debugging helper
- if(rootnode == NULL){ //if rootnode doesn't exist
- rootnode = makenode(info);//make this the rootnode
- printf("DEBUG: Node placed!\n"); //debugging helper
- }
- else{
- node* previous; //create a pointer that points to the previous node traverse
- node* new = rootnode; //set current node to rootnode (this is used for comparison later on)
- while(new != NULL) { //while the current node has a value
- if(info >= new->info){ //if the new node info is more than the current node info...
- previous = new; //previous node = node that this one is being added to
- if(previous->right == NULL){
- previous->right = makenode(info); //Add newnode into right subtree
- new = new->right; //make right pointer = new nodes
- }
- else{
- addnode(new, new->info);
- }
- printf("DEBUG: node placed right\n");//debugging helper
- if(new->right ==NULL){ return rootnode;}
- }
- else /*(info <= rootnode->info)*/ { //if the new node info is less than the current node info...
- previous = new; //previous node = node that this one is being added to
- previous->left = makenode(info); //Add newnode into the left subtree
- new = new->left; //make right pointer = new nodes
- printf("DEBUG: node placed left\n");//debugging helper
- if(new->left ==NULL){ return rootnode;}
- }
- }
- }
- return rootnode; //return roots address
- }
- node* makenode(int info) { //gets info to make a new node
- //printf("DEBUG: makenode function is running\n"); //debugging helper
- //Pointers are being used to access the info fron the node as it is currently in the Heap
- node* newnode = (node*)malloc(sizeof(node)); //allocate enough memory bytes as the size of the rootnode
- newnode->left = NULL;
- newnode->right = NULL;
- newnode->info = info;
- printf("Value is: %d\n", newnode->info);
- return newnode;
- }
- void presentTree(node* nodeInfo){ //key, left key right key
- if(nodeInfo == NULL) { //If the nodesInfo is empty
- //printf("DEBUG: TREE IS EMPTY"); //debugging helper
- return; //stop presenting the Tree
- }
- printf("%d ->", nodeInfo->info);
- presentTree(nodeInfo->left);
- presentTree(nodeInfo->right);
- } //closes function
- int main(){
- node* rootnode = NULL; //initialized root node to be NULL [Emtpy Tree]
- rootnode = addnode(rootnode, 40); //Add another node with info of 40
- rootnode = addnode(rootnode, 45); //Add another node with info of 30
- rootnode = addnode(rootnode, 30); //Add another node with info of 45
- rootnode = addnode(rootnode, 25); //Add another node with info of 45
- presentTree(rootnode);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment