Call to Tree Insertion Function

Hey

I've been looking at some example code for a recursive function that inserts an element into a binary search tree. It's a relatively simple function that takes two arguments: the tree to be inserted into and the value to be inserted, the prototype looks like this: node* insert(node* p_tree, int value). Don't get me wrong, I understand what the function does I'm just not sure what would be sent to the function from main as 'p_tree' eg. insert(Something, 10)I'm assuming that you could write insert(NULL, 10) on the first call but I'm completely clueless as to what you would do in any subsequent calls. Anyway, any help would be much appreciated and code showing a call from main would be great.

Cheers.

Last edited on

This could help you to start.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

node*  insert(node* p_tree, int value)
{
    struct node* parent = (struct node*)malloc(sizeof(struct node));
    
    p_tree->value = value;     
    
    p_tree->left = NULL; 
    p_tree->right = NULL;
    parent = NULL;  

  if(IsEmptyTree()==1)
      root = p_tree;    
  else
  {      
    struct node* curr =(struct node*)malloc(sizeof(struct node)); 
    curr = root; 

    while(curr) 
    {
        parent = curr; 
        
        if( p_tree->value == curr->value )
           curr = curr->right; 
        else 
           curr = curr->left; 
    }

    if( p_tree->value < p_tree->value) 
       parent->left = p_tree; 
    else parent->right = p_tree; 
  }
  return root;
}


@cronopio
Last edited on
Topic archived. No new replies allowed.