473,795 Members | 3,386 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Finding the levels of a Binary tree

JC
Hello,

Does anyone know how to get the level(s) in a binary tree.

For example:

(60) Level 0
/ \
(50) (65) Level 1
/ \ \
(49) (55) (68) Level 2
\
(71) Level 3

I tried counting all the "left" or "right" leaves but that doesn't work.
because of children.

Any help would be greatly appreciated.
JC
Jul 22 '05 #1
3 3961
JC wrote:
Hello,

Does anyone know how to get the level(s) in a binary tree.

For example:

(60) Level 0
/ \
(50) (65) Level 1
/ \ \
(49) (55) (68) Level 2
\
(71) Level 3

I tried counting all the "left" or "right" leaves but that doesn't work.
because of children.


How about walking the whole tree and after every step down-tree storing away
the depth if it is bigger than the old max. Seems like a simple maximum
value search - with tree walking added.

--
WW aka Attila
:::
People are more violently opposed to fur than leather because it's safer
to harass rich women than motorcycle gangs.
Jul 22 '05 #2
"JC" <ke****@secret. com> wrote...
Does anyone know how to get the level(s) in a binary tree.

For example:

(60) Level 0
/ \
(50) (65) Level 1
/ \ \
(49) (55) (68) Level 2
\
(71) Level 3

I tried counting all the "left" or "right" leaves but that doesn't work.
because of children.
Counting leaves and counting levels is not the same thing.
Any help would be greatly appreciated.


You probably need recursive traversing of the tree with keeping
the maximum depth in a variable. Every time you reach a leaf,
compare the maximum depth with its depth. Adjust the maximum
depth if needed. BTW, what's your C++ _language_ question?

Victor
Jul 22 '05 #3
JC wrote:
Hello,

Does anyone know how to get the level(s) in a binary tree.

For example:

(60) Level 0
/ \
(50) (65) Level 1
/ \ \
(49) (55) (68) Level 2
\
(71) Level 3

I tried counting all the "left" or "right" leaves but that doesn't work.
because of children.

Any help would be greatly appreciated.

struct node
{
node * branches[ 2 ];

int depth()
{
int thisdepth = 1;

if ( branches[ 0 ] )
{
thisdepth = max( thisdepth, 1 + max( branches[0].depth() );
}

if ( branches[ 1 ] )
{
thisdepth = max( thisdepth, 1 + max( branches[1].depth() );
}

return thisdepth;
}
};

Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
15316
by: Bernard Drolet | last post by:
Hi, I have a table containing many millions of rows. This table has a tree stucture, with the following columns id name parent_id I need to go through the tree, starting from a specific Id, to find all the rows with a specific name.
1
3407
by: Jerry Khoo | last post by:
hello, everybody, i am kinda new here, nice to meet u all. Now, i am > cs students and are now facing difficult problems in understanding > what a binary tree is, how it works, and the algorithm to display, > arrange, find, delete the leaf node in binary tree. > > I hope that anyone with expereince in building binary tree could help > me in explaining the binary tree functions, and give a sample code of > the whole picture behind the...
8
2781
by: Jerry Khoo | last post by:
hello, everybody, i am kinda new here, nice to meet u all. Now, i am cs students and are now facing difficult problems in understanding what a binary tree is, how it works, and the algorithm to display, arrange, find, delete the leaf node in binary tree. I hope that anyone with expereince in building binary tree could help me in explaining the binary tree functions, and give a sample code of the whole picture behind the tree. And...
11
2533
by: jova | last post by:
Is there a difference between a Binary Tree and a Binary Search Tree? If so can someone please explain. Thank You.
4
9021
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
2
2182
by: andrew browning | last post by:
gbd says the segmentation fault is being generated in the insert function. //CONSTRUCTOR tree():data(value_type()), left(0), right(0){}; tree(value_type vt, tree* l = 0, tree* r = 0): data(vt), left(l), right(r) {}; //PRIVATE MEMBERS
19
8592
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
1
2952
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;
7
3875
by: Vinodh | last post by:
Started reading about Binary Trees and got the following questions in mind. Please help. Definition of a Binary Tree from "Data Structures using C and C++ by Tanenbaum" goes like this, "A binary tree is a finite set of elements that is either empty or is partitioned into three disjoint subsets. The first subset contains a single element called the 'Root' of the tree. The other two subsets are themselves binary trees, called the 'Left'...
0
9519
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,...
0
10214
jinu1996
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...
1
10164
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,...
0
10001
tracyyun
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...
0
9042
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5437
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3723
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2920
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.