Ask your own question, for FREE!
Computer Science 19 Online
OpenStudy (anonymous):

Anyone can help to find what the error with this binary tree and how to fixed it.

OpenStudy (anonymous):

//tree.java //demonstrates binary tree //to run this program: C>java TreeApp import java.io.*; import java.util.*; // for Stack class //////////////////////////////////////////////////////////////// class Node { public char iData; public double dData; // data item public Node leftChild; // this node's left child public Node rightChild; // this node's right child public void displayNode() { System.out.print('{'); System.out.print(iData); System.out.print(", "); System.out.print(dData); System.out.print("} "); } } // end class Node //////////////////////////////////////////////////////////////// class Tree { public Node root; // first node of tree //------------------------------------------------------------- public Tree() { root = null; } // no nodes in tree yet //------------------------------------------------------------- public void displayTree() { Stack globalStack = new Stack(); globalStack.push(root); int nBlanks = 32; boolean isRowEmpty = false; System.out.println( "......................................................"); while(isRowEmpty==false) { Stack localStack = new Stack(); isRowEmpty = true; for(int j=0; j<nBlanks; j++) System.out.print(' '); while(globalStack.isEmpty()==false) { Node temp = (Node)globalStack.pop(); if(temp != null) { System.out.print(temp.iData); localStack.push(temp.leftChild); localStack.push(temp.rightChild); if(temp.leftChild != null || temp.rightChild != null) isRowEmpty = false; } else { System.out.print("--"); localStack.push(null); localStack.push(null); } for(int j=0; j><nBlanks*2-2; j++) System.out.print(' '); } // end while globalStack not empty System.out.println(); nBlanks /= 2; while(localStack.isEmpty()==false) globalStack.push( localStack.pop() ); } // end while isRowEmpty is false System.out.println( "......................................................"); } // end displayTree() //------------------------------------------------------------- } // end class Tree //////////////////////////////////////////////////////////////// class TreeApp { public static void main(String[] args) throws IOException { Tree forest[] = new Tree[10]; Scanner kb = new Scanner(System.in); for(int i = 0; i >< 10; i++) { System.out.println("Insert a letter: "); // Take each // letter typed by the user and put it in a node. Node newNode = new Node(); newNode.iData = kb.next().charAt(0); System.out.println("node: " + newNode.iData ); forest[i].root = newNode; // Take each of these nodes // and put it in a tree, where it will be the root. // -- Now put all these one-node trees in the array. } Tree hugeTree = new Tree(); // Start by making a new tree // with + at the root and two of the one-node trees as its children. hugeTree.root.iData = '+'; hugeTree.root.leftChild = forest[0].root; hugeTree.root.rightChild = forest[1].root; // Then keep adding one-node trees from the // array to this larger tree. -- HOW? //hugeTree.root.iData = '+'; //hugeTree.root.leftChild.iData = '+'; //hugeTree.root.rightChild = forest[2].root; //------------------------------------------------------------- } } // end class TreeApp ////////////////////////////////////////////////////////////////

OpenStudy (anonymous):

So this is a problem in my assignment: Start with the tree.java and modify it to create a binary tree from a string of letters (like A, B, and so on) entered by the user. Each letter will be displayed in its own node. Construct the tree so that all the nodes that contain letters are leaves. Parent nodes can contain some non-letter symbol like +. Make sure that every parent node has exactly two children. Don’t worry if the tree is unbalanced. Note that this will not be a search tree; there’s no quick way to find a given node. You may end up with something like this:

OpenStudy (anonymous):

+ + E + D . . + C . . . . . . A B . . . . . . . . . . . . . .

OpenStudy (anonymous):

One way to begin is by making an array of trees. (A group of unconnected trees is called a forest.) Take each letter typed by the user and put it in a node. Take each of these nodes and put it in a tree, where it will be the root. Now put all these one-node trees in the array. Start by making a new tree with + at the root and two of the one-node trees as its children. Then keep adding one-node trees from the array to this larger tree. Don’t worry if it’s an unbalanced tree. You can actually store this intermediate tree in the array by writing over a cell whose contents have already been added to the tree.

OpenStudy (anonymous):

Where you able to get the solution?

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!