Results 1 to 2 of 2
Thread: Allocate memory
- 12-17-2010, 09:22 PM #1
Member
- Join Date
- Dec 2010
- Posts
- 2
- Rep Power
- 0
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); } } }
- 12-17-2010, 10:49 PM #2
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
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
-
SEVERE: Allocate exception for servlet ServletHandler
By shyameni in forum Java ServletReplies: 2Last Post: 03-05-2010, 12:35 PM -
how do I increase memory allocated to code cache (Non Heap Memory)
By manibhat in forum Advanced JavaReplies: 2Last Post: 08-21-2008, 07:33 PM -
Out of memory
By mew in forum New To JavaReplies: 1Last Post: 01-20-2008, 08:55 AM -
Memory
By mew in forum CLDC and MIDPReplies: 0Last Post: 12-28-2007, 11:02 AM -
Exception Report:annot allocate servlet instance for path
By mary in forum Java ServletReplies: 4Last Post: 11-05-2007, 05:14 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks