HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_Tree.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: UT_Tree.h (C++, Utility Library)
7  *
8  * COMMENTS:
9  *
10  * A simple templatized n-ary tree implementation using single pointers.
11  * To be extended as required by users of this class. (For example,
12  * you can't as yet remove nodes from it.)
13  *
14  * For slightly enhanced version of this, see UT_BidirectionalTree.
15  *
16  * - You must declare your tree class like so:
17  * class MyTreeType : public UT_Tree<MyTreeType>
18  * - The parent node will free its own children upon destruction so don't
19  * do things like adding pointers to items on the stack.
20  */
21 
22 #ifndef __UT_Tree_h__
23 #define __UT_Tree_h__
24 
25 #include <stdlib.h>
26 
27 
28 template <class T> class UT_Tree
29 {
30 public:
32  {
33  myLeft = NULL;
34  myRight = NULL;
35  }
36  virtual ~UT_Tree()
37  {
38  delete myLeft;
39  delete myRight;
40  }
41 
42  //
43  // Methods for using this as an n-ary tree
44  //
45  void addSibling( UT_Tree<T> *new_sibling )
46  {
47  if( myRight == NULL )
48  myRight = new_sibling;
49  else
50  myRight->addSibling( new_sibling );
51  }
52  void addChild( UT_Tree<T> *new_child )
53  {
54  if( myLeft == NULL )
55  myLeft = new_child;
56  else
57  myLeft->addSibling( new_child );
58  }
59  T *getFirstChild() { return (T *)myLeft; }
60  T *getNextSibling() { return (T *)myRight; }
61 
62  //
63  // Methods for using this as a binary tree
64  //
65  void setLeft( UT_Tree<T> *new_child ) { myLeft = new_child; }
66  void setRight( UT_Tree<T> *new_child ) { myRight = new_child; }
67  T *getLeft() { return (T *)myLeft; }
68  T *getRight() { return (T *)myRight; }
69 
70 private:
71  UT_Tree( const UT_Tree<T> &copy ); // not implemented yet
72  UT_Tree<T> &operator =( const UT_Tree<T> &copy );// not implemented yet
73 
74 private:
75  UT_Tree<T> * myLeft;
76  UT_Tree<T> * myRight;
77 };
78 
79 
80 #endif // __UT_Tree_h__
T * getLeft()
Definition: UT_Tree.h:67
OIIO_UTIL_API bool copy(string_view from, string_view to, std::string &err)
T * getFirstChild()
Definition: UT_Tree.h:59
void addSibling(UT_Tree< T > *new_sibling)
Definition: UT_Tree.h:45
T * getRight()
Definition: UT_Tree.h:68
UT_Tree()
Definition: UT_Tree.h:31
void setLeft(UT_Tree< T > *new_child)
Definition: UT_Tree.h:65
T * getNextSibling()
Definition: UT_Tree.h:60
void addChild(UT_Tree< T > *new_child)
Definition: UT_Tree.h:52
virtual ~UT_Tree()
Definition: UT_Tree.h:36
void setRight(UT_Tree< T > *new_child)
Definition: UT_Tree.h:66