[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Binary Search

Posted on 2004-11-13
6
Medium Priority
?
318 Views
Last Modified: 2013-11-15
#include <stdio.h>
#include <stdlib.h>

/* Small program to explore using binary trees.
   The program takes an array filled with random numbers
   and inserts the numbers into a binary tree.
   The program then prints the contents of the tree in
   key order. It should print -
   0 1 2 3 4 5 6 7 8 9
   The program then free's up the tree.

   I can't seem to find the error of this program..
*/

struct treeRecord
{
   int key;
   struct treeRecord *left, *right;
};

typedef struct treeRecord treeNode;

void insertArrayInTree(treeNode **root, int array[], int size);
void insertElementInTree(treeNode **root, int number);
void printTree(treeNode *start);
void freeTree(treeNode **start);
treeNode* newNode(int number);

int main()
{
   int array[] = { 6, 3, 8, 1, 5, 2, 9, 7, 4, 0 };
   treeNode *root = NULL;

   insertArrayInTree(&root, array,10);
   printTree(root);
   printf("\n");
   freeTree(&root);

   return 0;
}


void insertArrayInTree(treeNode **root, int array[], int size)
{
   /* insert elements of array into correct position in binary tree */

   int i;

   for (i=0; i<size; i++)
   {
      insertElementInTree(root, array[i]);
   }
}

void insertElementInTree(treeNode **root, int number)
{
   treeNode *temp = *root;

if (*temp != NULL)
{
      if (number < (*root)->key)
         insertElementInTree(&(temp->left), number);
      else
         insertElementInTree(&(temp->right), number);
}
else
{
   *root = newNode(number);
}
}

void printTree(treeNode *root)
{
   /* print the contents of the tree in order */

   while (root != NULL)
   {
      printf(" %d", root->key);
      root = root->right;
   }
}

void freeTree(treeNode **root)
{
   /* free up memory allocated to binary tree */
   treeNode *temp = *root;

   if (temp != NULL)
   {
      if (temp->left != NULL) freeTree(&temp->left);
      if (temp->right != NULL) freeTree(&temp->right);
      free(temp);
      *root = NULL;
   }
}
treeNode* newNode(int number)
{
   /* dynamicaly allocate memory for a treeNode
      make sure it is initialised correctly
   */

   treeNode *temp;

   temp = (treeNode *) malloc(sizeof(treeNode));
   if (temp == NULL)
   {
      printf("WARNING - Memory allocation error\n");
      exit(EXIT_FAILURE);
   }
   temp->key = number;
   temp->left = NULL;

   return temp;
}
0
Comment
Question by:blaze_wk
  • 3
4 Comments
 
LVL 5

Accepted Solution

by:
van_dy earned 2000 total points
ID: 12577379
thre are not many errors  however make the following changes

void insertElementInTree(treeNode **root, int number)
{
   treeNode *temp = *root;

if (temp != NULL)            //not *root
{
      if (number < (*root)->key)
         insertElementInTree(&(temp->left), number);
      ..// rest of you insertElementTree()



and use this function for printing tree
void printTree(treeNode *root)
{
   /* print the contents of the tree in order */

      if(!root)
            return;
      printTree(root->left);
      printf(" %d", root->key);
      printTree(root->right);

}


hope this helps,
van_dy


0
 
LVL 5

Expert Comment

by:van_dy
ID: 12577384
in case i was not clear, in my last post, in  function void insertElementInTree(treeNode **root, int number)

>> if (temp != NULL)            //not *root
should be
     if(temp != NULL)            //not *temp
0
 

Expert Comment

by:ksanand_be
ID: 12579728
Hi,

Is that (*root)->key or root->key? Does that make any difference?
0
 
LVL 5

Expert Comment

by:van_dy
ID: 12579741
>>Is that (*root)->key or root->key? Does that make any difference?
It should be (*root)->key in insertElementInTree(treeNode **root, int number). (*root) will be a pointer to treenode,
so u access its key memeber by (*root)->key
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Want to know how to use Exchange Server Eseutil command? Go through this article as it gives you the know-how.
Tech giants such as Amazon and Google have sold Alexa and Echo to such an extent that they have become household names. And soon they are expected to be used by commoners in their homes, ordering takeout, picking out a song, answering trivia questio…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question