hi everyone
i have a problem in my project that i have made
pleaze help!!!!!
this project allows the user to read words and their meanings from file and store them in AVL tree and then print the tree inorder, postorder, and preorder then print the result to file in the form of dictionary A: .........
..........
B:..........
and so on, and then search for a word and its all meanings and if the word is not found add it to the tree then draw the tree in java graphics and finally ask the user if he want to delete any word

Attachments
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class AVLTree extends JFrame implements ActionListener {
	protected AVLNode root,parent,node;
	File file;
	private AVLTree tree;
	String [] word,meaning;
	
	JTextField fs=new JTextField("enter word you want to search for");
	JTextField fd=new JTextField ("enter word you want to delete");
	JButton btree= new JButton("construct the tree");
	JButton bprint= new JButton("print");
	JButton bresult= new JButton("print result");
	JButton bsearch= new JButton("do search");
	JButton bs= new JButton ("DO SEARCH");
	JButton bdel= new JButton("delete");
	JButton bd= new JButton ("DELETE");
	JButton bdraw= new JButton("draw the tree");
	JButton bin= new JButton("print inorder");
	JButton bpre= new JButton("print preorder");
	JButton bpost= new JButton("print postorder");

	/*
	 * default constructor
	*/
	public AVLTree(){
		root=null;
		JPanel p= new JPanel ();
		p.setLayout(new GridLayout(6,1,10,20));
		p.add(btree);
		p.add(bprint);
		p.add(bresult);
		p.add(bsearch);
		p.add(bdel);
		p.add(bdraw);
		
		
		add(p);
		
		btree.addActionListener(this);
		bprint.addActionListener(this);
		bresult.addActionListener(this);
		bsearch.addActionListener(this);
		bdel.addActionListener(this);
		bdraw.addActionListener(this);
		bin.addActionListener(this);
		bpre.addActionListener(this);
		bpost.addActionListener(this);
		
	}

	public AVLNode getRoot(){
		return root;
	}
	
	// Insert value into the AVL tree
	public AVLNode insert(String value,AVLNode t) {
		
		if( t == null ){
			//if the Root is Null Create New Node
			t = new AVLNode(value);
			t.left=null;
			t.right=null;	
		}
		else if (value.compareTo(t.key)<0){
	    	t.left=insert(value,t.left);
	    	if( t.left.height - t.right.height == 2 ) {
	            if( value.compareTo( t.left.key ) < 0 )
	            	t = singleRotation(t, 1);
	            else
	                t = doubleRotation(t, 1);
	        }
	    }	
	    else if (value.compareTo(t.key)>0){
	    	t.right=insert(value,t.right);
	    	if( t.right.height - t.left.height == 2 ) {
	            if( value.compareTo( t.right.key ) > 0 )
	            	t = singleRotation(t, 1);
	            else
	                t = doubleRotation(t, 1);
	        }
	    }
	    else
	    	t.height=Math.max(t.left.height,t.right.height)+1;
	    return t;
	}

    /** In order traversal from a subtree */
    private void inorder(AVLNode root) {
    	if (root != null){
    		inorder(root.left);
    		System.out.println(root.key + " ");
        	inorder(root.right);
    	}
    }
	
    /** Pre order traversal from a subtree */
    private void preorder(AVLNode root) {
    	if (root != null){
    		System.out.println(root.key + " ");
    		preorder(root.left);
    		preorder(root.right);
    	}
    }
	
    /** Post order traversal from a subtree */
    private void postorder(AVLNode root) {
    	if (root == null) {
    		postorder(root.left);
    		postorder(root.right);
    		System.out.println(root.key + " ");
    	}
    }
 
	public AVLNode search(String s){
		AVLNode current = root;  // start at root
		while (s!=current.key){
			if(s.compareTo(current.key) <0)  // go left
				current = current.left;
			else                             // or go right
				current = current.right;
			if(current == null){  // if no child
				System.out.println("Not found");
				return null;    // didn't find it
			}
		}
		return current;    // found
	}
		
	public void delete(String s){
		AVLNode current=root;
		parent=root;
		boolean isLeft=false;
		while(s!=current.key){        // search for node
			parent = current;
			if(s.compareTo(current.key) <0)         // go left?
				current = current.left;
            else                            // or go right?
            	current = current.right;
			if(current == null)             // end of the line,
				System.out.println("NOT found"); // didn't find it
        }  // end while
		
		// if no children, simply delete it
	    if(current.left==null &&current.right==null){
	         if(current == root) // if root,
	            root = null;     // tree is empty
	         else if(isLeft){
	            parent.left = null;
	            if( current.left.height - current.right.height == 2 ) {
	            	if( s.compareTo( current.left.key ) < 0 )
	            		current = singleRotation(current, 1);
	                else
	                    current = doubleRotation(current, 1);
	            }
	         }   
	         else{                         
	            parent.right = null;
	            if( current.right.height - current.left.height == 2 ) {
	            	if( s.compareTo( current.right.key ) > 0 )
	            		current = singleRotation(current, 1);
	                else
	                    current = doubleRotation(current, 1);
	            }
	         }   
	         } 
		
	}	
	
	public int getHeight1(){
		return getHeight1(getRoot(), 0);
	}

	public int getHeight1(AVLNode p, int currentHeight){
		if(p == null)
			return currentHeight;
		else
			return max(getHeight1(p.getLeft(), currentHeight ), getHeight1(p.getRight(), currentHeight));
	}

	public int max(int aValue, int bValue){
		return aValue > bValue ? aValue : bValue;
	}

	public boolean isEmpty(){
		return root == null;
	}
	
	private AVLNode singleRotation (AVLNode node, int side){
		AVLNode temp = node; // Just to declare
	    if (side == 0){ // Left Rotation
	    	temp = node.left;
	        node.left = temp.right;
	        temp.right = node;
	        node.height = Math.max( node.left.height, node.right.height ) + 1;
	        temp.height = Math.max( temp.left.height, node.height ) + 1;
	    }
	    else if (side == 1){ // Right Rotation
	    	temp = node.right;
	        node.right = temp.left;
	        temp.left = node;
	        node.height = Math.max( node.left.height, node.right.height ) + 1;
	        temp.height = Math.max( temp.right.height, node.height ) + 1;
	    }
	    return temp;
	}
	
	private AVLNode doubleRotation(AVLNode node, int side) {
		if (side == 0){ //Double Left Rotation
			node.left = singleRotation( node.left, 1 );        
	        return singleRotation( node, 0 );
	    }
	    else if (side == 1){ //Double Right Rotation
	    	node.right = singleRotation( node.right, 0 );
	        return singleRotation( node, 1 );
	    }
	        
	    return node;
	}
	
	// count method to compute the number of lines in a file
    public int count(File filename) throws IOException {    
    	InputStream is = new BufferedInputStream(new FileInputStream(filename));   
    	byte[] c = new byte[1024];    
    	int count = 0;    
    	int readChars = 0;    
    	while ((readChars = is.read(c)) != -1) {        
    		for (int i = 0; i < readChars; i++) {            
    			if (c[i] == '\n')                
    				++count;       
    		}   
    	}    
    	return count;
    }
    
	 /** Main method */
	public static void main(String[] args)  {
		JFrame frame = new AVLTree();
		frame.setTitle("What to do");
		frame.setLocationRelativeTo(null); // Center the frame
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(300, 300);
		frame.setResizable(false);
		frame.setVisible(true);
	} 
	
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==btree){
			//read nodes from file
            JFileChooser fb = new JFileChooser();
            fb.showOpenDialog(null);
            file = fb.getSelectedFile();
            FileInputStream fis = null;
            BufferedInputStream bis = null;
            DataInputStream dis = null;

            try {
                fis = new FileInputStream(file);
                bis = new BufferedInputStream(fis);
                dis = new DataInputStream(bis);
                int no_nodes = count (file);
                String [] words= new String[no_nodes];
                while (dis.available() != 0) {
                	for (int i = 0; i < words.length; i++){
                		tree=new AVLTree();
                		words[i]= dis.readLine();
                		System.out.println("\n"+words[i]);
                		while (words[i]!=":"){
                			word[i]=words[i];
                			meaning[i]=words[i];
                		}	
                		node=insert(word[i],node);
                	}
                }    
                   
                fis.close();
                bis.close();
                dis.close();
            } catch (FileNotFoundException e1) {
                e1.printStackTrace();
            } catch (IOException e1) {
                e1.printStackTrace();
            }   
            
		}
		
		if(e.getSource()==bprint){
			JFrame f = new JFrame();
			f.setTitle("How to print the tree");
			f.setLocationRelativeTo(null);
			f.setSize(300,200);
			f.setVisible(true);
			
			JPanel p1= new JPanel ();
			p1.setLayout(new GridLayout(3,1,10,10));
			p1.add(bin);
			p1.add(bpre);
			p1.add(bpost);
			
			f.add(p1);
		}
		
		if(e.getSource()==bresult){
			char a ='A';
			try {
			int no_nodes = count (file);
			for (int i=0; i<no_nodes; i++){
				
			}
			a++;
			}catch (FileNotFoundException e1) {
                e1.printStackTrace();
            } catch (IOException e1) {
                e1.printStackTrace();
            }   
			
		}
		
		if(e.getSource()==bsearch){
			JFrame f1 = new JFrame ();
			f1.setTitle("SEARCH");
			f1.setLocationRelativeTo(null);
			f1.setSize(300,200);
			f1.setVisible(true);
			
			JPanel p= new JPanel();
			p.setLayout(new GridLayout(2,1,10,10));
			p.add(fs);
			p.add(bs);
			
			f1.add(p);
		}
		
		if (e.getSource()==bs){
			String a= fs.getText();
			node= search(a);
			JOptionPane.showMessageDialog(null,"The wanted word is:"+node+"\t"+"and the meaning is"+meaning);
		}
		
		if(e.getSource()==bdel){
			JFrame f1 = new JFrame ();
			f1.setTitle("DELETE");
			f1.setLocationRelativeTo(null);
			f1.setSize(300,200);
			f1.setVisible(true);
			
			JPanel p= new JP

Pleaze i need this program soon if any one can help me

You haven't described what is your problem and what have you done so far. Don't expect us to read the entire code and see what works and what doesn't.
Also you are mixing everything up.

Forget about the gui.
Keep the node class and create an AVLTree class. The Tree is not a gui and has nothing to do with is. It is a tree. Meaning, test the functionality of the tree separately. The tree will do only the things you described. Have methods with arguments that search and delete and test all of them using System.out.print in a separate class. That class will have only the main method. Instantiate the tree in the main method and test it. Then create another gui class and this time instantiate the tree in there and use its methods.

That way if something doesn't work you will know what it is.

With your description and code no one is going to understand what is your problem. If your problem is for example "preorder" then all you have to do is fix the specific method and test it separately in the main method that I described. If it works, in the same main you will test the rest of the methods.
Then if all of them work you will call the Tree class in the new gui class that you will create and all other errors that you will be getting will be gui related. So you will not get confused as where to search for the error.


PS:
Use this:

left = null;
        right = null;
        father = null;

not this:

left = right = father = null;

with your way you are saying: left = right = father = null; that left, right, father are the same objects so one change made to the "left" will applied to the rest and mess up your logic

Edited 7 Years Ago by javaAddict: n/a

This question has already been answered. Start a new discussion instead.