I have been given this interface, -
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;
-
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() {
-
Node temp = new Node();
-
/* could have used these for recursion
-
final boolean flag = true;
-
if (flag) */
-
temp = root;
-
if (temp == null) {
-
return 0;
-
} else {
-
/* would have been easy to find height using this recursion
-
return 1 + max(height(temp.left), height(temp.right)); */
-
}
-
}
-
-
public boolean contains (Integer target) {
-
-
}
-
/**
-
* @param args the command line arguments
-
*/
-
public static void main(String[] args) {
-
-
BSTree bs = new BSTree();
-
bs.insert(12);
-
bs.insert(3);
-
bs.insert(14);
-
}
-
-
}
-
-
The objective requires that the height be implemented without using an argument. Do you have some ideas?
Jan 6 '08
17 16421 JosAH 11,448
Recognized Expert MVP
I must have been thinking of Ents
How silly: Ents are totally extinct because they were all male. You can use
final static variables for them.
kind regards,
Jos ;-)
JosAH 11,448
Recognized Expert MVP
Maybe it's my background (math) but I usually find recursion easier to write and understand than iteration.
Let's shake hands but the way I read the question was about a non recursive
method and I'm also from the lazy bones camp and I think it's much easier
to cache stuff manipulated in the insert method than to set up explicit stacks
and/or trying to be clever with pointer fiddling etc. Recursive methods are much
easier to handle and implement though; I agree.
kind regards,
Jos
BigDaddyLH 1,216
Recognized Expert Top Contributor
Let's shake hands but the way I read the question was ...
Hear, hear. Now if we weren't on far sides of the world, I would say that calls for a beer!
JosAH 11,448
Recognized Expert MVP
Hear, hear. Now if we weren't on far sides of the world, I would say that calls for a beer!
Me too! No problem though, distance is just a figment of people's imagination and
we can solve this the mathematical way: I buy and drink your beer as well ;-)
kind regards,
Jos
Me too! No problem though, distance is just a figment of people's imagination and
we can solve this the mathematical way: I buy and drink your beer as well ;-)
kind regards,
Jos
Public drinking is not allowed in the public forums.
Admin.
JosAH 11,448
Recognized Expert MVP
Public drinking is not allowed in the public forums.
Admin.
I wore a brown paper bag over my head so there was nothing public about it. I
looked and acted just like any other law obedient citizen.
kind regards,
Jos ;-)
- static Node root;
-
static int countNode;
-
...
-
-
public BSTree() {
-
root = null;
-
}
-
...
-
public int size () {
-
return countNode;
-
}
-
-
I just noticed that the root and the countNode (tree size) fields are static. What happens when you have another tree?
Those variables are static because trees themselves don't move either; when will
people ever learn ...
kind regards,
Jos ;-)
Thanks Jos for defending the objective. In this problem we were not assigned to make multiple BSTrees, rather just one tree which would have all these functions. :)
As far as writing a non-recursive (loop-iterative) height function is concerned, I could not come up with any and therefore implemented an overridden recursive function in the same class and called that recursive function from this non-recursive function.
I know that wasn't what I was supposed to do but I didnt have any bright ideas until the very last minute of my submission. Time for a compromise..eh
BigDaddyLH 1,216
Recognized Expert Top Contributor
Thanks Jos for defending the objective. In this problem we were not assigned to make multiple BSTrees, rather just one tree which would have all these functions. :)
I would rethink that, if I were you. You are going out of your way to make your code less general. If you insist on having static data, then all the methods should be made static, and the constructor hidden, because you've given up on a class that is designed to be instantiated.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: pembed2003 |
last post by:
Hi,
I have a question about how to walk a binary tree. Suppose that I have
this binary tree:
8
/ \
5 16
/ \ / \
3 7 9 22
/ \ / \ / \
|
by: Max |
last post by:
Hi guys, did some searching on the topic, but can't find much more then
just basic info on binary trees. I need to write a binary tree
implementation so that it is always complete. In other words operations
such as add and delete will not cause the completeness to break.
Re-adding all of the elements to a brand new tree and wiping out the old
one is not an option, so any sorting that I do must be in place.
Does anyone know of a good...
|
by: Tarique Jawed |
last post by:
Alright I needed some help regarding a removal of a binary search
tree. Yes its for a class, and yes I have tried working on it on my
own, so no patronizing please. I have most of the code working, even
the removal, I just don't know how to keep track of the parent, so
that I can set its child to the child of the node to be removed.
IE - if I had
C
/ \
B D
|
by: Foodbank |
last post by:
Hi all,
I'm trying to do a binary search and collect some stats from a text
file in order to compare the processing times of this program (binary
searching) versus an old program using linked lists. I'm totally new
to binary searches by the way. Can anyone help me with the commented
sections below? Much of the code such as functions and printfs has
already been completed. Any help is greatly appreciated.
Thanks,
|
by: ramu |
last post by:
Hi,
I have, suppose 1000 numbers, in a file. I have to find out 5
largest numbers among them without sorting. Can you please give me an
efficient idea to do this? My idea is to put those numbers into a
binary tree and to find the largest numbers. How else can we do it?
Regards
| |
by: eSolTec, Inc. 501(c)(3) |
last post by:
Thank you in advance for any and all assistance. Is there a way to start,
pause and resume a recurrsive search exactly where you left off, say in the
registry programmatically?
--
Michael Bragg, President
eSolTec, Inc.
a 501(C)(3) organization
MS Authorized MAR
looking for used laptops for developmentally disabled.
|
by: hn.ft.pris |
last post by:
I have the following code:
Tree.h defines a simple binary search tree node structure
########## FILE Tree.h ################
#ifndef TREE_H
#define TREE_H
//using namespace std;
template <typename Tclass Tree{
private:
Tree<T*left;
|
by: nishit.gupta |
last post by:
Can somebody please help me for algorithm for postorder bst traversal
without recursion.
It should use stack. i am not able to implement it.
thnx
|
by: aemado |
last post by:
I am writing a program that will evaluate expressions using binary trees. Most of the code has been provided, I just have to write the code for the class functions as listed in the header file. However, I am really new to recursion and trees...and this program uses public functions and private helper functions, which I am completely lost in. Here is the header file:
struct CTNode
{
NodeType type;
int operand;
CTNode *left, *right;
};
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |