Click here to Skip to main content
6,635,160 members and growing! (15,311 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » Libraries » General     Beginner

Binary Search Tree Template

By Prateek Kumar Dubey

This is a class library, it can be used to create binary search tree of any basic data type or a class object.
VC7.1WinXP, Visual Studio, MFC, Dev
Posted:18 May 2006
Views:32,301
Bookmarked:16 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
12 votes for this article.
Popularity: 3.86 Rating: 3.58 out of 5
1 vote, 8.3%
1
1 vote, 8.3%
2
1 vote, 8.3%
3

4
9 votes, 75.0%
5

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.

Using the code

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 objCTreeInt ; //creating an instance of class

CTree<FLOAT> objCTreeFloat ;
CTree<MYCLASS> objCTreeMyClass ;

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> **) ;

Constraints

While creating tree of any user defined class object one must over load '==', '=', '<' and '>' operators, in the class, snd should keep a copy constuctor.

Methods and Explanations

1 - bool AddRoot(T & );
Adds the root element, only if root is not present.
2 - bool Insert(T & );
Inserts an element in the tree, can insert root if not present.
3 - bool Delete(T & );
Deletes an element from the tree.
4 - bool Search(T & );
Searches for an element.
5 - bool Minimum(T &, T & );
Searches fro minimum of the tree or a sub tree, pass content of the root in IN prameter (see code) to find minimum of the tree.
6 - bool Maximum(T &, T & );
counterpart of Minimum.
7 - bool Successor(T &, T & );
Searches fro Successor of the tree or a sub tree, pass content of the root in IN prameter (see code) to find Successor of the tree.
8 - bool Predecessor(T &, T & );
Counterpart of Successor.
9 - bool Parent(T &, T &) ;
Searches for parent of a node in the tree.
10 - bool Left(T &, T &) ;
Searches for left child of a node in the tree.
11 - bool Right(T &, T &) ;
Searches for right child of a node in the tree.
12 - bool TreeInorderWalk(T & ) ;
Performs In Order Tree walk of the tree or a sub tree, and prepares a link list of all the nodes it incounters. Pass content of root to traverse complete tree.
13 - bool TreePreorderWalk(T & ) ;
Performs Pre Order Tree walk of the tree or a sub tree. and prepares a link list of all the nodes it incounters. Pass content of root to traverse complete tree.
14 - bool TreePostorderWalk(T & ) ;
Private function:: Performs Post Order Tree walk of the tree or a sub tree. and prepares a link list of all the nodes it incounters. Pass content of root to traverse complete tree.
15 - bool DeleteTree(T & ) ;
Deletes a tree or a sub tree. pass content of the root to delete complete tree.
16 - bool Delete(CNode<T>* pNode );
Deletes a node in the tree.
17 - bool Search(T &obj, CNode<T> **pOut);
Searches for a node in the tree, and returns its pointer in the out parameter)see code).
18 - bool Minimum(CNode<T>* , CNode<T>** );
Searches fro minimum of the tree or a sub tree, pass pointer to the root in IN prameter (see code) to find minimum of the tree. Minmum is returned in OUT paramenter (see code).
19 - bool Maximum(CNode<T>* , CNode<T>** );
Counterpart of minimum.
20 - bool Successor(CNode<T>* , CNode<T>** );
Searches fro Successor of the node , pass pointer to the root in IN prameter (see code) to find Successor of the tree. Successor is returned in OUT paramenter (see code).
21 - bool Predecessor(CNode<T>* , CNode<T>** );
Counterpart of the Successor.
22 - CNode<T>* Parent(CNode<T>** pNode)
Returns pointer to the Parent of a node.
23 - CNode<T>* Left(CNode<T>** pNode)
Returns pointer to the Left child of a node.
24 - CNode<T>* Right(CNode<T>** pNode)
Returns pointer to the Right child of a node.
25 - bool TreeInorderWalk(CNode<T>*) ;
Performs In Order Tree walk of the tree or a sub tree, and prepares a link list of all the nodes it incounters. Pass pointer to root to traverse complete tree.
26 - bool TreePreorderWalk(CNode<T>*) ;
Performs Pre Order Tree walk of the tree or a sub tree, and prepares a link list of all the nodes it incounters. Pass pointer to root to traverse complete tree.
27 - bool TreePostorderWalk(CNode<T>*) ;
Performs Post Order Tree walk of the tree or a sub tree, and prepares a link list of all the nodes it incounters. Pass pointer to root to traverse complete tree.
28 - bool DeleteTree(CNode<T>*) ;
Deletes a tree or a sub tree. pass pointer to the root to delete complete tree.
In addition the link lists which can be prepared by using methods from 22 to 26 supprors methods like
1 - bool AddToFirst(T &);
Add a node in bigning of the link list list.
2 - bool AddToLast(T &);
Add a node at last of the link list list.
3 - bool DeleteFirst();
Deletes a node from bigning of the link list list.
4 - bool DeleteLast();
Deletes a node from last of the link list list.
5 - bool GetNode(T &, CListNode<T> **) ;
Retrive pointer to a node.

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.

Auther

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Prateek Kumar Dubey


Member
I am currently working with Microsoft at Bangalore (India). My interest lies in areas of generic C++ and windows development. Apart from office hours I try to develop new and useful small tools.
Well, I still feel that I need to be more serious..!
Smile
Occupation: Software Developer (Senior)
Company: Microsoft
Location: India India

Other popular Libraries articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 3 of 3 (Total in Forum: 3) (Refresh)FirstPrevNext
Generalexapanded code using string PinmemberSadru21:13 23 Feb '09  
GeneralTHIS is a bad written article, but... PinmemberKochise3:09 18 May '06  
GeneralRe: THIS is a bad written article, but... PinmemberRage3:15 18 May '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 18 May 2006
Editor:
Copyright 2006 by Prateek Kumar Dubey
Everything else Copyright © CodeProject, 1999-2009
Web22 | Advertise on the Code Project