Elm  2
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
BinTree.h
1 /*
2  * $Id$
3  * Copyright (c) 2004, Alfheim Corporation.
4  *
5  * BinTree.h -- binary interface.
6  */
7 #ifndef ELM_INHSTRUCT_BINTREE_H
8 #define ELM_INHSTRUCT_BINTREE_H
9 
10 namespace elm { namespace inhstruct {
11 
12 // BinTree class
13 class BinTree {
14 public:
15  // Node class
16  class Node {
17  Node *l, *r;
18  public:
19  inline Node(void);
20  inline Node(Node *left, Node *right);
21  inline Node *left(void) const;
22  inline Node *right(void) const;
23  inline void insertLeft(Node *node);
24  inline void insertRight(Node *node);
25  };
26 
27  // Visitor class
28  class Visitor {
29  public:
30  virtual ~Visitor(void) { }
31  virtual int process(Node *node) = 0;
32  };
33 
34 private:
35  Node *_root;
36  int count(Node *node) const;
37  bool contains(Node *current, Node *node) const;
38  bool visit(Visitor *visitor, Node *node) const;
39  bool visitPreOrder(Visitor *visitor, Node *node) const;
40  bool visitPostOrder(Visitor *visitor, Node *node) const;
41  void search(Visitor *visitor, Node *node) const;
42  void clear(Node *node);
43 
44 public:
45  inline BinTree(void);
46  inline bool isEmpty(void) const;
47  inline bool contains(Node *node);
48  inline int count(void) const;
49  inline Node *root(void) const;
50  inline void setRoot(Node *node);
51  inline void visit(Visitor *visitor) const;
52  inline void visitPreOrder(Visitor *visitor) const;
53  inline void visitPostOrder(Visitor *visitor) const;
54  void search(Visitor *visitor) const;
55  inline void clear(void);
56 };
57 
58 
59 // BinTree::Node inlines
61  l = node;
62 }
64  r = node;
65 }
66 inline BinTree::Node::Node(void): l(0), r(0) {
67 }
69 : l(left), r(right) {
70 }
71 inline BinTree::Node *BinTree::Node::left(void) const {
72  return l;
73 }
74 inline BinTree::Node *BinTree::Node::right(void) const {
75  return r;
76 }
77 
78 // BinTree inlines
79 inline BinTree::BinTree(void): _root(0) {
80 }
81 inline bool BinTree::isEmpty(void) const {
82  return _root == 0;
83 }
84 inline bool BinTree::contains(Node *node) {
85  return contains(_root, node);
86 }
87 inline int BinTree::count(void) const {
88  return count(_root);
89 }
90 inline BinTree::Node *BinTree::root(void) const {
91  return _root;
92 }
93 inline void BinTree::setRoot(BinTree::Node *node) {
94  _root = node;
95 }
96 inline void BinTree::visit(BinTree::Visitor *visitor) const {
97  visit(visitor, _root);
98 }
99 inline void BinTree::visitPreOrder(BinTree::Visitor *visitor) const {
100  visitPreOrder(visitor, _root);
101 }
102 inline void BinTree::visitPostOrder(BinTree::Visitor *visitor) const {
103  visitPostOrder(visitor, _root);
104 }
105 
106 inline void BinTree::clear(void) {
107  clear(_root);
108 }
109 
110 } } // elm::inhstruct
111 
112 #endif // ELM_INHSTRUCT_BINTREE_H
elm::inhstruct::BinTree::Node::Node
Node(void)
Definition: BinTree.h:66
elm::inhstruct::BinTree::Visitor::process
virtual int process(Node *node)=0
elm::inhstruct::BinTree
Definition: BinTree.h:13
elm::inhstruct::BinTree::Node::left
Node * left(void) const
Definition: BinTree.h:71
elm::inhstruct::BinTree::Node::insertRight
void insertRight(Node *node)
Definition: BinTree.h:63
elm::inhstruct::BinTree::BinTree
BinTree(void)
Definition: BinTree.h:79
elm::inhstruct::BinTree::root
Node * root(void) const
Definition: BinTree.h:90
elm::inhstruct::BinTree::clear
void clear(void)
Definition: BinTree.h:106
elm
Definition: adapter.h:26
elm::inhstruct::BinTree::Node::right
Node * right(void) const
Definition: BinTree.h:74
elm::inhstruct::BinTree::isEmpty
bool isEmpty(void) const
Definition: BinTree.h:81
elm::inhstruct::BinTree::Visitor::~Visitor
virtual ~Visitor(void)
Definition: BinTree.h:30
elm::inhstruct::BinTree::Node::insertLeft
void insertLeft(Node *node)
Definition: BinTree.h:60
elm::inhstruct::BinTree::Node
Definition: BinTree.h:16
elm::inhstruct::BinTree::Visitor
Definition: BinTree.h:28
elm::io::right
IntFormat right(IntFormat fmt)
Definition: Output.h:264
elm::io::left
IntFormat left(IntFormat fmt)
Definition: Output.h:263
elm::inhstruct::BinTree::setRoot
void setRoot(Node *node)
Definition: BinTree.h:93
elm::inhstruct::BinTree::count
int count(void) const
Definition: BinTree.h:87