0

I have a problems in this code below, it's a part of a program for ordered binary trees. The problem is that when I enter numbers in the input, some elements are just lost and it happens all the times. I looked at the code and can't figure out why that happens. Can you help me out with this? Thanks.

void insert_ord(int number, struct treenode *currentNode){
   if(currentNode->flag == 0){
      currentNode->number = number;
      currentNode->flag = 1;                  
   }
   else{
      if(number <= currentNode->number){
         if(currentNode->left != NULL) insert_ord(number, currentNode->left);
         else {
            struct treenode *store = (struct treenode *)malloc(sizeof(struct treenode));
            currentNode->left = store;
            store->number = number;
            store->left = store->right = NULL;
            store->prev = currentNode;
         }
      }
      if(number > currentNode->number){
         if(currentNode->right != NULL) insert_ord(number, currentNode->right);
         else {
            struct treenode *store = (struct treenode *)malloc(sizeof(struct treenode));
            currentNode->right = store;
            store->number = number;
            store->left = store->right = NULL;
            store->prev = currentNode;
         }
      }
   }
}
5
  • aren't all binary trees ordered? Commented Jun 6, 2011 at 1:56
  • Yes they are, what do you mean? Commented Jun 6, 2011 at 2:03
  • "I have a problems in this code below, it's a part of a program for ordered binary trees." - the 'ordered' part is superfluous Commented Jun 6, 2011 at 2:04
  • @Mitch Wheat: no, a binary heap is not ordered in the sense I think you mean. You can also use a binary tree as a "concatenation structure", where the values at the vertices are not ordered in any way. Commented Jun 6, 2011 at 2:05
  • @Mitch: sorry, I assumed from your question you were assuming binary trees to be totally ordered. A heap is partially ordered and a concatenation tree is unordered. Commented Jun 6, 2011 at 6:49

2 Answers 2

2

You aren't setting store->flag for the newly inserted nodes. Presumably it should be set to 1.

You should also restructure your code so that the new node creation isn't duplicated - the easiest way to do this with your current code is to separate that out into a new function:

struct treenode *new_node(struct treenode *parent, int number)
{
    struct treenode *store = malloc(sizeof(struct treenode));

    if (store) {
        store->number = number;
        store->left = store->right = NULL;
        store->prev = parent;
        store->flag = 1;
    }

    return store;
}

Then your insertion code just becomes:

if (currentNode->left)
    insert_ord(number, currentNode->left);
else
    currentNode->left = new_node(currentNode, number);

(and similar for the right node).

Sign up to request clarification or add additional context in comments.

Comments

0

malloc returns uninitialised memory and you are not intialising the flag field of your malloced structures.

2 Comments

isn't this essentially what @Caf posted some 13 minutes earlier?
@Mitch, pretty much. I didn't see Caf's reply when I hit submit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.