EPICS ARCHIVER V4
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups
BinaryTree.h
1 // -*- c++ -*-
2 // $Id: BinaryTree.h,v 1.6 2006/06/05 13:51:17 kasemir Exp $
3 //
4 // Please refer to NOTICE.txt,
5 // included as part of this distribution,
6 // for legal information.
7 
8 #ifndef __BINARY_TREE_H__
9 #define __BINARY_TREE_H__
10 
11 // tools
12 #include "tools/GenericException.h"
13 #include "tools/NoCopy.h"
14 
17 
20 template<class Item> class BinaryTreeItem {
21 public:
22 
25  _left = 0;
26  _right = 0;
27  }
28 
29  Item _item;
32 
33 private:
34 
35  PROHIBIT_DEFAULT_COPY(BinaryTreeItem);
36 
37 };
38 
39 
42 template<class Item> class BinaryTree {
43 public:
44 
45  BinaryTree()
46  { _root = 0; }
47 
48  ~BinaryTree()
49  { cleanup(_root); }
50 
52  void add(const Item &item) {
53 
55  try {
56  n = new BinaryTreeItem<Item>;
57  } catch (...) {
58  throw GenericException(__FILE__, __LINE__,
59  "Cannot allocate new Item");
60  }
61 
62  n->_item = item;
63  n->_left = 0;
64  n->_right = 0;
65  insert(n, &_root);
66  }
67 
71  bool find(const Item &item) {
72 
73  BinaryTreeItem<Item> *n = _root;
74 
75  while (n) {
76  if (item == n->_item) return true;
77 
78  if (item < n->_item) {
79  n = n->_left;
80  } else {
81  n = n->_right;
82  }
83  }
84  return false;
85  }
86 
88  void traverse(void (*visit) (const Item &, void *), void *arg=0) {
89  visit_inorder (visit, _root, arg);
90  }
91 
92 private:
93 
94  PROHIBIT_DEFAULT_COPY(BinaryTree);
95 
96  BinaryTreeItem<Item> *_root;
97 
98  void insert(BinaryTreeItem<Item> *new_item, BinaryTreeItem<Item> **node) {
99 
100  if (*node == 0){
101  *node = new_item;
102  } else if (new_item->_item < (*node)->_item) {
103  insert(new_item, & ((*node)->_left));
104  } else {
105  insert(new_item, & ((*node)->_right));
106  }
107  }
108 
109  void visit_inorder(void (*visit) (const Item &, void *),
110  BinaryTreeItem<Item> *node, void *arg) {
111  if (node) {
112  visit_inorder(visit, node->_left, arg);
113  visit(node->_item, arg);
114  visit_inorder(visit, node->_right, arg);
115  }
116  }
117 
118  void cleanup(BinaryTreeItem<Item> *node) {
119  if (node){
120  cleanup(node->_left);
121  cleanup(node->_right);
122  delete node;
123  }
124  }
125 };
126 
127 
129 
130 #endif
131 
bool find(const Item &item)
Try to find item in tree.
Definition: BinaryTree.h:71
Binary tree item.
Definition: BinaryTree.h:20
BinaryTreeItem * _left
left pointer
Definition: BinaryTree.h:30
Item _item
item
Definition: BinaryTree.h:29
BinaryTreeItem * _right
right pointer
Definition: BinaryTree.h:31
void traverse(void(*visit)(const Item &, void *), void *arg=0)
Sorted (inorder) traverse, calling visitor routine on each item.
Definition: BinaryTree.h:88
void add(const Item &item)
Add (copy) Item into the tree.
Definition: BinaryTree.h:52
Generic Exception: Base class for exceptions.
Definition: GenericException.h:45
BinaryTreeItem()
Constructor.
Definition: BinaryTree.h:24
Sorted binary tree for Items that support the &quot;less than&quot; and &quot;equal&quot; operators.
Definition: BinaryTree.h:42