arraylist - Trying to print a binary tree vertically using HashMap in Java -


I am trying to print the binary tree vertically. I have used the hashmap with the horizontal distance of the node from the root and used it as the simplified list of nodes in the form of values. In accordance with the hardcode input in the main module, the tree looks like this:

  1/2 2 3 / / / 4 5 6 7   

Required output (when I print the hampapa entry):
[0 = [1,5, 6], 1 = [3], 2 = [7], -2 = [4], -1 = [2]]

Output given by the code given below:
[0 = [1, 6], 1 = [], 2 = [], -2 = [], -1 = []]

Can anyone explain to me the problem here?

  import java.util.ArrayList; Import java.util.HashMap; Public class TriDode {Private key key; Left the private tree node; Private Tree Node Correct; Public TreeNode (Anti Key) {Public key key}} Public key () {return key;} Public triode left () {left left}; Public triode right () {right;} Return to right} Public Zero Set Public keys set {Public key} {this.key = key;} Public Zero Set Lift (tree_node left) {this.left = left;} Public Zero Satrite (tree node right) {this.right = right;}} Public square tree { Private tree node root; Public Tree (TreeNode Route) {this.root = root; } Public Zero Vertical Print () Exception {HashMap & lt; Integer, ArrayList & lt; Integer & gt; & Gt; Hm; Hm = new hashmap & lt; & Gt; (); verticalPrintTree (root, 0, hm); Println (hm.entrySet ()); } Private Wide Vertical PrintTree (TreeNode Root, Int HD, Hashmap & LT; Integer, ALEIST & LT; integer & gt; & gt; HM) throws an exception {if (root == null) return; verticalPrintTree (root.left (), HD-1, Hm); If (hm.get (hd) == faucet) hm.put (hd, new arreist (root.k) ()); Else hm.get (hd) .add (root.key ()); verticalPrintTree (root.right (), HD + 1, Hm); } Public Zero PrintTree () {printInorder (Route); } Private Zero PrintInder (TriNode Route) {if (root == null) return; printInorder (root.left ()); Println (root.key ()); printInorder (root.right ()); }} Public square trivart print {Public static zero principal throws exception []; {TreeNode root = new triode (1); Root.setLeft (new tree node (2)); Root.setRight (New TreeNode (3)); root.left (). setLeft (new TreeNode (4)); Root.left (). SetRight (New TreeNode (5)); root.right (). setLeft (New TreeNode (6)); Root.right () SetRight (New TreeNode (7)); Tree t = new tree (root); T.printTree (); t.verticalPrint (); }} code    

) {return; } Vertical print line (root. Left (), HD-1, hm); If (hm.get (hd) == faucet) {System.out.println ("start element for index + + + + +" element "+ root.key ()); array list = new arreist () ; list.add (root.key ()); Hm.put (hd, list);} else {System.out.println ("add element" + root.key () + "for index" + HD); hm.get (HD) .add (root.key ());} vertical printtree (root. (right), hd + 1, hm);}

Specify a collection of items to be added or you specify the initial capacity, so you can do the wrong thing. Were referred to the need to create a collection and add that element then. Finally, you can store it in your hash.

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -