Results 1 to 2 of 2

Thread: Allocate memory

  1. #1
    thescratchy is offline Member
    Join Date
    Dec 2010
    Posts
    2
    Rep Power
    0

    Default Allocate memory

    Hello

    I have the following problem:

    I am trying to create binary search tree.
    I have my dictionary from the type NodeType and I give it at the function insert(). In the function insert I use new NodeType() to fill it in

    BUT: If i print the value after this insertfunction in the main. I get an seg fault. My data given at this parameter disappears after the insert function. Does anyone have an idea wy / how to resolve this?

    Thank you

    Java Code:
    
    class Main{
    
      public static void main(String []args){
    	  
    	  BinTree tree = new BinTree();
    	  
    	 [B] tree.insert(10, tree.dictionary);[/B]
    	  tree.insert(5, tree.dictionary);
    
    	  System.out.println(tree.isMember(10, tree.dictionary));
    	  System.out.println(tree.isMember(5, tree.dictionary));
    	  
      }
    
    };
    
    class NodeType{
    	public int value;
    	public NodeType leftChild, rightChild;
    }
    
    class BinTree{
    	
    	[B]NodeType dictionary;[/B]
    	
    
    	boolean isEmpty(){
    		return (dictionary == null);
    	}
    	
    	boolean isMember(int val, NodeType d){
    		
    		if(d == null){
    			return false;
    		} else if (d.value == val)
    			return true;
    		else if (val  < d.value)
    			return (isMember(val, d.leftChild));
    		else if (val > d.value)
    			return (isMember(val, d.rightChild));
    		return false;
    	}
    	
    	void makeNull(){
    		dictionary = null;
    	}
    	
    	[B]void insert(int value, NodeType d){[/B]
    		
    		if(d == null){
    
    			d = new NodeType();
    			d.leftChild = null;
    			d.rightChild = null;
    			d.value = value;
    
    			
    		} else if (value < d.value){
    			insert(value, d.leftChild);
    		}
    		else {
    			insert(value, d.rightChild);
    		}
    
    	}
    }

  2. #2
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Your insertion method is wierd. Does it really have to have a reference to the tree it's being inserted into? It just complicates things. Keep a record of your root node, and upon insertion recursively go down the tree till you hit a null.
    Ever seen a dog chase its tail? Now that's an infinite loop.

Similar Threads

  1. SEVERE: Allocate exception for servlet ServletHandler
    By shyameni in forum Java Servlet
    Replies: 2
    Last Post: 03-05-2010, 12:35 PM
  2. Replies: 2
    Last Post: 08-21-2008, 07:33 PM
  3. Out of memory
    By mew in forum New To Java
    Replies: 1
    Last Post: 01-20-2008, 08:55 AM
  4. Memory
    By mew in forum CLDC and MIDP
    Replies: 0
    Last Post: 12-28-2007, 11:02 AM
  5. Replies: 4
    Last Post: 11-05-2007, 05:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •