Advertisement
Guest User

Untitled

a guest
Nov 24th, 2014
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. public void balancedDraw() {
  2. assignLevels();
  3. HashMap<GeometricTreeNode, Integer> map = new HashMap<GeometricTreeNode, Integer>();
  4. GeometricTreeNode u = r, prev = nil, next;
  5. int nodeTotal;
  6. while (u != nil) {
  7. nodeTotal = 0;
  8. if (prev == u.parent) {
  9. if (u.left != nil)
  10. next = u.left;
  11. else if (u.right != nil)
  12. next = u.right;
  13. else
  14. next = u.parent;
  15. } else if (prev == u.left) {
  16. if (u.right != nil)
  17. next = u.right;
  18. else
  19. next = u.parent;
  20. } else {
  21. next = u.parent;
  22. }
  23.  
  24. if (next == u.parent) {
  25. if (u.left != nil)
  26. nodeTotal += map.get(u.left);
  27. if (u.right != nil)
  28. nodeTotal += map.get(u.right);
  29. map.put(u, nodeTotal + 1);
  30. }
  31.  
  32. prev = u;
  33. u = next;
  34. }
  35.  
  36. u = r;
  37. prev = nil;
  38. int x = 0, y = 0, maxx = 0;
  39. while (u != nil) {
  40. if (prev == u.parent) {
  41. u.position.x = x;
  42. u.position.y = y;
  43. if (u.left != nil && u.right != nil) {
  44. if (map.get(u.left) < map.get(u.right)) {
  45. next = u.left;
  46. } else
  47. next = u.right;
  48. y++;
  49. } else if (u.left == nil && u.right == nil) {
  50. next = u.parent;
  51. } else if (u.left == nil) {
  52. next = u.right;
  53. x = maxx + 1;
  54. maxx = x;
  55. } else {
  56. next = u.left;
  57. x = maxx + 1;
  58. maxx = x;
  59. }
  60.  
  61. } else if (u.left != nil && u.right != nil) {
  62. if (map.get(u.left) < map.get(u.right)) {
  63. if (prev == u.left) {
  64. next = u.right;
  65. x = maxx + 1;
  66. maxx = x;
  67. } else {
  68. next = u.parent;
  69. }
  70. } else {
  71. if (prev == u.right) {
  72. next = u.left;
  73. x = maxx + 1;
  74. maxx = x;
  75. } else {
  76. next = u.parent;
  77. }
  78. }
  79. } else {
  80. next = u.parent;
  81. }
  82.  
  83. if (next == u.parent) {
  84. if (u.parent != nil) {
  85. x = u.parent.position.x;
  86. y = u.parent.position.y;
  87. }
  88. }
  89.  
  90. prev = u;
  91. u = next;
  92. }
  93.  
  94. // System.out.println(map);
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement