I am trying to create a binary tree that takes strings but it uses "-" and "+" to go left or right if the sign is + insert left and if it's - then insert right. Here is a visual representation of what I am trying to do.
insert method should take the word and just a single sign for now and based of that insert right or left
Here is my code but I am getting nullpointer error. Apparently, I am not inserting into the right order
public class BinaryTree {
private static Node root = null;
private static Node sign = null;
public static void main(String[] args) {
// TODO Auto-generated method stub
BinaryTree bt = new BinaryTree();
bt.insert("to", "-");
bt.insert("the", "+");
bt.preorder();
}
private class Node {
String data;
String sign;
Node left;
Node right;
public Node(String w) {
data = w;
left = right = null;
}
// public Node(String w, String s) {
// data = w;
// sign = s;
// left = right = null;
//
// }
} // -----------------end of Node
private void insert(String val, String sign) {
root = insert(root, val, sign);
}
Node insert(Node r, String data, String passSign) {
if (r == null) {
return new Node(data);
}
if(r.sign.equals(passSign)) {
r.right = insert(r.right, data, passSign);
}
else if (r.sign.equals(passSign)){
r.left = insert(r.left, data, passSign);
}
return r;
}
public void preorder() {
preorder(root);
}
public void preorder(Node p) {
if (p != null) {
System.out.println(p.data);
preorder(p.left);
preorder(p.right);
}
}
}
The main problems are:
The BinaryTree nor the Node instances should have a sign member. The sign only plays a role during the insertion process, but has no meaning any more once a node is inserted
r.sign.equals(passSign) is therefore also not the correct condition to check. According to your description you should just check whether the sign is a "-" and go right, or else go left ("+"). There is no state of the node that influences this decision. So do passSign.charAt(0) == '-' instead.
When making the recursive call you should not pass the same sign again: it has already been processed. Instead, pass any signs that follow after the consumed one. You can use substring for that purpose.
The image shows a root node that has no value. Yet you are right in creating a tree instance with no node at all. So your insert method should deal with the case where the root is null, but the sign argument is not the empty string. In that case a root node should be created, but it should not hold the target data, as for that we should still go deeper in the tree. This principle could apply to any node, not only the root. So foresee the creation of such "place-holder" nodes and give them some default value (like "(null)").
Not a problem, but I find it more useful to print in inorder order, and indent the deeper nodes. This way you get an idea how the tree is structured.
Here is the corrected code:
public class BinaryTree {
private static Node root = null;
// No sign member needed;
public static void main(String[] args) {
BinaryTree bt = new BinaryTree();
bt.insert("to", "-");
bt.insert("the", "+");
bt.insert("buy", "-+");
bt.insert("imperial", "+-");
bt.insert("afflication", "++");
bt.inorder();
}
private class Node {
String data;
// No sign member needed;
Node left;
Node right;
public Node(String w) {
data = w;
left = right = null;
}
}
private void insert(String val, String sign) {
root = insert(root, val, sign);
}
Node insert(Node r, String data, String passSign) {
// Check whether there is a sign
if (passSign.length() == 0) {
return new Node(data);
}
// If needed, create a placeholder node so to be able to descend further
if (r == null) {
r = new Node("(null)");
}
if (passSign.charAt(0) == '-') {
// Extract the rest of the signs
r.right = insert(r.right, data, passSign.substring(1, passSign.length()));
}
else {
r.left = insert(r.left, data, passSign.substring(1, passSign.length()));
}
return r;
}
public void inorder() {
inorder(root, "");
}
// This method gives a bit more visual output
public void inorder(Node p, String indent) {
if (p != null) {
inorder(p.left, indent + " ");
System.out.println(indent + p.data);
inorder(p.right, indent + " ");
}
}
}
Related
I have a Binary Search Tree and I think one of my method is working incorrectly. The program I have is a program that separates the strings read from a file word by word and deletes the special characters in it, then transfers these words to the data structure in alphabetical order. If the same word was previously conveyed during the transmission, it increases the frequency of that word. While checking the output of my program, I saw something like this.
MY OUTPUT:
Readed Line: sun-meal //After some operation it is seperated like "sun" and "metal"
String inserted.
String inserted.
Readed Line: sun-oil //After some operation it is seperated like "sun" and "oil"
String inserted.
String inserted. //Error is here.
TRUE OUTPUT SHOULD BE:
Readed Line: sun-meal //After some operation it is seperated like "sun" and "metal"
String inserted.
String inserted.
Readed Line: sun-oil //After some operation it is seperated like "sun" and "oil"
String inserted.
Repeated String. Frequency +1. //It should be like that.
I will share my source code but what I want to know is what am I doing wrong? Why is "sun" inserted 2 times?
TreeDriver Class:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class TreeDriver
{
public static void main(String [] args) throws FileNotFoundException {
Tree stTree = new Tree();
TreeNode compareNode;
Scanner scan = new Scanner(new File(args[0]));
while (scan.hasNextLine()) {
String data = scan.nextLine();
System.out.println("Readed Line: "+data);
String[] convertedData = data.replaceAll("[^a-zA-Z ]", " ").toLowerCase().split("\\s+");
int y = 0;
try {
while(convertedData[y] != null){
String st = convertedData[y];
if (st.contains(" ")) {
}
else{
compareNode = Tree.search(stTree.getRoot(), st);
if (compareNode != null) {
compareNode.upFreq();
System.out.println("\tRepeated String. Frequency +1.");
} else {
stTree.insert(st);
System.out.println("\tString inserted.");
}
y++;
}
}
}
catch(Exception ignored) {
}
}
scan.close();
}
}
TreeNode Class
public class TreeNode
{
private int freq; //frequency of the String in the Node
private String stValue;
private TreeNode left;
private TreeNode right;
public TreeNode(String st)
{
stValue = st;
left = null;
right = null;
freq = 1;
}
public void add(String st)
{
if (left == null)
{
left = new TreeNode(st);
}
else if (right == null)
{
right = new TreeNode(st);
}
else
{
if(countNodes(left) <= countNodes(right))
{
left.add(st);
}
else
{
right.add(st);
}
}
}
//Count the nodes in the binary tree to which root points, and
public static int countNodes( TreeNode root ) {
if ( root == null )
// The tree is empty. It contains no nodes.
return 0;
else {
// Start by counting the root.
int count = 1;
// Add the number of nodes in the left subtree.
count += countNodes(root.getLeft());
// Add the number of nodes in the right subtree.
count += countNodes(root.getRight());
return count; // Return the total.
}
}
public TreeNode getLeft(){
return left;
}
public TreeNode getRight(){
return right;
}
public String getString()
{
return stValue;
}
public void upFreq()
{
freq = freq + 1;
}
public int getFreq()
{
return freq;
}
}
Tree Class:
public class Tree
{
private TreeNode root;
public Tree()
{
root = null;
}
public boolean isEmpty()
{
return root == null;
}
public void insert(String st)
{
if (isEmpty())
{
root = new TreeNode(st);
}
else
{
root.add(st);
}
}
public TreeNode getRoot()
{
return root;
}
public static TreeNode search(TreeNode root, String st)
{
if(root == null)
{
return null;
}
else if(st.equals(root.getString()))
{
return root;
}
else
{ if (root.getLeft() != null)
return search(root.getLeft(), st);
else
return search(root.getRight(), st);
}
}
public TreeNode found(TreeNode root)
{
return root;
}
public static void preorderPrint(TreeNode root)
{
if ( root != null )
{
System.out.print( root.getString() + " " ); // Print the root item.
preorderPrint( root.getLeft() ); // Print items in left subtree.
preorderPrint( root.getRight() ); // Print items in right subtree.
}
}
}
Can you please help me find the problem?
Indeed, your search function is wrong :
if (root.getLeft() != null)
return search(root.getLeft(), st);
else
return search(root.getRight(), st);
You are going through the right child node only if the left one is null, when you should go through both.
I want to write a java program to arrange the given employee ID in a Binary Search Tree format and then find the immediate manager of any given employee in the organization.
Employee Binary Tree:
Input Format
The first line should contain the number of employees "n" to be inserted in the Tree
The second line should contain the employee ID for which we need to find the immediate manager.
The next "n" lines should contain the employee IDs to form the binary search Tree.
Sample:
Below is the code with which I have intialized the tree successfully. I need help on how to search the parent node for a given child node.
import java.util.Scanner;
class BinarySearchTree {
/* Class containing left and right child of current node and key value*/
class Node {
int data;
int key;
Node left, right;
public Node(int item) {
key = item;
left = right = null;
}
}
// Root of BST
Node root;
// Constructor
BinarySearchTree() {
root = null;
}
// This method mainly calls insertRec()
void insert(int key) {
root = insertRec(root, key);
}
/* A recursive function to insert a new key in BST */
Node insertRec(Node root, int key) {
/* If the tree is empty, return a new node */
if (root == null) {
root = new Node(key);
return root;
}
/* Otherwise, recur down the tree */
if (key < root.key)
root.left = insertRec(root.left, key);
else if (key > root.key)
root.right = insertRec(root.right, key);
/* return the (unchanged) node pointer */
return root;
}
// This method mainly calls InorderRec()
void inorder() {
inorderRec(root);
}
// A utility function to do inorder traversal of BST
void inorderRec(Node root) {
if (root != null) {
inorderRec(root.left);
System.out.println(root.key);
inorderRec(root.right);
}
}
public static void main(String[] args) {
BinarySearchTree tree = new BinarySearchTree();
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
//int sch = sc.nextInt();
for (int i=0;i<num;i++) {
tree.insert(sc.nextInt());
}
tree.inorder();
}
}
When you write a recursive method, the first thing you need is a condition that terminates the recursion. In your case there are two conditions, as follows.
You find the employee that you are searching for.
You reach the end of the tree.
The recursion needs to terminate if either of the above conditions is true. In your case, terminating the recursion means the method must return a value.
If the terminating condition is not true, the method needs to call itself but with different values for the method parameters.
Here is my recursive search method. More explanation appears after the code.
int search(int employee, int manager, Node node) {
if (node != null) {
if (employee == node.key) {
return manager;
}
manager = node.key;
if (employee < node.key) {
return search(employee, manager, node.left);
}
else {
return search(employee, manager, node.right);
}
}
else {
return -1;
}
}
The method parameters are as follows:
employee - the employee to search for
manager - the manager of the next parameter
node - a node in the tree that will be checked to see if its key is the employee we are searching for
We start searching at the root of the tree and traverse down through the tree nodes. Hence the initial call to the method, which would probably be in method main() would be:
tree.search(sch, -1, tree.root)
According to your sample data, sch is 55. Note that in the initial call, the manager is -1 since the root of the tree does not have a manager. So if sch equals the root key, the method will return -1.
Since we know that the key in the left child of a node is lower and the right child's key is higher, we don't have to search every node. So the method calls itself with the relevant child node. Also we save the current key because if the child node's key is the one we are searching for, then this node's key is the manager.
If the node parameter in method search() is null, that means we have reached the end of the tree which means we have not found the value we are searching for. The value -1 (minus one) is a convention for a value that indicates that the search failed. Method indexOf() in class String is an example of a method also using that convention.
Finally, I used the code you posted in your question. I only added the above method as well as a call to that method from method main(). I added the initial call to method search() as the last line in method main().
For completeness, here is the entire code:
import java.util.Scanner;
public class BinarySearchTree {
class Node {
int key;
Node left, right;
public Node(int item) {
key = item;
left = right = null;
}
}
// Root of BST
Node root;
// Constructor
BinarySearchTree() {
root = null;
}
// This method mainly calls insertRec()
void insert(int key) {
root = insertRec(root, key);
}
/* A recursive function to insert a new key in BST */
Node insertRec(Node root, int key) {
/* If the tree is empty, return a new node */
if (root == null) {
root = new Node(key);
return root;
}
/* Otherwise, recur down the tree */
if (key < root.key)
root.left = insertRec(root.left, key);
else if (key > root.key)
root.right = insertRec(root.right, key);
/* return the (unchanged) node pointer */
return root;
}
// This method mainly calls InorderRec()
void inorder() {
inorderRec(root);
}
// A utility function to do inorder traversal of BST
void inorderRec(Node root) {
if (root != null) {
inorderRec(root.left);
System.out.println(root.key);
inorderRec(root.right);
}
}
// The method I added.
int search(int employee, int manager, Node node) {
if (node != null) {
if (employee == node.key) {
return manager;
}
manager = node.key;
if (employee < node.key) {
return search(employee, manager, node.left);
}
else {
return search(employee, manager, node.right);
}
}
else {
return -1;
}
}
public static void main(String[] args) {
BinarySearchTree tree = new BinarySearchTree();
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
int sch = sc.nextInt();
for (int i = 0; i < num; i++) {
tree.insert(sc.nextInt());
}
tree.inorder();
System.out.printf("Manager is %d%n", tree.search(sch, -1, tree.root)); // changed this line to print only manager.
}
}
I'm having an issue with BST that can't seem to figure out.
I am to output get, min, max, floor, ceiling, rank, iterator using the DOB in my project.
I am currently hung up on the Dates being unaccepted due to integer number being too large.
public class StringBinaryTreeSample {
node root;
public void addNode(int key, String name) {
// Create a new Node and initialize it
node newNode = new node(key, name);
// If there is no root this becomes root
if (root == null) {
root = newNode;
} else {
// Set root as the Node we will start
// with as we traverse the tree
node focusNode = root;
// Future parent for our new Node
node parent;
while (true) {
// root is the top parent so we start
// there
parent = focusNode;
// Check if the new node should go on
// the left side of the parent node
if (key <= focusNode.key) {
// Switch focus to the left child
focusNode = focusNode.leftChild;
// If the left child has no children
if (focusNode == null) {
// then place the new node on the left of it
parent.leftChild = newNode;
return; // All Done
}
} else { // If we get here put the node on the right
focusNode = focusNode.rightChild;
// If the right child has no children
if (focusNode == null) {
// then place the new node on the right of it
parent.rightChild = newNode;
return; // All Done
}
}
}
}
}
// All nodes are visited in ascending order
// Recursion is used to go to one node and
// then go to its child nodes and so forth
public void inOrderTraverseTree(node focusNode) {
if (focusNode != null) {
// Traverse the left node
inOrderTraverseTree(focusNode.leftChild);
// Visit the currently focused on node
System.out.println(focusNode);
// Traverse the right node
inOrderTraverseTree(focusNode.rightChild);
}
}
public node findNode(int key) {
// Start at the top of the tree
node focusNode = root;
// While we haven't found the Node
// keep looking
while (focusNode.key != key) {
// If we should search to the left
if (key == focusNode.key) {
// Shift the focus Node to the left child
focusNode = focusNode.leftChild;
} else {
// Shift the focus Node to the right child
focusNode = focusNode.rightChild;
}
// The node wasn't found
if (focusNode == null)
return null;
}
return focusNode;
}
public static void main(String[] args) {
StringBinaryTreeSample theTree = new StringBinaryTreeSample();
theTree.addNode("8/15/1998", "Mujib");
theTree.addNode("5/13/2005", "Zia");
theTree.addNode("1/13/1952", "Freedom");
theTree.addNode("2/12/1990", "Victory");
theTree.addNode("3/2/1985", "Molly");
theTree.addNode("5/1/2010", "Asad");
theTree.addNode("11/23/1983", "Genny");
theTree.addNode("6/14/1979", "Independent");
// Different ways to traverse binary trees
// theTree.inOrderTraverseTree(theTree.root);
// theTree.preorderTraverseTree(theTree.root);
// theTree.postOrderTraverseTree(theTree.root);
// Find the node with key 75
theTree.inOrderTraverseTree(theTree.root);
System.out.println("11/23/1983");
System.out.println(theTree.findNode(1));
}
public String toString() {
return name + " has the DOB " + key;
/*
* return name + " has the key " + key + "\nLeft Child: " + leftChild +
* "\nRight Child: " + rightChild + "\n";
*/
}
public class node implements Comparable {
int key;
String name;
node leftChild;
node rightChild;
node(int key, String name) {
this.key = key;
this.name = name;
}
private int handleDOB(final node that){
final String [] thisDOB = key.split("/");
final String [] thatDOB = that.key.split("/");
final int thisMonth = Integer.valueOf(thisDOB[0]);
final int thisDay = Integer.valueOf(thisDOB[1]);
final int thisYear = Integer.valueOf(thisDOB[2]);
final int thatMonth = Integer.valueOf(thatDOB[0]);
final int thatDay = Integer.valueOf(thatDOB[1]);
final int thatYear = Integer.valueOf(thatDOB[2]);
if (thisYear < thatYear) {
return -1;
}
if (thisYear > thatYear) {
return +1;
}
if (thisMonth < thatMonth) {
return -1;
}
if (thisMonth > thatMonth) {
return +1;
}
if (thisDay < thatDay) {
return -1;
}
if (thisDay > thatDay) {
return +1;
}
return 0;
}
}
}
The first evident issue i see is this one:
theTree.addNode(08/15/1975, "Mujib");
That first parameter it's not a date in Java... you are performing a division with result equals to 0 since it's casted to int from it's original float value of 0.00027, use a Date object instead or an integer timestamp (maybe uglier but more practical?).
This is not a date in Java:
08/15/1975
It is a numeric expression that attempts to divide 08 by 15, then by 1975. If it did work, then integer division would yield 0.
But the "integer too large" error occurs because prefixing an int literal with a 0 indicates to Java that you have an octal literal, and only the digits 0-7 are legal for an octal number.
It is unclear how you expect a date to be implicitly converted to an int.
You may want to switch to using a Calendar object. You can parse a string such a "08/15/1975" into a Calendar using a SimpleDateFormat. If you're using Java 8, then you have the option of using LocalDate.
Basically, I am implementing an AVL tree by reading a set of integers from a text file and then populate the tree by using the add() method. Also, the program is supposed to print in order the set of integers.
As I run the program, a StackOverflowError pops up. I think that this error is being triggered due to something malfunctioning in the add() method.
I would really appreaciate if someone helps me as I am new to this type of programming.
This is part of the Main Class:
public static void main(String[] args) throws FileNotFoundException
{
AVL s1 = new AVL();
Scanner file = new Scanner(new File("C:\\Users\\Dell\\Desktop\\integers.txt"));
while(file.hasNext())
{
// String str = file.next();
//int b = Integer.parseInt(str);
int b = file.nextInt();
s1.add(b);
}
v1.PrintInOrder(v1.root);
These are the add() and PrintInOrder() methods:
public boolean add(int key)
{
root = add(root, key);
return true;
}
private Node add(Node b1, int key)
{
if(b1 == null)
{
return new Node(key);
}
if(key < b1.element){
b1.left = add(b1.left, key);
}
else
{
b1.right = add(b1.right, key);
}
int Left_Height = getHeight(b1.left);
int Right_Height = getHeight(b1.right);
// a height imbalance requires that two subtrees differ by two
if(Math.abs(LeftHeight - RightHeight )== 2)
return Balance(n1);
else
{
n1.ResetHeight();
return b1;
}
}
public void PrintInOrder(Node b1){
if(b1 != null){
PrintInOrder(b1.left);
System.out.println(b1.element);
PrintInOrder(b1.right);
}
}
This is the Node class:
public class Node {
Node left;
Node right;
int element;
int height;
public Node(int keys){
this(keys, null, null);
}
public Node(int d, Node right1, Node left1){
element = d;
height = 0;
left = left1;
right = right1;
}
// This method recalculates the height if the right or left subtrees have been altered
public void ResetHeight(){
int LeftHeight = AVL.getHeight(left);
int RightHeight = AVL.getHeight(right);
height = 1 + Math.max(LeftHeight,RightHeight);
}
Since stack overflows commonly occur in recursion. Use your IDE and set a break at locations where you have done recusion, then debug. Step through it.
I had to do some methods about BinaryTree (no Search Binary Tree). I'm not able to do 3 methods: reflect (reflect the tree, my code don't work beacuse reflect only a part of the tree), cut and cut2. The code is:
public class BinaryTree {
protected class Node {
Integer element;
Node left;
Node right;
Node(int element) {
this.element = element;
left = right = null;
}
Node(int element, Node left, Node right) {
this.element = element;
this.left = left;
this.right = right;
}
// is leaf?
boolean isLeaf() {
return left == null && right == null;
}
}
protected Node root;
public BinaryTree() {
root = null;
}
/* doesn't work */
public void reflect() {
if (root == null)
return;
reflect(root);
}
protected void reflect(Node node) {
reflect(node.left);
reflect(node.right);
Node temp = new Node(node.left.element);
node.left.element = node.right.element;
node.right.element = temp.element;
}
/* this method had to trim the tree at level h,
if h=0, it cut the whole tree. It change the original tree */
/* doesn't work */
public void cut(int h) {
}
/* i can change parameters */
protected void cut(Node node, int h) {
}
/* this method had to trim the tree at level h,
if h=0, it cut the whole tree. It doesn't change the original tree,
it returns a new tree */
/* doesn't work */
public BinaryTree cut2(int h) {
}
/* i can change parameters */
protected BinaryTree cut2(Node node, int h) {
}
}
}
I'm not able to do the methods reflect cut and cut2. Help me please, thank you!
This smells like homework, and even though the tag was pruned, I don't think us writing complete implementations of binary trees is the right thing.
Having said that, can you explain what isn't clear about implementing both of those methods? They're pretty much equal, except for the tree copying needed for cut2. I'd implement it via a recursive private method cutInternal(Node n, int currLevel, int h) passing in the level number that we're currently at. Then, when currLevel == h we just prune both nodes and return.
You almost had reflect right. Just avoid creating new Node objects:
protected void reflect(Node node) {
if (node != null) {
reflect(node.left);
reflect(node.right);
Node temp = node.left;
node.left = node.right;
node.right = temp;
}
}
As for cut:
public void cut(int h) {
if (h == 0) {
root = null;
} else {
cut(root, h - 1);
}
}
Then you can write cut(Node, int):
protected void cut(Node node, int h) {
if (node != null) {
if (h == 0) {
node.left = node.right = null;
} else {
cut(node.left, h - 1);
cut(node.right, h - 1);
}
}
}
See if you can work out cut2 on your own using the above as a start.