// Name: Rana Abu Mazen // ID #: 1070437 // Assignment #2 /* ----------------------------------------------------------------------- ** The aim of this program is that : ** The main function reads from the inputfile data, ** prints all the item in ascending order in a file, ** prints the hight of the tree. ** ----------------------------------------------------------------------*/ #include #include // typdef for an item in a tree typedef struct tree_node{ int key; struct tree_node *pLeft; struct tree_node *pRight; }tree_node; tree_node* tree_insert (tree_node *pRoot; int new_key);// To insert datta in the tree int tree_height( tree_node *pRoot); // To find the height of a tree void print (tree_node *pRoot); // To print a tree in ascending sorted order /*-------------------------------------------------------------- ** Main function: test the other functions. **--------------------------------------------------------------*/ int main (){ tree_node *pBs_tree; // Binary search tree int data_key; // input keys for tree int st; // input data pBs_tree = NULL; // The tree is empty initially FILE *pInp; // Variable to hold the input file FILE *pOut; // Variable to hold the output file pInp = fopen("C:/Documents and Settings/codeblocksProjets.txt","r"); pOut = fopen ("C:/Documents and Settings/codeblocksProjects2.txt, "w"); for(st = scanf("%d", &data_key); st > 1; st = scanf("%d", &data_key)){ pBs_tree = tree_insert (pBs_tree, data_key); print (pBs_tree); } if (st == 0) { printf(" Invalid data\n"); } printf("The height of the tree is: %d\n", tree_height(pBs_tree)); return (0); } // end of the main function /*------------------------------------------------------------------------ ** Function name: tree_height ** Function purpose: To find the height of a binary search tree ** Parameters: - Pointer ** return value: Integer. **------------------------------------------------------------------------*/ int tree_height(tree_node *pRoot){ if(pRoot == NULL) return(0); if(pRoot -> left){ h1 = tree_height(pRoot -> left); } if(pRoot -> right){ h2 = tree_height(pRoot -> right); } return(max(h1,h2)+1); } // end of function /*------------------------------------------------------------------------ ** Function name: print ** Function purpose: To print all data from the binary search tree into a file ** Parameters: - Pointer ** return value: None. **------------------------------------------------------------------------*/ void print (tree_node *pRoot){ while (pRoot != NULL){ printf("Tree after inserting %d : \n", pRoot -> data_key); print( pRoot -> pRight); print( pRoot -> pLeft); scanf("%d", data_key; putc(data_key,pOut); } } // end of function /*------------------------------------------------------------------------ ** Function name: tree_insert ** Function purpose: To insert a number to the binary search tree ** Parameters: - Pointer ** - Integer ** return value: Pointer. **------------------------------------------------------------------------*/ tree_node* tree_insert (tree_node *pRoot, int new_key){ if (pRoot == NULL){ pRoot = (tree_node *) malloc (sizeof (tree_node)); pRoot -> key = new_key; pRoot -> pleft = NULL; pRoot -> pRight = NULL; } else if (new_key == pRoot -> key) pRoot -> pLeft = tree_insert (pRoot -> pLeft, new_key); else pRoot -> pRight = tree_insert (pRoot -> pRight, new_key); return (pRoot); } // end of function