• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Binary Tree

 
Ranch Hand
Posts: 373
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How would I write a method to check if second node is a descendant of the first node? I believe this should be the heading of the code:
 
Saloon Keeper
Posts: 9714
79
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How many descendants can a TreeNode have? Where does it keep track of them?
 
Ana Smith
Ranch Hand
Posts: 373
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:How many descendants can a TreeNode have? Where does it keep track of them?

functionCall.jpg
[Thumbnail for functionCall.jpg]
 
Carey Brown
Saloon Keeper
Posts: 9714
79
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:How many descendants can a TreeNode have? Where does it keep track of them?


You didn't answer my question.
 
Ana Smith
Ranch Hand
Posts: 373
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:

Carey Brown wrote:How many descendants can a TreeNode have? Where does it keep track of them?


You didn't answer my question.


A treenode can have infinite descendants right?
 
Carey Brown
Saloon Keeper
Posts: 9714
79
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ana Smith wrote:

Carey Brown wrote:

Carey Brown wrote:How many descendants can a TreeNode have? Where does it keep track of them?


You didn't answer my question.


A treenode can have infinite descendants right?


I don't know. You've supposedly have the source code for TreeNode, what does it say? How does it keep track of them?
 
Ana Smith
Ranch Hand
Posts: 373
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Carey Brown
Saloon Keeper
Posts: 9714
79
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, now that you have that, what's the answer?
 
Carey Brown
Saloon Keeper
Posts: 9714
79
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Perhaps to be clearer: "immediate descendants"
 
Ana Smith
Ranch Hand
Posts: 373
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A TreeNode can have 2 immediate descendants s and it keeps track by using getLeft() and getRight()
 
Carey Brown
Saloon Keeper
Posts: 9714
79
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, if you have a "first" node and a "second" node, how do you tell if second is an IMMEDIATE descendant of first?
 
Ana Smith
Ranch Hand
Posts: 373
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:So, if you have a "first" node and a "second" node, how do you tell if second is an IMMEDIATE descendant of first?


 
Carey Brown
Saloon Keeper
Posts: 9714
79
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Very good. So let's say that it wouldn't return true, that doesn't tell us if it is a distant descendant. How would we tell if first had no more descendants, and what should we do in that case?
 
Ana Smith
Ranch Hand
Posts: 373
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Very good. So let's say that it wouldn't return true, that doesn't tell us if it is a distant descendant. How would we tell if first had no more descendants, and what should we do in that case?


public boolean isDescendant(TreeNode first, TreeNode second)
{
    if(first.getLeft()==second || first.getRight()==second)
    {
           return true;

     }
     else if (first==null)
          return false;
      else
           return isDescendant(first,second);

 
}
 
Carey Brown
Saloon Keeper
Posts: 9714
79
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oopsare we looking for first or second?
 
Carey Brown
Saloon Keeper
Posts: 9714
79
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're looking to see if first is null after you've already used first. You would have gotten a null pointer exception.
 
Carey Brown
Saloon Keeper
Posts: 9714
79
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
 
Ana Smith
Ranch Hand
Posts: 373
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Carey Brown
Saloon Keeper
Posts: 9714
79
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If first was null then line 3 would have thrown an exception.
 
And then we all jump out and yell "surprise! we got you this tiny ad!"
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic