Template Version of Generic binary sorted Tree. : template class « Class « C++

C++
1. Bitset
2. Class
3. Console
4. Data Structure
5. Data Type
6. Deque
7. Development
8. File
9. Function
10. Generic
11. Language
12. List
13. Map Multimap
14. Overload
15. Pointer
16. Queue Stack
17. Set Multiset
18. STL Algorithms Binary search
19. STL Algorithms Heap
20. STL Algorithms Helper
21. STL Algorithms Iterator
22. STL Algorithms Merge
23. STL Algorithms Min Max
24. STL Algorithms Modifying sequence operations
25. STL Algorithms Non modifying sequence operations
26. STL Algorithms Sorting
27. STL Basics
28. String
29. Valarray
30. Vector
Java
XML
XML Tutorial
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
C++ » Class » template classScreenshots 
Template Version of Generic binary sorted Tree.
  
#include  <iostream>
#include  <string.h>
using namespace std;

//A generic binary sorted tree.
template <class T>  class gen_tree;     //forward decl
template <class T>
class bnode {
private:
   friend class gen_tree<T>;
   bnode<T>*  left;
   bnode<T>*  right;
   T          data;
   int        count;
   bnode(T d, bnode<T>* l, bnode<T>* r:
         data(d), left(l), right(r), count(1) { }
   void  print() const
      cout << data << " : " << count << '\t'}
};

template <class T>
class gen_tree {
public:
   gen_tree() { root = 0}
   void  insert(T d);
   T  find(T dconst return (find(root, d))}
   void  print() const print(root)}
private:
   bnode<T>*  root;
   T  find(bnode<T>* r, T dconst;
   void  print(bnode<T>* rconst;
};

template <class T>
void gen_tree<T>::insert(T d)
{
   bnode<T>*  temp = root;
   bnode<T>*  old;

   if (root == 0) {
      root = new bnode<T>(d, 00);
      return;
   }
   while (temp != 0) {
      old = temp;
      if (comp(temp -> data, d== 0) {
         (temp -> count)++;
         return;
      }
      if (comp(temp -> data, d0)
         temp = temp -> left;
      else
         temp = temp -> right;
   }
   if (comp(old -> data, d0)
      old -> left = new bnode<T>(d, 00);
   else
      old -> right = new bnode<T>(d, 00);
}

template <class T>
T gen_tree<T>::find(bnode<T>* r, T dconst
{
   if (r == 0)
      return 0;
   else if (comp(r -> data, d==  0)
      return (r -> data);
   else if (comp(r -> data, d0)
      return (findr -> left, d));
   else
      return (findr -> right, d));
}

template <class T>
void gen_tree<T>::print(bnode<T> *rconst
{
   if (r != 0) {
      printr -> left);
      r -> bnode<T>::print();
      print r -> right);
   }
}

template <class T>      //general case
int comp(T i, T j)
{
   if (i == j)          //assumes ==  < defined for T
      return 0;
   else
      return ( (i < j? -);
}

//specialization for const char*
int comp(const char* i, const char* j){
   return (strcmp(i, j));
}

int main()
{
   char             dat[256];
   gen_tree<char*>  t;
   char*            p;

   while (cin>>dat){
      p = new char[strlen(dat1];
      strcpy(p, dat);
      t.insert(p);
   }
   t.print();
   cout << "EOF" << endl << endl;

   gen_tree<int>  i_tree;

   for (int i = 15; i > -5; --i)
      i_tree.insert(i);
   i_tree.print();
}
  
    
  
Related examples in the same category
1. template class with type parameter
2. template class with generic parameter
3. Demonstrate a very simple safe pointer class.
4. A generic safe-array class that prevents array boundary errors.
5. Get storage off stack for array
6. template extending
7. sequence template
8. template class with two generic parameters
9. generic stack template class
10. Using exceptions with templates.
11. Passing by reference and using virtual functions in exceptions.
12. Using Non-Type Arguments with Generic Classes
13. Using Default Arguments with Template Classes
14. Generic Classes: demonstrates a generic stack.
15. An Example with Two Generic Data Types
16. Applying Template Classes: A Generic Array Class
17. Explicit Class Specializations for generic template class
w___w___w__._j___a__va2_s_.__co__m___ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.