| | |
Copying a sorted array to a balanced inorder Binary Search Tree
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Jan 2009
Posts: 11
Reputation:
Solved Threads: 0
Hey everyone,
So, for my C++ course I am implementing a Binary Search Tree. In this Binary Search Tree we are to create a function to copy the values of a passed array into a Balanced Binary Search Tree that will render a correct inorder traversal.
I.E. an array of 0, 1, 2 will be in the tree in Root->1, Left->0, Right->2
I think I'm pretty close to figuring out the function (which is good considering how long I've been working on it
). However, I'm having issues on figuring out not only my base case, but also if passing mid in is the correct way to go about "altering" my values.
I know line 25 is wrong, as well as (I think) line 20 but I'm not sure where to go from here...Any help would be greatly appreciated!
PS: The while loop in the first function is to find out what the highest subscript with a stored value in the array is. We are guaranteed to be passed a statically allocated array of 99 elements, but we have to find out how many elements are in the array. Thus the loop.
So, for my C++ course I am implementing a Binary Search Tree. In this Binary Search Tree we are to create a function to copy the values of a passed array into a Balanced Binary Search Tree that will render a correct inorder traversal.
I.E. an array of 0, 1, 2 will be in the tree in Root->1, Left->0, Right->2
I think I'm pretty close to figuring out the function (which is good considering how long I've been working on it
). However, I'm having issues on figuring out not only my base case, but also if passing mid in is the correct way to go about "altering" my values.I know line 25 is wrong, as well as (I think) line 20 but I'm not sure where to go from here...Any help would be greatly appreciated!
PS: The while loop in the first function is to find out what the highest subscript with a stored value in the array is. We are guaranteed to be passed a statically allocated array of 99 elements, but we have to find out how many elements are in the array. Thus the loop.
c++ Syntax (Toggle Plain Text)
//------------------------------ arrayToBSTree -------------------------------- void BSTree::arrayToBSTree(NodeData *data[]) { this->makeEmpty(); int i = 0; int high = i; while(data[i] != NULL && i <= 99) { high = i; i++; } this->overallRoot = toTreeHelper(data, 0, high, this->overallRoot); } //------------------------------ toTreeHelper --------------------------------- BSTree::Node* BSTree::toTreeHelper(NodeData *data[], int low, int high, Node *root) { int mid = (low + high) / 2; root = new Node; root->left = root->right = NULL; root->data = NULL; if(mid != 0) { root->left = toTreeHelper(data, low, mid, root->left); } root->data = data[mid]; data[mid] = NULL; if(mid != 0 || mid != high - 1) { root->right = toTreeHelper(data, mid + 1, high, root->right); } return root; }
•
•
Join Date: May 2008
Posts: 639
Reputation:
Solved Threads: 103
I'd like to experiment with the code to try it, but in the interest of a quick answer, try
Basic translation: If there are values between low and high that are lower than mid, add them to the left. If there are values between low and high that are higher than mid, add them to the right.
low < mid on line 20 and mid < high on line 25.Basic translation: If there are values between low and high that are lower than mid, add them to the left. If there are values between low and high that are higher than mid, add them to the right.
It makes no sense to check if mid != 0. How could mid be 0? Well, in most cases, mid could not be 0, except at the very left edge of the tree. So your checks for the base case are bad.
Basically your hard job is to consider the cases where high - low is 0 (if possible), 1 (if possible), 2, 3, maybe 4, and in general, even or odd.
Also, you should look twice at your arrayToBSTree function: you might go past the end of the array.
Basically, this problem is designed to get you to practice thinking about off-by-one errors.
Basically your hard job is to consider the cases where high - low is 0 (if possible), 1 (if possible), 2, 3, maybe 4, and in general, even or odd.
Also, you should look twice at your arrayToBSTree function: you might go past the end of the array.
Basically, this problem is designed to get you to practice thinking about off-by-one errors.
If you want optimal balance, you'll need to think carefully about the case where the size of the segment you're working on is a power of two. You need to split that cleanly in half. Are you splitting it cleanly in half?
Also, what do you think the value of root->left is in:
Also, what do you think the value of root->left is in:
C++ Syntax (Toggle Plain Text)
root->left = toTreeHelper(data, low, mid, root->left);
•
•
Join Date: May 2008
Posts: 639
Reputation:
Solved Threads: 103
Ok, so I have the values 0,1,2,3,4,5,6,7,8,9,10 in those positions in the input array...
You caclulate mid as (low + high) /2 so the first mid is 5?
we put 5 at the root and iterate left with (0,4) and right with (6,10)
(0,4): mid=2 we put 2 to the left of 5 [left: (0,1) right: (3,4)]
(0,1): mid=0 we put 0 to the left of 2 [left: none right: (1,1)]
(1,1): mid=1 we put 1 to the right of 0 [none]
(3,4): mid=3 we put 3 to the right of 2 [left:none right: (4,4)]
(4,4): mid=4 we put 4 to the right of 3
(6,10): mid=8 we put 8 to the right of 5 [left: (6,7) right: (9,10)]
(6,7): mid=6 we put 6 to the left of 8 [left: none right: (7,7)]
(7,7): mid=7 we put 7 to the right of 6
(9,10): mid=9 we put 9 to the right of 8 [left:none right: (10,10)]
(10,10): mid=10 we put 10 to the right of 9 [none]
so we built something like:
The z's are empty nodes. How much more balanced does that need to be?
You caclulate mid as (low + high) /2 so the first mid is 5?
we put 5 at the root and iterate left with (0,4) and right with (6,10)
(0,4): mid=2 we put 2 to the left of 5 [left: (0,1) right: (3,4)]
(0,1): mid=0 we put 0 to the left of 2 [left: none right: (1,1)]
(1,1): mid=1 we put 1 to the right of 0 [none]
(3,4): mid=3 we put 3 to the right of 2 [left:none right: (4,4)]
(4,4): mid=4 we put 4 to the right of 3
(6,10): mid=8 we put 8 to the right of 5 [left: (6,7) right: (9,10)]
(6,7): mid=6 we put 6 to the left of 8 [left: none right: (7,7)]
(7,7): mid=7 we put 7 to the right of 6
(9,10): mid=9 we put 9 to the right of 8 [left:none right: (10,10)]
(10,10): mid=10 we put 10 to the right of 9 [none]
so we built something like:
C++ Syntax (Toggle Plain Text)
5 2 8 0 3 6 9 z 1 z 4 z 7 z 10
The z's are empty nodes. How much more balanced does that need to be?
Last edited by Murtan; Jan 22nd, 2009 at 2:37 am. Reason: should have drawn the tree in notepad
![]() |
Other Threads in the C++ Forum
- Previous Thread: help.my input isn't wat i request.
- Next Thread: difference between linux distros, windows, etc.
Views: 1428 | Replies: 10
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays assignment beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory multidimensional newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






