| | |
Urgent : Binary Search Tree problem
Thread Solved |
•
•
Join Date: Nov 2008
Posts: 6
Reputation:
Solved Threads: 0
interface BinarySearchTree {
public void insert(Integer data);
public int size();
public int height();
public boolean contains(Integer target);
}
and I have to implement BST with all these functions. I have implemented the first insert and size like this way -
class Node {
Node left, right, next;
Integer data;
Node () {
left = right = null;
data = 0;
}
}
public class BSTree extends Node implements BinarySearchTree {
static Node root;
static int countNode;
/**
* Creates a new instance of BSTree
*/
public BSTree() {
root = null;
}
public void insert(Integer data) {
if (root == null) {
root.data = data;
countNode++;
} else {
Node temp = new Node();
temp = root;
while (temp != null) {
if (temp.data < data) temp = temp.right;
else {
temp = temp.left;
}
temp.data = data;
countNode++;
}
}
}
public int size () {
return countNode;
}
public int height() {}
public boolean contains (Integer target) {}
public static void main(String[] args) {
BSTree bs = new BSTree();
bs.insert(12);
bs.insert(3);
bs.insert(14);
}
}
Can any one help me by writing the code of finding the height and contains function ?
public void insert(Integer data);
public int size();
public int height();
public boolean contains(Integer target);
}
and I have to implement BST with all these functions. I have implemented the first insert and size like this way -
class Node {
Node left, right, next;
Integer data;
Node () {
left = right = null;
data = 0;
}
}
public class BSTree extends Node implements BinarySearchTree {
static Node root;
static int countNode;
/**
* Creates a new instance of BSTree
*/
public BSTree() {
root = null;
}
public void insert(Integer data) {
if (root == null) {
root.data = data;
countNode++;
} else {
Node temp = new Node();
temp = root;
while (temp != null) {
if (temp.data < data) temp = temp.right;
else {
temp = temp.left;
}
temp.data = data;
countNode++;
}
}
}
public int size () {
return countNode;
}
public int height() {}
public boolean contains (Integer target) {}
public static void main(String[] args) {
BSTree bs = new BSTree();
bs.insert(12);
bs.insert(3);
bs.insert(14);
}
}
Can any one help me by writing the code of finding the height and contains function ?
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
"How to ask questions the smart way ?"
"How to ask questions the smart way ?"
![]() |
Other Threads in the Java Forum
- Previous Thread: Intranet aplication
- Next Thread: about big endian and little endian representation
| Thread Tools | Search this Thread |
android api applet application apps array arrays automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) card chat class classes client code collision columns component constructor database designadrawingapplicationusingjavajslider draw eclipse error errors eventlistener exception expand fractal free game givemetehcodez graphics gui guidancer html ide image inetaddress integer integration intellij j2me java javafx javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia linux list loop machine map method methods migrate mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle plazmic print problem program programming project recursion scanner server set sharepoint smart sms smsspam sort sortedmaps sql string subclass support swing textfield threads tree unlimited utility webservices windows






