patterncppMinor
Making a sorted linked list into a balanced binary search tree
Viewed 0 times
searchintobalancedbinarysortedlistmakinglinkedtree
Problem
My code works, but I need help on revising it to make it shorter and simple if possible on my main.cpp part ONLY. The other files is to make the program compile and work out if you want to see the output.
main.cpp:
bintree.h header file
```
// FILE: bintree.h (part of the namespace main_savitch_10)
// PROVIDES: A template class for a node in a binary tree and functions for
// manipulating binary trees. The template parameter is the type of data in
// each node.
//
// TYPEDEF for the binary_tree_node template class:
// Each node of the tree contains a piece of data and pointers to its
// children. The type of the data (binary_tree_node::value_type) is
// the Item type from the template parame
main.cpp:
#include
#include "bintree.h"
#include
#include
#include
#include
#include
using namespace std;
using namespace main_savitch_10;
list* item_list = new list;
list* item_list2 = new list;
list* create_list()
{
for(int i = 1; i push_front(i);
item_list -> sort();
}
return item_list;
}
list* temp2 = new list;
template
binary_tree_node* balanced_tree_rec(list* list,Item count)
{
if(count *left=balanced_tree_rec(list, count /2);
binary_tree_node* temp = new binary_tree_node(list->front());
for(int i = 1; i ( list->front() );
}
temp2 = list;
temp2 -> pop_front();
binary_tree_node * right = balanced_tree_rec(temp2, count - ( count / 2) -1);
binary_tree_node* root = new binary_tree_node(temp -> data(), left, right);
return root;
}
template
binary_tree_node* BBST(list* list)
{
int count = list->size();
return balanced_tree_rec(list, count);
}
template
void push(Item& test)
{
item_list2 -> push_back(test);
}
template
void print(binary_tree_node* tree)
{
if(tree)
{
cout data() left());
printT(tree -> right());
}
}
int main()
{
printf("The Balance Binary Search Tree\n");
list* a = create_list();
binary_tree_node* p = BBST(a);
print(p, 1);
return 0;
}bintree.h header file
```
// FILE: bintree.h (part of the namespace main_savitch_10)
// PROVIDES: A template class for a node in a binary tree and functions for
// manipulating binary trees. The template parameter is the type of data in
// each node.
//
// TYPEDEF for the binary_tree_node template class:
// Each node of the tree contains a piece of data and pointers to its
// children. The type of the data (binary_tree_node::value_type) is
// the Item type from the template parame
Solution
Shortening main.cpp
Move all the template functions into either bintree.h or bintree.template.
Templates are for generic use and really shouldn't be in the file where you
are using them.
Perhaps make the template functions in main.cpp part of your binary tree class.
using namespace std;
Namespaces were invented to prevent the collision of function names from different
sources. The code contains a specific namespace, main_savitch_10. Within
main_savitch_10 std:: is actually used. It would be less confusing to anyone
that had to maintain this code if the namespaces were fully specified within
all the code. Take a look at this question on StackOverflow.
This is especially true in your function balanced_tree_rec(), this
would be a lot more readable and maintainable if it was declared
Global Variables
The use of global variables is generally frowned on. There may be a few valid uses
of them, but the use of global variables within a template function is not
portable and not maintainable. It effectively breaks the template function.
template
binary_tree_node balanced_tree_rec(list list,Item count)
template void push(Item& test)
Use Meaningful Names
Quite often in the software development world, others have to maintain the code
we write, variable names and function names should clearly indicate what they
are some examples that would be a problem are
Standard File Extensions
There are several discussions on Stack Overflow about the proper extension for
a template file, you might want to look at these (convention, extension and templates). I personally would either add it within the name space section of bintree.h or create a bintree.hpp. The boost libraries use hpp. The reason I would combine both files into one is that the template file by itself won't compile without the bintree.h.
Move all the template functions into either bintree.h or bintree.template.
Templates are for generic use and really shouldn't be in the file where you
are using them.
Perhaps make the template functions in main.cpp part of your binary tree class.
using namespace std;
Namespaces were invented to prevent the collision of function names from different
sources. The code contains a specific namespace, main_savitch_10. Within
main_savitch_10 std:: is actually used. It would be less confusing to anyone
that had to maintain this code if the namespaces were fully specified within
all the code. Take a look at this question on StackOverflow.
This is especially true in your function balanced_tree_rec(), this
would be a lot more readable and maintainable if it was declared
binary_tree_node* balanced_tree_rec(std::list* list,Item count)Global Variables
The use of global variables is generally frowned on. There may be a few valid uses
of them, but the use of global variables within a template function is not
portable and not maintainable. It effectively breaks the template function.
list* item_list = new list;
list* item_list2 = new list;
list* temp2 = new list;template
binary_tree_node balanced_tree_rec(list list,Item count)
temp2 = list;
temp2 -> pop_front();template void push(Item& test)
item_list2 -> push_back(test);Use Meaningful Names
Quite often in the software development world, others have to maintain the code
we write, variable names and function names should clearly indicate what they
are some examples that would be a problem are
BBST(), list, p, and a;Standard File Extensions
There are several discussions on Stack Overflow about the proper extension for
a template file, you might want to look at these (convention, extension and templates). I personally would either add it within the name space section of bintree.h or create a bintree.hpp. The boost libraries use hpp. The reason I would combine both files into one is that the template file by itself won't compile without the bintree.h.
Code Snippets
binary_tree_node<int>* balanced_tree_rec(std::list<Item>* list,Item count)list<int>* item_list = new list<int>;
list<int>* item_list2 = new list<int>;
list<int>* temp2 = new list<int>;temp2 = list;
temp2 -> pop_front();item_list2 -> push_back(test);Context
StackExchange Code Review Q#134495, answer score: 2
Revisions (0)
No revisions yet.