Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > insert a new node in a binary search tree

Reply
Thread Tools

insert a new node in a binary search tree

 
 
mathon@gmx.at
Guest
Posts: n/a
 
      11-22-2006
Hello,

im currently implementing a binary search tree means, that a greater
number than root will be added as right child and a less number as left
child. My insert function looks currently like this:

Code:
    template <class Item>
	void bag<Item>::insert(const Item& entry)
	// Header file used: bintree.h
	{
	    binary_tree_node<Item> *cursor;

	    if (root_ptr == NULL)
	    {   // Add the first node of the binary search tree:
		root_ptr = new binary_tree_node<Item>(entry);
		return;
	    }

	    else
	    {   // Move down the tree and add a new leaf:
			cursor = root_ptr;
			while(cursor != NULL)
			{
				if(cursor->data() > entry)
					cursor=cursor->right();
				else if (cursor->data() <= entry)
					cursor = cursor->left();
			}
		}
	}
So in the else-branch he should look for the right place of the entry
and then insert the new node. I tried it with a while loop and step
through the tree, but i do not know exactly if this is right
respectively how should i then insert the new node.
Has somebody here an idea...?

lg matti

 
Reply With Quote
 
 
 
 
mathon@gmx.at
Guest
Posts: n/a
 
      11-22-2006

something more to the implementation - if the number is higher it
should append as right child, if the number is less or equal it should
be appended as left child.

has anybody an idea if or respectively how i have to define that right,
regarding my imlementation of my function of the previous posting?

matti

 
Reply With Quote
 
 
 
 
mathon@gmx.at
Guest
Posts: n/a
 
      11-22-2006

I modified the function a little bit, the functions is_leaf checks if
the node is a leaf or has children.

Code:
template <class Item>
    void bag<Item>::insert(const Item& entry)
    // Header file used: bintree.h
    {
        binary_tree_node<Item> *cursor;

        if (root_ptr == NULL)
        {   // Add the first node of the binary search tree:
        root_ptr = new binary_tree_node<Item>(entry);
        return;
        }

        else
        {   // Move down the tree and add a new leaf:
            cursor = root_ptr;
            while(cursor != NULL)
            {
                if(entry <= cursor->data())
                {
                    if(cursor->is_leaf())
                    {
                        cursor->set_left(new
binary_tree_node<Item>(entry));
                    }
                    else
                        cursor=cursor->left();
                }
                else
                {
                    if(cursor->is_leaf())
                    {
                        cursor->set_right(new
binary_tree_node<Item>(entry));
                    }
                    else
                        cursor = cursor->right();
                }
            }
        }
    }
Is that the right way or are there still a error reasoning in it? :-/

matti

 
Reply With Quote
 
mathon@gmx.at
Guest
Posts: n/a
 
      11-22-2006

One more modification:

Code:
template <class Item>
	void bag<Item>::insert(const Item& entry)
	// Header file used: bintree.h
	{
	    binary_tree_node<Item> * current;
	    binary_tree_node<Item> * child;

	    if (root_ptr == NULL)
	    {   // Add the first node of the binary search tree:
			root_ptr = new binary_tree_node<Item>(entry);
			return;
	    }
	    else
	    {   // Move down the tree and add a new leaf:
			current = root_ptr;
			while(current)
			{
				if(current->data() <= entry)
				{
					child = current->right();
					if(!child)
					{
						current->set_right(new binary_tree_node<Item>(entry));
						return;
					}
				}
				else
				{
					child = current->left();
					if(!child)
					{
						current->set_left(new binary_tree_node<Item>(entry));
						return;
					}
				}
				current = child;
			}
		}
	}
 
Reply With Quote
 
mathon@gmx.at
Guest
Posts: n/a
 
      11-22-2006


i finally found the error...i manipulated a pointer in the wrong way..

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Binary tree search vs Binary search Bogdan C Programming 22 10-21-2010 09:46 PM
generate an N-node random binary search tree yogi_bear_79 C++ 1 05-05-2008 12:35 PM
find path from one tree node to another tree node Peter Mueller Java 6 01-13-2008 02:36 AM
How to set the node indent property between the parent node and the leaf node viveknatani@gmail.com ASP .Net 0 02-13-2006 07:11 PM
Re: binary search tree insert() John Harrison C++ 0 07-28-2003 06:30 AM



Advertisments
 



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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57