height


Binary Tree find height of a specific Node


Hello guys I am stack somewhere so this is my question...
I got the following binary tree class
public class BTree
{
private TreeNode root;
int i = 0;
/////////////////////
public BTree()
{
root = null;
}
}
and my TreeNode class
public class TreeNode
{
private TreeNode left; //consider public
private int number; //
private TreeNode right; //////////////////
//////////////////////
public TreeNode(int number)
{
this.number = number;
left = null;
right = null;
}
....
}
and I need to build a method that could count Tree height till a specific data
inside a Node. For Example: int height = tree.height(5);
(to find the height of the Node that contains number 5)
I am in the part :
public int height(int number)
{
return countHeight(root, number);
}
private int countHeight(TreeNode node, int number)
{
if(node != null && node.getNumber() == number)
return 0;
return //what should I write here?
}
Recursive methods is a little hard for me so I would like
a little help over here.
Thnx in advance...
I just wrote this code and it seems to work properly.
If you got any better solution feel free to share.
public int height(int number) /// it could be Object
{
return countHeight(root, number);
}
private int countHeight(TreeNode node, int number)
{
if(node != null && node.getNumber() == number)
return 1;
if(number < node.getNumber())
return countHeight(node.getLeft()) + 1;
else
return countHeight(node.getRight()) + 1;
}

Related Links

WooCommerce - change price based on product_id, width and height
Susy gallery breaks height for display table and table-cell
Flex Slider full height and width without stretching
mPDF set Height for SetHTMLHeader
How to Set Height of PdfPTable in iTextSharp
Margin not showing with 100% width & height
KO Grid Scrollbars not visible & Display issues
Increase scrollview's element height on clicking it
Using soft keyboard on libgdx for iOS and Android (hides textview)
Fine-uploader - Scaling picture by height
Zurb foundation framework - 100% height grid
Dynamically expanding CollectionView : Height of the CollectionView itself (not the items) changing when adding new items
IE 11 bug, div with height > 1,533,917px display negative computed height and doesnt render
iText setMinimumHeight on splitted cell
Apply Document Height to a div using Jquery
jQuery UI Dialog - Height always incorrect on first show, and vertical centering incorrect when page is scrolled

Categories

HOME
jdbc
semantic-ui
shopify
puzzle
wmic
wildcard
angular2-routing
chaiscript
rebol
codeblocks
shader
contact-form-7
criteria
ctypes
automata
dlib
scenebuilder
python-unicode
sensu
cocoa-touch
tcpclient
remove-method
http-method
yosys
samsung-mobile
visual-c++-2017
geopandas
metadata-extractor
rowcount
preg-grep
google-drive-realtime-api
recurrence-relation
gettext
large-data
meta-raspberrypi
duktape
socketscan
onbackpressed
ssh.net
ttcn
catia
z3py
lirc
taskmanager
multi-agent
persistent
rollback
modelattribute
static-code-analysis
opencpu
unity3d-editor
libtiff.net
libreadline
qregexp
jackson-databind
contour
fps
django-south
supportmapfragment
sonarqube5.3
jenkins-scriptler
apigee-baas
pylearn
jai
python-ggplot
belongs-to
gmaps4rails
lumx
edit-in-place
modeshape
installshield-2009
dynamic-binding
arbor.js
mozart-mvc
code-conversion
scrollto
visual-web-gui
separation-of-concerns
smooth
for-xml-path
email-spec
clause
pci-bus
openvg
configurable
divx

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App