Advertisement
Guest User

Untitled

a guest
Aug 26th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 KB | None | 0 0
  1. #ifndef AUGMENTEDBALANCEDBINARYTREE_HEADER_INCLUDED
  2. #define AUGMENTEDBALANCEDBINARYTREE_HEADER_INCLUDED
  3.  
  4. #include <stdint.h>
  5. #include <set>
  6.  
  7. #pragma pack(1) // fuck you visual studio without this it will crash, to no fault of the code, but what appears to be a bug in VS
  8. // wikipedia: Balance factors can be kept up-to-date by knowing the previous balance factors and the change in height –
  9. // it is not necessary to know the absolute height. For holding the AVL balance information, two bits per node are sufficient.
  10. std::set<uint64_t> niggerSet;
  11. struct treeNode
  12. {
  13. treeNode()
  14. {
  15. headAddress = 0;
  16. tailAddress = 0;
  17. height = 0;
  18. leftChild = 0;
  19. rightChild = 0;
  20. tailInstructionLength = 0;
  21. }
  22.  
  23. ~treeNode()
  24. {
  25. std::set<uint64_t>::iterator it = niggerSet.find(headAddress);
  26. if (it != niggerSet.end())
  27. {
  28. int asdfasdf = 324532452354;
  29. }
  30.  
  31. niggerSet.insert(headAddress);
  32. }
  33. uint64_t headAddress;
  34. uint64_t tailAddress;
  35.  
  36. treeNode* leftChild;
  37. treeNode* rightChild;
  38.  
  39. uint16_t tailInstructionLength;
  40.  
  41. unsigned char height;
  42.  
  43. };
  44.  
  45. /*
  46. modified avl instead of red-black because we want faster lookup.
  47. a node will store a range (eg. 7-15) and will operate like a normal avl tree (19-23) goes to the right of (7-15); as there will be no range overlaps ever.
  48. nodes will automatically merge together if they "hug", to use the tree for general purpose ranges just set the tailInstructionLength to 1 so 5-12 and 13-200 will hug
  49. while not overlapping. Pointers are meaningless here, for example when you go to delete a node, instead you delete a range because nodes will merge and be deleted
  50. automatically behind the scenes, so that node pointer you have in the outside world may be meaningless, stay safe.
  51. */
  52. class rangedAVLTree
  53. {
  54. public:
  55. rangedAVLTree();
  56. ~rangedAVLTree();
  57.  
  58. // the node MAY be deleted, consider this function to insert the range into the tree, the internal tree may shift and merge nodes assume the pointer is trash after the call.
  59. // instead of merging nodes from inside, nodes will be flaged during insertion and then merging will take place outside to ensure tree balancing functions normally.
  60. void
  61. insertRange(treeNode* node);
  62.  
  63. // delete a range, just fill out the node and this will remove that range from the tree.
  64. void
  65. removeRange(treeNode* node);
  66.  
  67. void
  68. deleteTree();
  69.  
  70. void
  71. inOrder(treeNode* node, void(*func)(void*));
  72.  
  73. void
  74. preOrder(treeNode* node, void(*func)(void*));
  75.  
  76. void
  77. postOrder(treeNode* node, void(*func)(void*));
  78.  
  79. void
  80. breadthFirst(treeNode* node, void(*func)(void*));
  81.  
  82. uint32_t
  83. getTreeHeight();
  84.  
  85. treeNode*
  86. isValueInRange(uint64_t value);
  87.  
  88. treeNode* treeRoot;
  89.  
  90. uint64_t numberOfItems;
  91.  
  92. private:
  93. private:
  94. private:
  95.  
  96. unsigned char
  97. height(treeNode* node);
  98.  
  99. int
  100. balanceFactor(treeNode* node);
  101.  
  102. void
  103. fixHeight(treeNode* node);
  104.  
  105. treeNode*
  106. rotateRight(treeNode* node);
  107.  
  108. treeNode*
  109. rotateLeft(treeNode* node);
  110.  
  111. treeNode*
  112. balance(treeNode* node);
  113.  
  114. treeNode*
  115. findMin(treeNode* node);
  116.  
  117. treeNode*
  118. removeMin(treeNode* node);
  119.  
  120. // insert and remove return the root.
  121. treeNode*
  122. internalInsertRange(treeNode* root, treeNode* node);
  123.  
  124. void
  125. internalRemoveRange(treeNode* root, treeNode* node, treeNode** mergedNodes);
  126.  
  127. // this should be internal only because we dont know if nodes have been merged so hanging onto that tree pointer in the outside world and trying to remove it is dangerous.
  128. void
  129. remove(treeNode* node);
  130.  
  131. treeNode*
  132. internalRemove(treeNode* root, treeNode* node);
  133.  
  134. //void deleteNodes(treeNode* node);
  135. };
  136.  
  137.  
  138. /*
  139. 1
  140. 5
  141. 9
  142. 13
  143. ... space width from start of row to end (including the end)
  144.  
  145. left to begin of number 2 + 2 + 2.....
  146.  
  147. 5
  148. / \
  149. 3 6
  150. / \ / \
  151. 2 7 8
  152. / \ / \
  153. 1 2 5 9
  154. */
  155. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement