Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: construct a BST tree using the Node class given - revision

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default construct a BST tree using the Node class given - revision

    class Node{
        private int value;
        private Node[] children;
        boolean hasLeft = false;
        boolean hasRight = false;
     
        public Node(int a){
            children = new Node[2];
            value = a;
        }
        public Node(int a, Node left, Node right){
            children = new Node[2];
            value = a;
            children[0] = left;
            children[1] = right;
            hasLeft = true;
            hasRight = true;
        }
        public void setLeft(Node left){
            children[0] = left;
            hasLeft = true;
        }
        public void setRight(Node right){
            children[1] = right;
            hasRight = true;
        }
     
        public Node getLeft(){
            return children[0];
        }
        public Node getRight(){
            return children[1];
        }
        public int getValue(){
            return value;
        }
        public String toString(){
            return ""+value;
        }
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: construct a BST tree using the Node class given - revision

    Do you have a question?

  3. #3
    Junior Member
    Join Date
    Jun 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: construct a BST tree using the Node class given - revision

    Hey there Thanks for having a look - I am trying to construct a BST tree using this Node class given

    --- Update ---

    The program Im writing reads a txt into an arraylist, I have done that - from there I pick out which numbers are prime using my isPrime() method, done that - now I am trying to construct a binary search tree using the Node class given and use my addOntoBST() method to add into the BST.

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.ArrayList;
    import java.util.Scanner;
    import java.util.*;
     
     
    public class OrderingPrime {
        public static void main(String[] args) {
            orderPrime a = new orderPrime();
            a.order();
        }
    }
    class orderPrime{
        public orderPrime(){}
        private Scanner fileScanner;
        public void order(){
     
     ArrayList<Integer> allNumbers = new ArrayList<Integer>();
     
    try {
                fileScanner = new Scanner(new File("input.txt"));
                while(fileScanner.hasNextInt()){
                    allNumbers.add(fileScanner.nextInt());
                }
                System.out.println("File input.txt" + " " +allNumbers);
     
     
                for(int i = 0; i < allNumbers.size(); i++){
                        if(isPrime(allNumbers.get(i))){
                            System.out.print(allNumbers.get(i)+ " "); 
                        }
                }
     
     
            fileScanner.close();
            } catch (FileNotFoundException e) {
                        System.out.println("File not found!");
            } finally{
                        if(fileScanner!= null)fileScanner.close();
            }
          }
     
     
           public boolean isPrime(int input) {
              boolean isPrime = true;
              if(input<2)return false;
              for(int i=2;i<=Math.sqrt(input);i++){
                  if(input%i == 0){
                      //found a factor!
                      isPrime = false;
                  }
              }
              return isPrime;
          }
          public void addOntoBST(Node treeHead, Node current){
            if( treeHead == null){
     
            }
     
    }
     
     
     
    class Node{
        private int value;
        private Node[] children;
        boolean hasLeft = false;
        boolean hasRight = false;
     
        public Node(int a){
            children = new Node[2];
            value = a;
        }
        public Node(int a, Node left, Node right){
            children = new Node[2];
            value = a;
            children[0] = left;
            children[1] = right;
            hasLeft = true;
            hasRight = true;
        }
        public void setLeft(Node left){
            children[0] = left;
            hasLeft = true;
        }
        public void setRight(Node right){
            children[1] = right;
            hasRight = true;
        }
     
        public Node getLeft(){
            return children[0];
        }
        public Node getRight(){
            return children[1];
        }
        public int getValue(){
            return value;
        }
        public String toString(){
            return ""+value;
        }
    }

    your help is much appreciated

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: construct a BST tree using the Node class given - revision

    Welcome stun31

    You still have not asked a question. The only implied question I get from your post so far is "Will someone finish my work for me?"
    If you have a different question please ask it

  5. #5
    Junior Member
    Join Date
    Jun 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: construct a BST tree using the Node class given - revision

    Thanks

    My question is that I dont know how to construct a binary search tree using this class given and would like some points on how to start one using this class, I can construct a BST tree on its own but not using this class

Similar Threads

  1. Help with my Binary tree. (Node links are incorrect) Please!
    By Jacksla3 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 18th, 2013, 12:19 PM
  2. Height of a node in a binary tree
    By ueg1990 in forum Algorithms & Recursion
    Replies: 2
    Last Post: November 19th, 2012, 01:17 AM
  3. Dictionary using Distributed Hash Table and BST Tree
    By dezett in forum What's Wrong With My Code?
    Replies: 28
    Last Post: June 23rd, 2012, 12:03 PM
  4. [SOLVED] Finding deepest node of tree??
    By bczm8703 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 10th, 2011, 11:48 AM
  5. Date Of Birth Revision , Class please help
    By youssef22 in forum Object Oriented Programming
    Replies: 2
    Last Post: November 6th, 2010, 02:41 AM