Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void balancedDraw() {
- assignLevels();
- HashMap<GeometricTreeNode, Integer> map = new HashMap<GeometricTreeNode, Integer>();
- GeometricTreeNode u = r, prev = nil, next;
- int nodeTotal;
- while (u != nil) {
- nodeTotal = 0;
- if (prev == u.parent) {
- if (u.left != nil)
- next = u.left;
- else if (u.right != nil)
- next = u.right;
- else
- next = u.parent;
- } else if (prev == u.left) {
- if (u.right != nil)
- next = u.right;
- else
- next = u.parent;
- } else {
- next = u.parent;
- }
- if (next == u.parent) {
- if (u.left != nil)
- nodeTotal += map.get(u.left);
- if (u.right != nil)
- nodeTotal += map.get(u.right);
- map.put(u, nodeTotal + 1);
- }
- prev = u;
- u = next;
- }
- u = r;
- prev = nil;
- int x = 0, y = 0, maxx = 0;
- while (u != nil) {
- if (prev == u.parent) {
- u.position.x = x;
- u.position.y = y;
- if (u.left != nil && u.right != nil) {
- if (map.get(u.left) < map.get(u.right)) {
- next = u.left;
- } else
- next = u.right;
- y++;
- } else if (u.left == nil && u.right == nil) {
- next = u.parent;
- } else if (u.left == nil) {
- next = u.right;
- x = maxx + 1;
- maxx = x;
- } else {
- next = u.left;
- x = maxx + 1;
- maxx = x;
- }
- } else if (u.left != nil && u.right != nil) {
- if (map.get(u.left) < map.get(u.right)) {
- if (prev == u.left) {
- next = u.right;
- x = maxx + 1;
- maxx = x;
- } else {
- next = u.parent;
- }
- } else {
- if (prev == u.right) {
- next = u.left;
- x = maxx + 1;
- maxx = x;
- } else {
- next = u.parent;
- }
- }
- } else {
- next = u.parent;
- }
- if (next == u.parent) {
- if (u.parent != nil) {
- x = u.parent.position.x;
- y = u.parent.position.y;
- }
- }
- prev = u;
- u = next;
- }
- // System.out.println(map);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement