LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-19-2018, 01:57 PM   #1
zak100
Member
 
Registered: Jul 2009
Posts: 262

Rep: Reputation: 2
Windows: Java Programming: Tree Traversal & BST insert


Hi,
I have made a program to insert values in a Binary Search Tree (BST) in java. I have stored the values in an array and there are eleven values in the array 0..10 random values. However when I am using the condition:

Code:
 if(arrIndex<=10) 
      root = insert(root, data);
   else
      JOptionPane.showMessageDialog(null, "No more elements left in array");
I am getting overflow message but when I am using:
Code:
if(arrIndex<=10)[/B] 
      root = insert(root, data);
   else
      JOptionPane.showMessageDialog(null, "No more elements left in array");
it works. I cant figure out why :
Code:
if(arrIndex<=10)
does not work.

My complete program is given below:


Code:
//modified 2016 binary Search Tree Program
import javax.swing.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
class TreeNode{
   int data;
   TreeNode left;
   TreeNode right;
   TreeNode(int data1){
      data = data1;
      left = null;
      right =null;
   }
}

class bst2018 extends JFrame implements ActionListener{
   TreeNode root;
   String str= null;
   int[ ] arr= {7, 1, 9, 0, 3, 8, 10, 2, 5, 4, 6};
   int arrIndex;
   bst2018( ) {
      root = null;
      arrIndex=0;
      JButton btn1 = new JButton("Insert");
      JButton btn2 = new JButton("Inorder");
      JButton btn3 = new JButton("Preorder");
      JButton btn4 = new JButton("Postorder");
      JPanel p = new JPanel();
      p.add(btn1);
      p.add(btn2);
      p.add(btn3);
      p.add(btn4);
      btn1.addActionListener(this);
      btn2.addActionListener(this);
      btn3.addActionListener(this);
      btn4.addActionListener(this);
      add(p);
      pack();
      setVisible(true);
}
   public void actionPerformed(ActionEvent e) {
      String str1= "";
      String strBtnCaption=e.getActionCommand();
      if(strBtnCaption.equals("Insert")) {
         insert(arr[arrIndex]);
         
       }
       else if(strBtnCaption.equals("Inorder")) {
         inorder(root);
         JOptionPane.showMessageDialog(null,str );
       }
       else if(strBtnCaption.equals("Preorder")) {
         str = null;
         preorder(root);
         JOptionPane.showMessageDialog(null,str );
       }
       else if(strBtnCaption.equals("Postorder")) {
         str = null;
         postorder(root);
         JOptionPane.showMessageDialog(null,str );
       }
      
      }
   

public void insert(int data){ 
   if(arrIndex<10) 
      root = insert(root, data);
   else
      JOptionPane.showMessageDialog(null, "No more elements left in array");
}
public TreeNode insert(TreeNode node, int data){
   if (node == null){
      node = new TreeNode(data);
      JOptionPane.showMessageDialog(null, data);
      arrIndex++;
   }
   else {
          if(data < node.data)
             node.left = insert(node.left, data);
          else
             node.right = insert(node.right, data);
   }
   return node;
}
public void inorder(TreeNode r){ 
   
   if(r !=null)
      {
	inorder(r.left);
        str = str + " " +r.data;
	//
	inorder(r.right);
      }
   
}
public void preorder(TreeNode r) {
   if(r !=null)
	{
            str = str + " " + r.data;
	             preorder(r.left);
	             preorder(r.right);
	}
 }
public void postorder(TreeNode r) { 
    if(r !=null)
	{
	postorder(r.left);
	postorder(r.right);
        str = str + " " + r.data;
	
	}
}

public static void main(String[ ] args) {
   new bst2018();
}
}

Some body please guide me.

Zulfi.
 
Old 03-19-2018, 03:37 PM   #2
norobro
Member
 
Registered: Feb 2006
Distribution: Debian Sid
Posts: 792

Rep: Reputation: 331Reputation: 331Reputation: 331Reputation: 331
Did you look at the stack trace to see where the exception is occurring?
Code:
Exception in thread "AWT-EventQueue-1" java.lang.ArrayIndexOutOfBoundsException: 11
        at bst2018.actionPerformed(bst2018.java:46)
...
I suggest that you check your array index in that method.

Last edited by norobro; 03-19-2018 at 03:38 PM.
 
Old 03-20-2018, 09:25 AM   #3
zak100
Member
 
Registered: Jul 2009
Posts: 262

Original Poster
Rep: Reputation: 2
Hi,
Thanks for your help. I actually ignored the exception message. Thanks for pointing out my error. You are really a great programmer.

My correction is:
Code:
if(strBtnCaption.equals("Insert")) {
         if(arrIndex < arr.length)
            insert(arr[arrIndex]);
         else
            JOptionPane.showMessageDialog(null, "No more elements left in array");
       }
God bless you.
Zulfi.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Tree Programming Fzum Programming 3 06-21-2015 09:58 AM
[SOLVED] NFSv4 & NAT traversal ggeeoo Linux - Networking 1 09-01-2009 09:13 PM
BreadthFirst traversal of a tree with N children and Deque kpachopoulos Programming 1 11-18-2005 07:36 AM
Lex & yacc programming in windows. hemanexp Linux - Software 1 01-09-2004 05:41 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:07 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration