Bridges-C++  3.2.0
Bridges(C++API)
BSTElement.h
Go to the documentation of this file.
1 #ifndef BST_ELEMENT_H
2 #define BST_ELEMENT_H
3 
4 #include <sstream>
5 
6 #include "BinTreeElement.h" //string, sstream, using std
7 #include <JSONutil.h>
8 
9 
10 namespace bridges {
11  namespace datastructure {
30  template <typename K, typename E>
31  class BSTElement : public BinTreeElement<E> {
32  protected:
33  K key = K();// The search key value
34  public:
46  BSTElement(const K& k, BSTElement* l, BSTElement* r, const E& val = E(),
47  const string& lab = string())
48  : BinTreeElement<E>(l, r, val, lab), key(k) {
49  }
59  BSTElement(const K& k, const E& val = E(), const string& lab = string())
60  : BSTElement(k, nullptr, nullptr, val, lab) {
61  }
66  virtual const string getDStype() const override {
67  return "BinarySearchTree";
68  }
73  K getKey() const {
74  return key;
75  }
81  void setKey(const K& k) {
82  key = k;
83  }
88  virtual BSTElement* getLeft() override {
89  return static_cast<BSTElement*>(BinTreeElement<E>::getLeft());
90  }
96  virtual const BSTElement* getLeft() const override {
97  return static_cast<const BSTElement*>(BinTreeElement<E>::getLeft());
98  }
104  void setLeft(BSTElement* l) {
106  }
111  virtual BSTElement* getRight() override {
112  return static_cast<BSTElement*>(BinTreeElement<E>::getRight());
113  }
119  virtual const BSTElement* getRight() const override {
120  return static_cast<const BSTElement*>(BinTreeElement<E>::getRight());
121  }
127  void setRight(BSTElement* r) {
129  }
130  protected:
134  virtual const string getElementRepresentation() const override {
136 
137  string json = Element<E>::getElementRepresentation();
138  stringstream conv;
139  conv << key;
140  return json.insert(
141  json.size() - 1,
142  COMMA + QUOTE + "key" + QUOTE + COLON + JSONencode(conv.str())
143  );
144  }
145  }; //end of BSTElement class
146 
147  }
148 }//end of bridges namespace
149 #endif
This class can be used to create binary search tree elements, derived from BinTreeElement.
Definition: BSTElement.h:31
virtual BinTreeElement * getLeft()
Definition: BinTreeElement.h:67
virtual BSTElement * getRight() override
Definition: BSTElement.h:111
void setKey(const K &k)
Definition: BSTElement.h:81
virtual const BSTElement * getLeft() const override
Definition: BSTElement.h:96
BSTElement(const K &k, const E &val=E(), const string &lab=string())
Definition: BSTElement.h:59
const string COLON
Definition: DataStructure.h:51
virtual const BSTElement * getRight() const override
Definition: BSTElement.h:119
void setLeft(BSTElement *l)
Definition: BSTElement.h:104
K getKey() const
Definition: BSTElement.h:73
BSTElement(const K &k, BSTElement *l, BSTElement *r, const E &val=E(), const string &lab=string())
Definition: BSTElement.h:46
void setLeft(BinTreeElement *l)
Definition: BinTreeElement.h:84
virtual const string getElementRepresentation() const
Gets the JSON string of the element representation.
Definition: Element.h:238
virtual BSTElement * getLeft() override
Definition: BSTElement.h:88
virtual const string getDStype() const override
Definition: BSTElement.h:66
these methods convert byte arrays in to base64 codes and are used in BRIDGES to represent the color a...
Definition: alltypes.h:4
virtual BinTreeElement * getRight()
Definition: BinTreeElement.h:92
virtual const string getElementRepresentation() const override
Definition: BSTElement.h:134
This class can be used to create binary tree elements, derived from TreeElement.
Definition: BinTreeElement.h:24
const string COMMA
Definition: DataStructure.h:50
const string QUOTE
Definition: DataStructure.h:49
K key
Definition: BSTElement.h:33
void setRight(BSTElement *r)
Definition: BSTElement.h:127
void setRight(BinTreeElement *r)
Definition: BinTreeElement.h:109
std::string JSONencode(const T &d)
Definition: JSONutil.h:37