Daniel
Daniel

Reputation: 57

Why are all my BST traversals returning the values in order?

I am working on a homework assignment for my data structures class that utilizes the BST data structure to sort 15 Nodes that have both a String name and an Int weight value to them. The key value that I am supposed to use for the class is the String name. Here is what my code looks like so far:

import java.util.Scanner;
/**
 *
 * @author daniel
 */
public class Assignment3 {
public static void main(String args[]){
    Scanner keyboard = new Scanner(System.in);
    Tree BST = new Tree();
    //Add 15 names and weights to Tree
    for(int i = 0; i < 15; i++){
        Node newNode = new Node(keyboard.next(), keyboard.nextInt());
        BST.insert(newNode.name);
    }

    System.out.print("Post: \n");
    BST.postorder();
    System.out.print("\nPre: \n");
    BST.preorder();
    System.out.print("\nIn: \n");
    BST.inorder();
}
}

class Node{
Node left, right;
int weight;
String name;
//Constructors
public Node(String n){
    left = null;
    right = null;
    name = n;
}

public Node(String n, int w){
    left = null;
    right = null;
    name = n;
    weight = w;
}
}

class Tree{
private Node root;

public Tree(){
    root = null;
}
//Insertion Method
public void insert(String name){
    root = insert(root, name);
}
//Insert Recursively
private Node insert(Node node, String name){
    if(node == null){
        node = new Node(name);
    }else{
        int compare = name.compareToIgnoreCase(node.name);          
        if(compare < 0){node.left = insert(node.left, name);}
        else{node.right = insert(node.right, name);}
    }
    return node;
}
//Inorder Traversal
public void inorder(){inorder(root);}
public void inorder(Node current){
    if(current != null){
        inorder(current.left);
        System.out.print(current.name + " ");
        inorder(current.right);
    }
}
//Postorder Traversal
public void postorder(){inorder(root);}
public void postorder(Node current){
    if(current != null){
        postorder(current.left);
        postorder(current.right);
        System.out.print(current.name + " ");

    }
}
//Preorder Traversal
public void preorder(){inorder(root);}
public void preorder(Node current){
    if(current != null){
        System.out.print(current.name + " ");
        preorder(current.left);
        preorder(current.right);
    }
}
}

However, when I run my code, all the traversals return the values in alphabetic order.

Here was my input: n 1 b 2 c 3 i 4 a 5 r 6 b 7 q 8 p 9 h 10 y 11 t 12 w 13 z 14 x 15

Output: Post: a b b c h i n p q r t w x y z

Pre: a b b c h i n p q r t w x y z

In: a b b c h i n p q r t w x y z

This is regardless to how I put in the data. I've tried multiple times to enter different data but I don't know what is going wrong. I'm thinking it has to do with my insertion method but I am not sure where to go from here. Thanks for your help.

Upvotes: 0

Views: 140

Answers (1)

Chandini
Chandini

Reputation: 550

public void postorder(){inorder(root);} // why is this inorder(root), it should be postorder(root), change it same with preorder.
public void postorder(Node current){
if(current != null){
    postorder(current.left);
    postorder(current.right);
    System.out.print(current.name + " ");

  }
}

Upvotes: 3

Related Questions