Method to Display a Splay Tree

心已入冬 提交于 2019-12-07 16:03:26

This works pretty well, reordered a few things...

public class test {

   public static void main(String[] args){
      node rootNode = new node(5);
      rootNode.r = new node(4);
      rootNode.l = new node(3);
      rootNode.r.r = new node(2);
      rootNode.r.l = new node(1);
      rootNode.l.r = new node(6);
      rootNode.l.l = new node(7);

      reverseInOrder(rootNode, 0);
   }

   public static void reverseInOrder(node h, int indent) { 
      if (h != null) {
         indent++;
         reverseInOrder(h.r, indent);

         for (int i = 0; i < indent; i++) {
            System.out.print("  ");
         }
         System.out.println(h.value);

         reverseInOrder(h.l, indent);
      }
   }
}

your indent-- at the end of your call isn't really doing anything since the function ends and it jumps back up. Also as indent increases the spacing is actually going up exponentially in your example code (because it prints spaces on entering each time so 1space + 2 space + 3 space), I just changed it to add the spaces only right before it prints the value itself (so it is always equal to indent itself instead of indent factorial).

output looks like this:

      2
    4
      1
  5
      6
    3
      7
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!