00001 #ifndef __AVL_TREE_H__
00002 #define __AVL_TREE_H__
00003
00004 #include <stdlib.h>
00005 #include <stdint.h>
00006 namespace bilateral
00007 {
00008
00009 class Node
00010 {
00011 public:
00012
00013 Node(int64_t value, int64_t squared, int64_t cubed);
00014 ~Node();
00015
00016
00017
00018 bool Insert(Node *node, Node **root);
00019 bool Delete(Node *to_go, Node **root);
00020 bool Remove(Node **root);
00021 Node *Find(int64_t);
00022
00023 void GetTotals(int64_t min, int64_t max,
00024 int64_t &total_values,
00025 int64_t &total_squares,
00026 int64_t &total_cubes,
00027 uint32_t &total_count);
00028
00029 void UpdateTotals(bool);
00030
00031 static bool RotateLeft(Node *);
00032 static bool RotateRight(Node *);
00033 static bool DoubleRotateLeft(Node *);
00034 static bool DoubleRotateRight(Node *);
00035
00036
00037 Node *GetLeft(void) const;
00038 Node *GetRight(void) const;
00039
00040 int64_t GetValue(void) const;
00041 int64_t GetSquareValue(void) const;
00042 int64_t GetCubeValue(void) const;
00043 uint32_t GetCount(void) const;
00044 void SetCount(uint32_t);
00045
00046 int64_t GetTotalValue(void) const;
00047 int64_t GetTotalSquareValue(void) const;
00048 int64_t GetTotalCubeValue(void) const;
00049 uint32_t GetTotalCount(void) const;
00050
00051
00052 void Dump(void);
00053 void Dump(int);
00054 bool Check(void);
00055
00056 protected:
00057
00058 int32_t GetBalance(void) const;
00059 void SetBalance(int32_t);
00060
00061 void ExcludeMin(int64_t min,
00062 int64_t &total_values,
00063 int64_t &total_squares,
00064 int64_t &total_cubes,
00065 uint32_t &total_count);
00066
00067 void ExcludeMax(int64_t min,
00068 int64_t &total_values,
00069 int64_t &total_squares,
00070 int64_t &total_cubes,
00071 uint32_t &total_count);
00072
00073 void SetLeft(Node *);
00074 void SetRight(Node *);
00075 void SetParent(Node *);
00076
00077 Node *GetParent(void) const;
00078
00079 Node *FindRoot(void);
00080 Node *FindMax(void);
00081 Node *FindMin(void);
00082
00083 bool Rebalance(Node **root);
00084 private:
00085 Node *m_left, *m_right, *m_parent;
00086 int32_t m_balance;
00087
00088 int64_t m_value, m_square_value, m_cube_value;
00089 int64_t m_total_value, m_total_square_value, m_total_cube_value;
00090 uint32_t m_total_count;
00091 uint32_t m_count;
00092 };
00093
00094 }
00095 #endif
00096