gtkIOStream  1.7.0
GTK+ << C++ IOStream operators for GTK+. Now with ORBing, numerical computation, audio client and more ...
BST.H
Go to the documentation of this file.
1 /* Copyright 2000-2018 Matt Flax <flatmax@flatmax.org>
2  This file is part of GTK+ IOStream class set
3 
4  GTK+ IOStream is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8 
9  GTK+ IOStream is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You have received a copy of the GNU General Public License
15  along with GTK+ IOStream
16  */
17 #ifndef BST_H
18 #define BST_H
19 
20 #include <stdlib.h>
21 #include <iostream>
22 using namespace std;
23 
24 #include "LinkList.H"
25 
26 #define BST_OK 0
27 #define BST_MALLOC_ERROR -1
28 
34 template<class BST_TYPE>
35 class BST{
36  BST *childL, *childR;
37  BST_TYPE *value;
38 public:
39 
41  typedef int(BST_TYPE::*BSTCompareMethod)(const BST_TYPE&)const;
42 
48  BST(BST_TYPE *v=NULL, BST* cL=NULL, BST* cR=NULL){
49  childL=cL;
50  childR=cR;
51  value=v;
52  }
53 
55  ~BST(void){
56  if (childL) delete childL;
57  childL=NULL;
58  if (childR) delete childR;
59  childR=NULL;
60  if (value) delete value;
61  value=NULL;
62  }
63 
70  int add(BST_TYPE *v, BSTCompareMethod compare=NULL){
71  if (value==NULL){ // handle the root node
72  value=v;
73  return BST_OK;
74  }
75  BST** child;
76  int comparison; // evaluate the comparison between v and value
77  if (compare!=NULL) // if supplied, then utilise
78  comparison=(*value.*compare)(*v);
79  else // otherwise default to v>value
80  comparison=(*v>*value);
81  if (comparison<0) // if *v>*value comparison will be negative
82  child=&childR;
83  else
84  child=&childL;
85  if (*child==NULL){ // if there is no child, then create one
86  *child=new BST<BST_TYPE>(v);
87  if (!*child){
88  cerr<<"couldn't create new BST child"<<endl;
89  return BST_MALLOC_ERROR;
90  }
91  } else // if there is a child, then ask the child to evaluate and store
92  return (*child)->add(v, compare);
93  return BST_OK;
94  }
95 
101  int sort(LinkList<BST_TYPE *> *linkList, BSTCompareMethod compare=NULL){
102  if (!linkList->getCount()) // return on the empty list
103  return BST_OK;
104  int res=BST_OK;
105  while (linkList->getCount()) // put each link into the BST
106  if ((res=add(linkList->remove(), compare))<0)
107  return res;
108  removeSorted(linkList); // remove in a sorted fashion
109  }
110 
116  if (childL){ // recursive removal of the left child.
117  childL->removeSorted(linkList); // remove the child and set to NULL
118  delete childL; childL=NULL;
119  }
120 
121  linkList->add(value); // add to the linked list
122  value=NULL;
123 
124  if (childR){ // recursive removal of the right child.
125  childR->removeSorted(linkList); // remove the child and set to NULL
126  delete childR; childR=NULL;
127  }
128 
129  }
130 
131 // BST* operator[](char which){
132 // if (which=='p')
133 // return parent;
134 // else if (which=='l')
135 // return childL;
136 // else if (which=='r')
137 // return childR;
138 // else {
139 // cerr<<"your algorithm must index either 'p' for parent, 'l' for left child, 'r' for right child"<<endl;
140 // exit(-1);
141 // }
142 // }
143 
144 // void dumpSorted(void){
145 // if (childL)
146 // childL->dumpSorted();
147 // cout<<value<<'\t';
148 // if (childR)
149 // childR->dumpSorted();
150 // }
151 //
152 // void dumpDepthFirst(void){
153 // cout<<value<<'\t';
154 // if (childL)
155 // childL->dumpDepthFirst();
156 // if (childR)
157 // childR->dumpDepthFirst();
158 // }
159 //
160 // void dumpPostOrder(void){
161 // if (childL)
162 // childL->dumpPostOrder();
163 // if (childR)
164 // childR->dumpPostOrder();
165 // cout<<value<<'\t';
166 // }
167 //
168 
169 };
170 #endif //BST_H
Definition: BST.H:35
BST_TYPE * value
The pointer to the variable.
Definition: BST.H:37
STL namespace.
int add(BST_TYPE *v, BSTCompareMethod compare=NULL)
Definition: BST.H:70
BST(BST_TYPE *v=NULL, BST *cL=NULL, BST *cR=NULL)
Definition: BST.H:48
#define BST_OK
Definition: BST.H:26
BST * childR
Left and right children nodes.
Definition: BST.H:36
#define BST_MALLOC_ERROR
Definition: BST.H:27
void removeSorted(LinkList< BST_TYPE *> *linkList)
Definition: BST.H:115
~BST(void)
Destructor - any resident variable or children are deleted.
Definition: BST.H:55
int sort(LinkList< BST_TYPE *> *linkList, BSTCompareMethod compare=NULL)
Definition: BST.H:101
gtkIOStream: /tmp/gtkiostream/include/mffm/BST.H Source File
GTK+ IOStream  Beta