473,396 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

insert a new node in a binary search tree

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:

Expand|Select|Wrap|Line Numbers
  1. template <class Item>
  2. void bag<Item>::insert(const Item& entry)
  3. // Header file used: bintree.h
  4. {
  5. binary_tree_node<Item*cursor;
  6.  
  7. if (root_ptr == NULL)
  8. {   // Add the first node of the binary search tree:
  9. root_ptr = new binary_tree_node<Item>(entry);
  10. return;
  11. }
  12.  
  13. else
  14. {   // Move down the tree and add a new leaf:
  15. cursor = root_ptr;
  16. while(cursor != NULL)
  17. {
  18. if(cursor->data() entry)
  19. cursor=cursor->right();
  20. else if (cursor->data() <= entry)
  21. cursor = cursor->left();
  22. }
  23. }
  24. }
  25.  
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

Nov 22 '06 #1
4 3875

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

Nov 22 '06 #2

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

Expand|Select|Wrap|Line Numbers
  1. template <class Item>
  2. void bag<Item>::insert(const Item& entry)
  3. // Header file used: bintree.h
  4. {
  5. binary_tree_node<Item*cursor;
  6.  
  7. if (root_ptr == NULL)
  8. {   // Add the first node of the binary search tree:
  9. root_ptr = new binary_tree_node<Item>(entry);
  10. return;
  11. }
  12.  
  13. else
  14. {   // Move down the tree and add a new leaf:
  15. cursor = root_ptr;
  16. while(cursor != NULL)
  17. {
  18. if(entry <= cursor->data())
  19. {
  20. if(cursor->is_leaf())
  21. {
  22. cursor->set_left(new
  23. binary_tree_node<Item>(entry));
  24. }
  25. else
  26. cursor=cursor->left();
  27. }
  28. else
  29. {
  30. if(cursor->is_leaf())
  31. {
  32. cursor->set_right(new
  33. binary_tree_node<Item>(entry));
  34. }
  35. else
  36. cursor = cursor->right();
  37. }
  38. }
  39. }
  40. }
  41.  
Is that the right way or are there still a error reasoning in it? :-/

matti

Nov 22 '06 #3

One more modification:

Expand|Select|Wrap|Line Numbers
  1. template <class Item>
  2. void bag<Item>::insert(const Item& entry)
  3. // Header file used: bintree.h
  4. {
  5. binary_tree_node<Item* current;
  6. binary_tree_node<Item* child;
  7.  
  8. if (root_ptr == NULL)
  9. {   // Add the first node of the binary search tree:
  10. root_ptr = new binary_tree_node<Item>(entry);
  11. return;
  12. }
  13. else
  14. {   // Move down the tree and add a new leaf:
  15. current = root_ptr;
  16. while(current)
  17. {
  18. if(current->data() <= entry)
  19. {
  20. child = current->right();
  21. if(!child)
  22. {
  23. current->set_right(new binary_tree_node<Item>(entry));
  24. return;
  25. }
  26. }
  27. else
  28. {
  29. child = current->left();
  30. if(!child)
  31. {
  32. current->set_left(new binary_tree_node<Item>(entry));
  33. return;
  34. }
  35. }
  36. current = child;
  37. }
  38. }
  39. }
  40.  
Nov 22 '06 #4


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

Nov 22 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: A | last post by:
Hi, I'm trying to solve the 3rd and final case in deleting a node from a binary tree. That is, deleting a node that has two subtrees. If someone out there who knows about this problem as they...
8
by: Jimmy | last post by:
Hi everyone, I am working with a binary tree, and I am having a bit of trouble visuallizing what needs to happen when I am trying to delete a node that has two children. (no child node and one...
2
by: maltchev | last post by:
i need to insert data from an xml file into sql server table. the xml file contains only one record. how to insert the data? how to map the names of the fields in the xml file and the table?...
9
by: raylopez99 | last post by:
What's the best way of implementing a multi-node tree in C++? What I'm trying to do is traverse a tree of possible chess moves given an intial position (at the root of the tree). Since every...
6
by: gk | last post by:
Hi, I have trouble figuring out how to search for a particular string in an XML document and annotating it. For example if I'm looking for the string "sentence with tagged words" and wish to...
11
by: Defected | last post by:
Hi, How i can create a Binary Search Tree with a class ? thanks
10
by: Aditya | last post by:
Hi All, I would like to know how it is possible to insert a node in a linked list without using a temp_pointer. If the element is the first element then there is no problem but if it is in...
7
by: kwstriker299 | last post by:
Today in class, my professor posed an interesting question for us to think about before our next class. If you insert items that are in ascending order into a binary search tree, then you will build...
1
by: yogi_bear_79 | last post by:
I am enrolled in distance learning class, this amounts to self taught. I have a book and that is about it. below is my assingment. The book doesn't prove useful for examples, and I haven't had...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.