snippetcppMinor
Convert an array to a binary tree
Viewed 0 times
convertbinaryarraytree
Problem
I wrote this code in C++ to convert an array to a binary tree.
For instance,
( here, I made a function named 'create_tree()' which only take one argument )
Is there any shorter way to do this?
```
#include
#include
#include
template
struct TNode{
E value;
TNode* leftChild;
TNode* rightChild;
TNode(){
this->value = NULL;
this->leftChild = NULL;
this->rightChild = NULL;
}
TNode(E value){
this->value = value;
this->leftChild = NULL;
this->rightChild = NULL;
}
};
template
class BTree{
private:
TNode* root;
/**
build and return the pointer to a binary tree for given array 'arr' and array size 'len'
*/
TNode build(E arr, int len){
TNode *subRoot = new TNode(arr[0]);
if (len == 1){
return subRoot; // terminating function - at the leaf
}
E* elementsToLeft = new E[len]; // array to store the left subtree children
E* elementsToRight = new E[len]; // array to store the right sub tree children
int counterLeft = 0;
int counterRight = 0;
for (int i = 1; i leftChild = build(elementsToLeft, counterLeft); // recursive call> array- left sub tree's children and size
subRoot->rightChild = build(elementsToRight, counterRight); // recursive call> array- right sub tree's children and size
return subRoot;
}
public:
/**
add the value specified by newvalueas the left child of the tree node specified by parent,if the node does not have a left childalready.
returns 1 if addition is successful and returns -1 in failure.
*/
template
int intaddLeftChild(TNode* parent, E newvalue){
if (parent->leftChild != NULL){ return -1; }
TNode *temp = new TNode(newvalue);
parent->leftChild = temp;
return 1;
}
For instance,
int nums[] = {7,5,9,1,7,3} to a binary tree (root-7, root's left child-5, root's right child-9...).( here, I made a function named 'create_tree()' which only take one argument )
Is there any shorter way to do this?
```
#include
#include
#include
template
struct TNode{
E value;
TNode* leftChild;
TNode* rightChild;
TNode(){
this->value = NULL;
this->leftChild = NULL;
this->rightChild = NULL;
}
TNode(E value){
this->value = value;
this->leftChild = NULL;
this->rightChild = NULL;
}
};
template
class BTree{
private:
TNode* root;
/**
build and return the pointer to a binary tree for given array 'arr' and array size 'len'
*/
TNode build(E arr, int len){
TNode *subRoot = new TNode(arr[0]);
if (len == 1){
return subRoot; // terminating function - at the leaf
}
E* elementsToLeft = new E[len]; // array to store the left subtree children
E* elementsToRight = new E[len]; // array to store the right sub tree children
int counterLeft = 0;
int counterRight = 0;
for (int i = 1; i leftChild = build(elementsToLeft, counterLeft); // recursive call> array- left sub tree's children and size
subRoot->rightChild = build(elementsToRight, counterRight); // recursive call> array- right sub tree's children and size
return subRoot;
}
public:
/**
add the value specified by newvalueas the left child of the tree node specified by parent,if the node does not have a left childalready.
returns 1 if addition is successful and returns -1 in failure.
*/
template
int intaddLeftChild(TNode* parent, E newvalue){
if (parent->leftChild != NULL){ return -1; }
TNode *temp = new TNode(newvalue);
parent->leftChild = temp;
return 1;
}
Solution
Main points to improve:
-
Memory leaks: C++ is not garbage collected. Every
-
Unusual data initialization style in constructors:
The following is the correct way to initialize members. You should use the constructor initialization list. Also, prefixing the variables with a
Other tidbits:
-
Inside the body of a template class, such as in
-
Template parameter name for types is by convention called
-
-
Integer return codes are quite error prone. Returning a
-
You didn't follow your
-
Memory leaks: C++ is not garbage collected. Every
new must be deleted at some point. This is one of the reasons why C++ has destructors, so that we can perform this and other kinds of cleanup. In modern C++, smart pointers and standard containers are preferable, over manual memory management, since forgetting to delete a pointer is a very easy to make mistake, thus manual memory management being a major source of bugs and memory leaks. -
Unusual data initialization style in constructors:
TNode(){
this->value = NULL;
this->leftChild = NULL;
this->rightChild = NULL;
}The following is the correct way to initialize members. You should use the constructor initialization list. Also, prefixing the variables with a
this-> in something we rarely do in C++:TNode()
: value(NULL)
, leftChild(NULL)
, rightChild(NULL)
{ }Other tidbits:
-
Inside the body of a template class, such as in
TNode, you should omit the template parameter in the name. Writing TNode everywhere is too verbose and unnecessary.-
Template parameter name for types is by convention called
T instead of E.-
NULL is deprecated since C++11 in favor of the better nullptr.-
Integer return codes are quite error prone. Returning a
bool true or false, when the function has only two possible outcomes, is a lot nicer. -
You didn't follow your
camelCase notation in the create_tree() method.Code Snippets
TNode<E>(){
this->value = NULL;
this->leftChild = NULL;
this->rightChild = NULL;
}TNode()
: value(NULL)
, leftChild(NULL)
, rightChild(NULL)
{ }Context
StackExchange Code Review Q#78625, answer score: 3
Revisions (0)
No revisions yet.