Introduction
There are many times when we try to create a binary search tree of many different data types. This article explains creation of a template library CTree. template <class T>class CTree can be used to create a binary search tree (BST) of any data type.
Features
This class support all basic operations on the tree, other then basic operations it includes
bool TreeInorderWalk(CNode<T>*) ;
bool TreePreorderWalk(CNode<T>*) ;
bool TreePostorderWalk(CNode<T>*) ;
bool TreeInorderWalk(T & ) ;
bool TreePreorderWalk(T & ) ;
bool TreePostorderWalk(T & ) ;
These functions performs various walks on the tree and they prepare a lnk list of all the nodes in the order they incounter. Pointer of thses link list is a public data member of ths CTree class.
1 - CLinkList<T> *m_pListInorder ;
Pointer to the link list prepared by calling TreeInorderWalk.
2 - CLinkList<T> *m_pListPreorder ;
Pointer to the link list prepared by calling TreePreorderWalk.
3 - CLinkList<T> *m_pListPostorder ;
Pointer to the link list prepared by calling TreePostorderWalk.
This list further supports a number of operations like
bool AddToFirst(T &);
bool AddToLast(T &);
bool DeleteFirst();
bool DeleteLast();
bool GetNode(T &, CListNode<T> **) ;
How to use
using is class is very simple, just copy all the header files
Node.h
ListNode.h"
LinkList.h
Tree.h
Treelib.h
to your project folder and inlude TreeLib.h in your application. After this
create an intance of the class.
Example
CTree<int> objCTreeInt ; //creating an instance of class
CTree<float> objCTreeFloat ;
CTree<MyClass> objCTreeMyClass ;
Now use various methods of CTree for further processings.
Thses objects now support a nummber of methods of the class like
1 - bool AddRoot(T & );
2 - bool Insert(T & );
3 - bool Delete(T & );
4 - bool Search(T & );
5 - bool Minimum(T &, T & );
6 - bool Maximum(T &, T & );
7 - bool Successor(T &, T & );
8 - bool Predecessor(T &, T & );
9 - bool Parent(T &, T &) ;
10 - bool Left(T &, T &) ;
11 - bool Right(T &, T &) ;
12 - bool TreeInorderWalk(T & ) ;
13 - bool TreePreorderWalk(T & ) ;
14 - bool TreePostorderWalk(T & ) ;
15 - bool DeleteTree(T & ) ;
16 - bool Delete(CNode<T>* pNode );
17 - bool Search(T &obj, CNode<T> **pOut);
18 - bool Minimum(CNode<T>* , CNode<T>** );
19 - bool Maximum(CNode<T>* , CNode<T>** );
20 - bool Successor(CNode<T>* , CNode<T>** );
21 - bool Predecessor(CNode<T>* , CNode<T>** );
22 - CNode<T>* Parent(CNode<T>** pNode)
23 - CNode<T>* Left(CNode<T>** pNode)
24 - CNode<T>* Right(CNode<T>** pNode)
25 - bool TreeInorderWalk(CNode<T>*) ;
26 - bool TreePreorderWalk(CNode<T>*) ;
27 - bool TreePostorderWalk(CNode<T>*) ;
28 - bool DeleteTree(CNode<T>*) ;
In addition the link lists which can be prepared by using methods from 22 to 26 supprors methods like
1 - bool AddToFirst(T &);
2 - bool AddToLast(T &);
3 - bool DeleteFirst();
4 - bool DeleteLast();
5 - bool GetNode(T &, CListNode<T> **) ;
Constraints
While creating tree of any user defined class object one must over load '==', '=', '<' and '>' operators, in the class.
At Last
I have tested this for a number of opertaions, but if you find any kind of bug, feel free to inform me. Enjoy reading.
Prateek Kumar Dubey