Does Java have global variables?

How do you create a tree in java using only two variables (HELP)?

  • I really need help and no one is answering my questions. I created a variable for each node, but we are supposed to reuse two variables to create the whole tree. How do you do this? //Ilana Mannine //Tree implementation public class Part1 { private static class NSNode<E> { E data; NSNode<E> nextSib; NSNode<E> firstChild; public NSNode(E data,NSNode<E> nextSib,NSNode<E> firstChild) { this.data = data; this.nextSib = nextSib; this.firstChild = firstChild; } } private static void visit(NSNode node, int depth) { for (int i=0; i<depth; i++) System.out.print(" "); System.out.println(node.data); } private static void preorderTraverse(NSNode node, int depth) { visit(node, depth); preorderTraverseChildren(node, depth+1); } private static void preorderTraverseChildren(NSNode node, int depth) { NSNode n = node.firstChild; while (n != null) { preorderTraverse(n, depth); n = n.nextSib; } } private static void postorderTraverse(NSNode node, int depth) { postorderTraverseChildren(node, depth+1); visit(node, depth); } private static void postorderTraverseChildren(NSNode node, int depth) { NSNode n = node.firstChild; while (n != null) { postorderTraverse(n, depth); n = n.nextSib; } } public static void main(String[] arg) { NSNode<String> aaea=new NSNode<String>("AAEA",null,null); NSNode<String> aae=new NSNode<String>("AAE",null,aaea); NSNode<String> aad=new NSNode<String>("AAD",aae,null); NSNode<String> aac=new NSNode<String>("AAC",aad,null); NSNode<String> aabc=new NSNode<String>("AABC",null,null); NSNode<String> aabb=new NSNode<String>("AABB",aabc,null); NSNode<String> aaba=new NSNode<String>("AABA",aabb,null); NSNode<String> aab=new NSNode<String>("AAB",aac, aaba); NSNode<String> aaab=new NSNode<String>("AAAB",null,null); NSNode<String> aaaa=new NSNode<String>("AAAA",aaab,null); NSNode<String> aaa=new NSNode<String>("AAA",aab,aaaa); NSNode<String> ae =new NSNode<String>("AE", null,null); NSNode<String> adc=new NSNode<String>("ADC",null,null); NSNode<String> adb=new NSNode<String>("ADB",adc, null); NSNode<String> ada=new NSNode<String>("ADA",adb, null); NSNode<String> ad =new NSNode<String>("AD", ae, ada); NSNode<String> ac =new NSNode<String>("AC", ad, null); NSNode<String> ab =new NSNode<String>("AB", ac, null); NSNode<String> aa =new NSNode<String>("AA", ab, aaa); NSNode<String> a =new NSNode<String>("A", null,aa); System.out.printf("Preorder\n--------\n… preorderTraverse(a, 0); System.out.printf("\n\n---------------\… postorderTraverse(a, 0); System.out.println(); } }

  • Answer:

    The only pointers you need to store is a pointer to the root of the tree and a pointer to the current node you are looking at, call your two Node pointers root and current. You need to keep a root pointer so all of your nodes will be accessible. To traverse the tree you just use the current pointer. eg: current = root; current = current.left; current = current.right; for a preorder traversal its actually easiest to use a recursive method: void preorderTraversal(Node n) { System.out.println(n.data); if (n.left != null) preorderTraversal(n.left); if (n.right != null) preorderTraversal(n.right); }

Ilana M at Yahoo! Answers Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.