how do i get the main method to work?  this is to return an binary tree from lowest to highest in java

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
100%

how do i get the main method to work? 

this is to return an binary tree from lowest to highest

in java

 

 

 

import java.util.*;
import java.io.*; 

public class BinarySearchTree {
    
        private int root;
        private int free;
        private int left[];
        private int data[];
        private int right[];
        private int currentSize;     
        
        public BinarySearchTree(int size) {
           root = -1;
           free =-1;
           left = new int[size];
           data = new int[size];
           right = new int[size];
           currentSize = 0;
 }
      
    // constructor for initialise the root to null BYDEFAULT
    
    public boolean full() {
        return currentSize == data.length;
    }
    
    private int createNode(int value){
        data[currentSize]=value;
        left[currentSize]=-1;
        right[currentSize]=-1;
        return currentSize++;
    }
    // insert method to insert the new Date
    public void insert(int d) {
        if(root==-1){
            root=createNode(d);
        }
        else{
            insert(d, 0);
        }
    }

    private void insert( int d, int index) {
        if (data[index] == d) {
            return;
        }
        if (data[index] > d) {
            if (left[index] == -1) {
                left[index] = createNode(d);
            } else {
                insert(d, left[index]);
            }
        } else {
            if (right[index] == -1) {
                right[index] = createNode(d);
            } else {
                insert(d, right[index]);
            }
        }
        return;
    }

    
    public void print(){
     print(root);
        System.out.println();
    }
    private void print(int index){
        if(index!=-1){
            print(left[index]);
            System.out.print(data[index] + " ");
            print(right[index]);
        
        }
    }


    public static void main(String[] args) {
        // Creating the object of BinarySearchTree class
        BinarySearchTree bst = new BinarySearchTree();
        // call the method insert
        bst.insert(8);
        bst.insert(5);
        bst.insert(9);
        bst.insert(3);
        bst.insert(7);        
        bst.print();
        
    }
}

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 5 steps with 7 images

Blurred answer
Knowledge Booster
Types of trees
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education