Bridges-C++  3.4.5-dev1-6-g935685a
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 namespace bridges {
10  namespace datastructure {
29  template <typename K, typename E>
30  class BSTElement : public BinTreeElement<E> {
31  protected:
32  K key = K();// The search key value
33  public:
45  BSTElement(const K& k, BSTElement* l, BSTElement* r, const E& val = E(),
46  const string& lab = string())
47  : BinTreeElement<E>(l, r, val, lab), key(k) {
48  }
58  BSTElement(const K& k, const E& val = E(), const string& lab = string())
59  : BSTElement(k, nullptr, nullptr, val, lab) {
60  }
65  virtual const string getDStype() const override {
66  return "BinarySearchTree";
67  }
72  K getKey() const {
73  return key;
74  }
80  void setKey(const K& k) {
81  key = k;
82  }
87  virtual BSTElement* getLeft() override {
88  return static_cast<BSTElement*>(BinTreeElement<E>::getLeft());
89  }
95  virtual const BSTElement* getLeft() const override {
96  return static_cast<const BSTElement*>(BinTreeElement<E>::getLeft());
97  }
103  void setLeft(BSTElement* l) {
105  }
110  virtual BSTElement* getRight() override {
111  return static_cast<BSTElement*>(BinTreeElement<E>::getRight());
112  }
118  virtual const BSTElement* getRight() const override {
119  return static_cast<const BSTElement*>(BinTreeElement<E>::getRight());
120  }
126  void setRight(BSTElement* r) {
128  }
129  protected:
133  virtual const string getElementRepresentation() const override {
135 
136  string json = Element<E>::getElementRepresentation();
137  stringstream conv;
138  conv << key;
139  return json.insert(
140  json.size() - 1,
141  COMMA + QUOTE + "key" + QUOTE + COLON + JSONencode(conv.str())
142  );
143  }
144  }; //end of BSTElement class
145 
146  }
147 }//end of bridges namespace
148 #endif
This class can be used to create binary search tree elements, derived from BinTreeElement.
Definition: BSTElement.h:30
virtual const BSTElement * getRight() const override
Definition: BSTElement.h:118
void setKey(const K &k)
Definition: BSTElement.h:80
virtual const string getDStype() const override
Definition: BSTElement.h:65
void setLeft(BSTElement *l)
Definition: BSTElement.h:103
K getKey() const
Definition: BSTElement.h:72
virtual BSTElement * getRight() override
Definition: BSTElement.h:110
BSTElement(const K &k, const E &val=E(), const string &lab=string())
Definition: BSTElement.h:58
BSTElement(const K &k, BSTElement *l, BSTElement *r, const E &val=E(), const string &lab=string())
Definition: BSTElement.h:45
virtual const string getElementRepresentation() const override
Definition: BSTElement.h:133
void setRight(BSTElement *r)
Definition: BSTElement.h:126
virtual const BSTElement * getLeft() const override
Definition: BSTElement.h:95
K key
Definition: BSTElement.h:32
virtual BSTElement * getLeft() override
Definition: BSTElement.h:87
This class can be used to create binary tree elements, derived from TreeElement.
Definition: BinTreeElement.h:24
void setLeft(BinTreeElement *l)
Definition: BinTreeElement.h:84
void setRight(BinTreeElement *r)
Definition: BinTreeElement.h:109
virtual BinTreeElement * getLeft()
Definition: BinTreeElement.h:67
virtual BinTreeElement * getRight()
Definition: BinTreeElement.h:92
virtual const string getElementRepresentation() const
Gets the JSON string of the element representation.
Definition: Element.h:235
std::string JSONencode(const T &d)
Definition: JSONutil.h:38
Support for drawing Bar charts.
Definition: alltypes.h:4
const string COLON
Definition: DataStructure.h:52
const string COMMA
Definition: DataStructure.h:51
const string QUOTE
Definition: DataStructure.h:50