59600

Method to Display a Splay Tree

Question:

I have built a splay tree and I am trying to print it out reverse in order so that when you turn your head to the left you can see the tree in a normal manner. I have written the following code and it outputs the tree somewhat correctly but it adds extra spaces in on the rightmost node and it doesn't add spaces for all of the child nodes that should be placed below the root node:

public void printReverseInOrder() { if (root != null) { reverseInOrder(root, 0); } else { System.out.println(); } } public void reverseInOrder(BSTnode h, int indent) { if (h != null) { for (int i = 0; i < indent; i++) { System.out.print(" "); } indent++; reverseInOrder(h.right, indent); reverseInOrder(h.left, indent); System.out.println(h.data); indent--; } }

I feel like it may be an error with either my recursion or the placement of my indent additions and subtractions.

Answer1:

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

Recommend

  • replacing first few digits with #
  • WPA does not see ETW event data, tracerpt does
  • Dynamic saving to PDF only certain Sheets in Excel VBA based on sheetname
  • Display Table-Cell: Remove Right and Left Border Space?
  • Updating and removing unique join relationships in CakePHP
  • Copying rows in a database when rows have children
  • Neo4j: Legacy Indexes and auto index vs new label bases schema indexes
  • Passing information to server-side function in a Google Docs Add On
  • How to add learning rate to summaries?
  • d3.js selection conditional rendering
  • Why doesnt this Java loop in a thread work?
  • If statement skipping right to else after being called once?
  • Validate jQuery plugin, field not required
  • In loopback documentation what does variable 'cb' stands for?
  • Simple linked list-C
  • Reading a file into a multidimensional array
  • CakePHP ACL tutorial initDB function warnings
  • JBoss External Properties Files in Classpath
  • App restarts from wrong activity
  • htaccess add www if not subdomain, if subdomain remove www
  • preg_replace Double Spaces to tab (\\t) at the beginning of a line
  • Jetty Server not starting: Unable to establish loopback connection
  • Avoid links criss cross / overlap in d3.js using force layout
  • Xamarin Forms - UWP Fonts
  • Jenkins: How To Build multiple projects from a TFS repository?
  • How to make a tree having multiple type of nodes and each node can have multiple child nodes in java
  • sending/ receiving email in Java
  • Timeout for blocking function call, i.e., how to stop waiting for user input after X seconds?
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Proper folder structure for lots of source files
  • JTable with a ScrollPane misbehaving
  • Angular 2 constructor injection vs direct access
  • Java static initializers and reflection
  • Android Google Maps API OnLocationChanged only called once
  • How does Linux kernel interrupt the application?
  • unknown Exception android
  • How can i traverse a binary tree from right to left in java?
  • failed to connect to specific WiFi in android programmatically
  • UserPrincipal.Current returns apppool on IIS
  • How can I use threading to 'tick' a timer to be accessed by other threads?