Binary Tree help!

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2008
Posts: 2
Reputation: johnnygaddar is an unknown quantity at this point 
Solved Threads: 0
johnnygaddar johnnygaddar is offline Offline
Newbie Poster

Binary Tree help!

 
0
  #1
Apr 6th, 2008
Hi,
I need to do this problem for an assignment. We have not focused very much on this topc and that would be great if someone could help me get started. I am not asking for a complete solution or anything, just to head me in the right direction. The problem is:

Problem: Specify, design, and implement a class for complete binary trees using the array representation. You should have only one member function that adds a nre node (since there is only one place where a node may be added), and one member function that removes the last node of the tree.

What I get is that complete binary tree should have all the leaves at the same depth. So basically we create a tree to which elemts can be added or removed through the user input. I am positng below source code for basic sturcture of a tree. I have not implemented any functions in it. If you can use the source below and guide me what I should do next. Thanks!
  1. plusplus]
  2. #include <cstdlib> //Provides EXIT_SUCCESS
  3. #include <iostream> //Provides cout, cin
  4. #include <string> //Provides string class
  5.  
  6. using namespace std;
  7.  
  8.  
  9. template <class Item>
  10.  
  11. class binary_tree_node
  12. {
  13. public:
  14.  
  15. //TYPEDEF
  16. typedef Item value_type;
  17.  
  18. //CONSTRUCTOR
  19. binary_tree_node(
  20. const Item& init_data = Item(),
  21. binary_tree_node* init_left = NULL,
  22. binary_tree_node* init_right = NULL
  23. )
  24. {
  25. data_field = init_data;
  26. left_field = init_left;
  27. right_field = init_right;
  28. }
  29.  
  30. //MODIFICATION MEMBER FUNCTIONS
  31.  
  32. Item& data() { return data_field; }
  33. binary_tree_node*& left() { return left_field; }
  34. binary_tree_node*& right() { return right_field; }
  35. void set_data(const Item& new_data) { data_field = new_data; }
  36. void set_left(binary_tree_node* new_left) { left_field = new_left; }
  37. void set_right(binary_tree_node* new_right) { right_field = new_right; }
  38.  
  39. //CONSTANT MEMBER FUNCTIONS
  40. const Item& data() const { return data_field; }
  41. const binary_tree_node* left() const { return left_field; }
  42. const binary_tree_node* right() const { return right_field; }
  43. bool is_leaf() const
  44. { return (left_field ==NULL) && (right_field ==NULL); }
  45.  
  46. private:
  47. Item data_field;
  48. binary_tree_node *left_field;
  49. binary_tree_node *right_field;
  50. };
  51.  
  52.  
  53. template <class Item>
  54. void tree_clear(binary_tree_node<Item>*& root_ptr);
  55. //Precondition: root_ptr is the root pointer of a binary tree (whihc may be NULL for the empty tree
  56. //Postcondition: All nodes at the root or below have been returned to the heap, and root_ptr has been set to NULL.
  57.  
  58. template <class Item>
  59. binary_tree_node<Item>* tree_copy
  60. (const binary_tree_node<Item>* root_ptr);
  61. //Precondition: root_ptr is the root pointer of a binary tree (which may be NULL for the empty tree).
  62. //Postcondition: A copy of the binary tree has been made, and the return value is apointer to the root of this copy.
  63.  
  64. template <class Item>
  65. void tree_clear(binary_tree_node<Item>*& root_ptr)
  66. {
  67. if (root_ptr != NULL)
  68. {
  69. tree_clear( root_ptr->left());
  70. tree_clear( root_ptr->right());
  71. delete root_ptr;
  72. root_ptr = NULL;
  73. }
  74. }
  75.  
  76.  
  77. template <class Item>
  78. binary_tree_node<Item>* tree_copy
  79. (const binary_tree_node<Item>* root_ptr)
  80. {
  81. binary_tree_node<Item> *l_ptr;
  82. binary_tree_node<Item> *r_ptr;
  83.  
  84. if (root_ptr ==NULL)
  85. return NULL;
  86. else
  87. {
  88. l_ptr = tree_copy( root_ptr->left());
  89. r_ptr = tree_copy( root_ptr->right());
  90. return new binary_tree_node<Item>
  91. (root_ptr->data(), l_ptr, r_ptr);
  92. }
  93. }
Last edited by Narue; Apr 6th, 2008 at 9:36 am. Reason: Added code tags, please do it yourself next time.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,614
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 713
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Binary Tree help!

 
0
  #2
Apr 6th, 2008
>If you can use the source below and guide me what I should do next.
Basically you haven't done anything, but you seem to understand the problem well enough. What you need to do is figure out how to add a new node at the correct position (as far to the right as possible on the first non-empty level without leaving gaps). I'll even give you three hints to start you thinking:

1) Think in terms of traversals and see if you can figure out an algorithm for finding the "last" node in a complete tree. From there it isn't difficult to see how you can add a new node. If the "last" node is also the last node on the level, start a new level (from the root, go left all the way and add a new node at the end of the path). If it isn't the last node on the level, find the inorder successor and add a new inorder node. Note that this isn't a good solution. It's very brute forcey and naive.

2) A complete binary tree is very well suited to storage in an array. Do some research on heaps, because they take advantage of that, and so can you.

3) The mathematical properties of a complete tree are very uniform. It's not hard to figure out, in a complete tree of N nodes, the physical location of the "last" node. Likewise, you can easily figure out mathematically where the physical location of the parent of the next "last" node to be inserted is (the kth node). Using that you can perform a simple traversal, halt when you get to the kth node and insert your new "last" node. This is an elegant solution.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 2
Reputation: johnnygaddar is an unknown quantity at this point 
Solved Threads: 0
johnnygaddar johnnygaddar is offline Offline
Newbie Poster

Re: Binary Tree help!

 
0
  #3
Apr 8th, 2008
thanks Narue!

i am using array method as we r suppoed to. so basically the program deletes the node of the tree which is the last elemt of the array. right?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC