Template Class Binary Tree In C
A binary tree is a hierarchical data structure whose behavior is similar to a tree, as it contains root and leaves (a node that has no child).The root of a binary tree is the topmost node.Each node can have at most two children, which are referred to as the left child and the right child.A node that has at least one child becomes a parent of its child.
#include<iostream> |
#include<cstdio> |
#include<cstdlib> |
#include<string.h> |
#include<vector> |
#include<string> |
#include<cmath> |
#include<stack> |
#include<map> |
#include<algorithm> |
usingnamespacestd; |
#definedebug(args..) dbg(),args |
#defineFOR(A,B,C) for(int A=B;A<C;++A) |
#defineRFOR(A,B,C) for(int A=B;A>=C;--A) |
#definePB(A,B) A.push_back(B); |
#defineMEM(A,B) memset(A,B,sizeof(A)) |
bool DBG; |
structdbg |
{ |
template<typename T> dbg& operator , (const T& v) { |
if (DBG) |
cerr << v << ''; return *this; |
} |
~dbg() { |
if (DBG) |
cerr << endl; |
} |
}; |
/** |
* Main code starts here |
*/ |
template<classT> |
classBinaryTree |
{ |
public: |
/** |
* Default Constructor |
*/ |
BinaryTree() |
{ |
root = NULL; |
} |
/** |
* This method inserts a given node into the |
* binary search tree |
*/ |
voidinsert(T data) |
{ |
//makeTree(data, &root); |
nonrecurseMakeTree(data); |
} |
/** |
* Prints the tree inorder using the recursive |
* or non recursive version |
*/ |
voidprintTree(bool choice) |
{ |
if (choice) { |
recurseInorderTrav(root); |
} else { |
nonrecurseInorderTrav(root); |
} |
} |
private: |
/** |
* Struture to hold the nodes of the tree |
*/ |
structnode |
{ |
T info; |
node *left, *right; |
} *root; |
/** |
* Builds the tree using recursion |
*/ |
voidmakeTree(T data, node **root) |
{ |
if (*root NULL) { |
*root = new node; |
(*root)->info = data; |
(*root)->left = NULL; |
(*root)->right = NULL; |
return; |
} |
else { |
if ((*root)->info >= data) { |
makeTree(data, &(*root)->left); |
} else { |
makeTree(data, &(*root)->right); |
} |
} |
} |
/** |
* Non recursive version of the makeTree |
*/ |
voidnonrecurseMakeTree(T data) |
{ |
node *temp = new node; |
temp->info = data; |
temp->left = temp->right = NULL; |
if (root NULL) { |
root = temp; |
} else { |
node *p = root, *q; |
while (p != NULL) { |
q = p; |
if (p->info >= data) { |
p = p->left; |
} else { |
p = p->right; |
} |
} |
if (q->info >= data) { |
q->left = temp; |
} else { |
q->right = temp; |
} |
} |
} |
/** |
* This is the non recursive version of the |
* inorder tree traversal using the stack |
*/ |
voidnonrecurseInorderTrav(node *root) |
{ |
stack<node*> s; |
node *p = root; |
do { |
while (p != NULL) { |
s.push(p); |
p = p->left; |
} |
if (!s.empty()) { |
p = s.top(); |
s.pop(); |
cout << p->info << ''; |
p = p->right; |
} |
} while (!s.empty() p != NULL); |
} |
/** |
* This is the inorder tree traversal using recursion |
*/ |
voidrecurseInorderTrav(node *root) |
{ |
if (root != NULL) { |
recurseInorderTrav(root->left); |
cout << root->info << ''; |
recurseInorderTrav(root->right); |
} |
} |
/** |
* Using operator overloading for >= |
* used when comparing info part of the node. |
*/ |
T operator>=(const T& other) |
{ |
return (this >= other); |
} |
}; |
The default constructor is incorrect. You create a local variable (temp) inside the constructor that will be destructed at the end of the function, causing the allocated memory to be inaccessible (memory leak).Furthermore, inside the ctor you don't have a stack object to begin with, so it's impossible to push the tree into it, unless you create it locally (but then it would be destructed at the end of the function).just wondering if the '.this' assignment would create a treeIt will most likely cause a compilation error. Let's say you have a BinaryTree, then you create a node with a int data member and try to assign a BinaryTree to an int variable.
Factory Training Repair Manual For Landini Vision 80-90-100. Landini vision 100 tractor manual.