MHB Writing an Algorithm to Check Depth of Leaves in an Ordered Tree

Click For Summary
The discussion focuses on creating an algorithm to determine if all leaves in an ordered tree, derived from a binary tree, are at the same depth. Participants clarify the definitions and relationships between ordered trees and binary trees, emphasizing that an ordered tree can have multiple children per node, while a binary tree cannot. The algorithm should traverse the tree to find the depth of each leaf, utilizing a pre-order traversal method. It is noted that during traversal, the depth of each leaf should be recorded and compared to ensure uniformity across all leaves. The conversation concludes with a consensus on the approach to implement the algorithm effectively.
evinda
Gold Member
MHB
Messages
3,741
Reaction score
0
Hello! (Wave)

I want to write an algorithm, that implements an ordered tree(not necessary binary tree). It should check if all the leaves of the ordered tree (that is implemented from the binary tree) are at the same depth.

Could you give me some hints how I could do this? (Thinking)
 
Technology news on Phys.org
Suppose that we have, for example, this tree:

View attachment 3548

Could you explain me how we found this binary search tree:

View attachment 3551
in order to make operations at the first tree? (Thinking)
 

Attachments

  • tree6.png
    tree6.png
    4.4 KB · Views: 162
  • binary_tree1.png
    binary_tree1.png
    4.4 KB · Views: 153
Last edited:
This isn't the right corresponding binary tree, right? (Thinking)

Because, I read that at a binary search tree, each node has no more than two child nodes.
 
evinda said:
Hello! (Wave)

I want to write an algorithm, that implements an ordered tree(not necessary binary tree). It should check if all the leaves of the ordered tree (that is implemented from the binary tree) are at the same depth.

Could you give me some hints how I could do this? (Thinking)

Hi! (Blush)

What should the algorithm implement? (Wondering)
An ordered tree (not necessarily binary)?
Or a test whether a binary tree has all leaves at the same depth?
Or a test whether an ordered tree, that is constructed from a binary tree, has all leaves at the same depth?
Or something else? :confused:
evinda said:
Suppose that we have, for example, this tree:

Could you explain me how we found this binary search tree:

in order to make operations at the first tree? (Thinking)

This seems to be the wrong way around. :confused:
If we have a tree like the second one, we can convert it into the binary tree that is shown first. (Wasntme)

evinda said:
This isn't the right corresponding binary tree, right? (Thinking)

Because, I read that at a binary search tree, each node has no more than two child nodes.

Check. $\checkmark$
 
I like Serena said:
Hi! (Blush)

What should the algorithm implement? (Wondering)
An ordered tree (not necessarily binary)?
Or a test whether a binary tree has all leaves at the same depth?
Or a test whether an ordered tree, that is constructed from a binary tree, has all leaves at the same depth?
Or something else? :confused:

The algorithm should test, whether an ordered tree, that is constructed from a binary tree, has all leaves at the same depth.. (Nod) (Blush)
I like Serena said:
This seems to be the wrong way around. :confused:
If we have a tree like the second one, we can convert it into the binary tree that is shown first. (Wasntme)

A ok.. (Nod) But, could you explain me how we can do this? (Thinking)
 
evinda said:
The algorithm should test, whether an ordered tree, that is constructed from a binary tree, has all leaves at the same depth.. (Nod) (Blush)

Actually, it's still the wrong way around.
I only know how to construct an ordered binary tree from an ordered tree. (Doh)
A ok.. (Nod) But, could you explain me how we can do this? (Thinking)

Well, we start with an ordered tree, which is not binary, as in your example where the root has 3 children.
Then we keep the link of each node to its leftmost child, and we remove all other child links, replacing them by sibling links.
Presto! An ordered binary tree. (Whew)
 
I like Serena said:
Actually, it's still the wrong way around.
I only know how to construct an ordered binary tree from an ordered tree. (Doh)

Well, we start with an ordered tree, which is not binary, as in your example where the root has 3 children.
Then we keep the link of each node to its leftmost child, and we remove all other child links, replacing them by sibling links.
Presto! An ordered binary tree. (Whew)

So, does this mean that we take the root, its child is the leftmost node from the next level and the other nodes of this level are the siblings of the leftmost node and so on..? (Thinking)

Or have I understood it wrong? :confused:
 
evinda said:
So, does this mean that we take the root, its child is the leftmost node from the next level and the other nodes of this level are the siblings of the leftmost node and so on..? (Thinking)

Or have I understood it wrong? :confused:

That sounds right.
Those siblings become child, grandchild, and great grandchild. (Nerd)
 
I like Serena said:
That sounds right.
Those siblings become child, grandchild, and great grandchild. (Nerd)

Nice.. (Nerd) And how can we find the depth of the leaves? (Thinking)
 
  • #10
evinda said:
Nice.. (Nerd) And how can we find the depth of the leaves? (Thinking)

Count the number of edges from the root to each leave? (Thinking)
 
  • #11
I like Serena said:
Count the number of edges from the root to each leave? (Thinking)

How do we know where the leaves are, at the ordered tree? (Thinking)
 
  • #12
evinda said:
How do we know where the leaves are, at the ordered tree? (Thinking)

If the root has any nodes connected to it, it's not a leaf, but an internal node.
The connected nodes are the children of the root, which are each at depth 1. (Thinking)

If any child has nodes connected to it other than its parent, it's not a leaf, but an internal node, and those nodes are in turn its children, which have a depth that is 1 greater. (Wasntme)

Any child that has no other nodes connected to it other than its parent, is a leaf.
It has a depth that is 1 greater than its parent. (Nerd)
 
  • #13
So, do we conclude the following? :confused:

I like Serena said:
If the root has any nodes connected to it, it's not a leaf, but an internal node.
The connected nodes are the children of the root, which are each at depth 1. (Thinking)

The node a is not a leaf.

I like Serena said:
If any child has nodes connected to it other than its parent, it's not a leaf, but an internal node, and those nodes are in turn its children, which have a depth that is 1 greater. (Wasntme)
The node b is the only child of a.

The nodes c,e are not leaves, and also they are children of b.

I like Serena said:
Any child that has no other nodes connected to it other than its parent, is a leaf.
It has a depth that is 1 greater than its parent. (Nerd)

The only leaves are k,l,i.Or have I understood it wrong? (Worried)
 
  • #14
evinda said:
So, do we conclude the following? :confused:

The node a is not a leaf.
The node b is the only child of a.
The nodes c,e are not leaves, and also they are children of b.
The only leaves are k,l,i.

Or have I understood it wrong? (Worried)

You have understood it right! (Nod)
 
  • #15
I like Serena said:
You have understood it right! (Nod)

Nice... (Smile) And, do we have to traverse now the tree in pre-order/ in-order/ post-order, in order to find the leaves? (Thinking)
Or, do we have to do something else? :confused:
 
  • #16
evinda said:
And, do we have to traverse now the tree in pre-order/ in-order/ post-order, in order to find the leaves? (Thinking)
Or, do we have to do something else? :confused:

Yes. (Nod)
Those are the most common ways to traverse a tree to do anything with it.
In your case to figure out where the leaves are and what their depth is. (Nerd)
 
  • #17
I like Serena said:
Yes. (Nod)
Those are the most common ways to traverse a tree to do anything with it.
In your case to figure out where the leaves are and what their depth is. (Nerd)

In our case, do we have to traverse the tree in pre-order? (Thinking)
 
  • #18
evinda said:
In our case, do we have to traverse the tree in pre-order? (Thinking)

Actually, the algorithm to traverse a tree implements all 3 forms at once.
For this problem it's not relevant which order to take. (Wasntme)
 
  • #19
I like Serena said:
Actually, the algorithm to traverse a tree implements all 3 forms at once.
For this problem it's not relevant which order to take. (Wasntme)

When we apply all forms at once, don't we traverse three times the tree? (Worried)
Or am I wrong? (Thinking)
 
  • #20
Assume your tree is implemented with nodes with child (youngest child) and sibling pointers. For such a tree, a node p is a leaf if and only if p.child == null.

So first find the depth of some leaf node for a non-empty tree:
Code:
depth=0;
p=root;
while (p.child != null) {
  depth=depth+1;
  p=p.child;
}
Now do a non-recursive pre-order traversal with the aid of a stack s:

Code:
push(root,0);
while (!s.isEmpty()) {
  s.pop(p,d);
  if (p.child == null && d != depth) {
    return(false);
  }
  if (p.sibling != null) {
    push(p.sibling,d);
  }
  if (p.child != null) {
    push(p.child,d+1);
  }
}
return(true);

I think it's interesting to note that the above pre-order traversal is exactly the same as a level order traversal except that a stack is used instead of a queue.
 
  • #21
evinda said:
When we apply all forms at once, don't we traverse three times the tree? (Worried)
Or am I wrong? (Thinking)

It's one traversal that is effectively pre-order.
But we can "do something" in the order we want. (Thinking)

The algorithm is:
Code:
traverseTree(node)
    if node == NULL
        return

    Do something in pre-order

    traverseTree(node->left)

    Do something in in-order

    traverseTree(node->right)

    Do something in post-order
(Wasntme)
 
  • #22
I like Serena said:
It's one traversal that is effectively pre-order.
But we can "do something" in the order we want. (Thinking)

The algorithm is:
Code:
traverseTree(node)
    if node == NULL
        return

    Do something in pre-order

    traverseTree(node->left)

    Do something in in-order

    traverseTree(node->right)

    Do something in post-order
(Wasntme)

I haven't understood how we can apply the three different forms of tree traversal, in order to check if all the leaves have the same depth.. (Sweating)
Could you explain it further to me? :confused:
 
  • #23
evinda said:
I haven't understood how we can apply the three different forms of tree traversal, in order to check if all the leaves have the same depth.. (Sweating)
Could you explain it further to me? :confused:

Let's take a look at the recursive algorithm again:
Code:
traverseTree(node, depth)
  if node = NULL
    // We're in the non-existing child of a leaf here.
    // This is the place where we can do something with the depth.
    return
  for each child of node
    traverseTree(child, depth + 1)

What might we do when we are in the final case of the recursive function? (Wondering)
 
  • #24
I think I detect some confusion on your part. Here's a tree:

f0so5u.png


Now here's the associated binary tree:

2q3akat.png
 
  • #25
I like Serena said:
Let's take a look at the recursive algorithm again:
Code:
traverseTree(node, depth)
  if node = NULL
    // We're in the non-existing child of a leaf here.
    // This is the place where we can do something with the depth.
    return
  for each child of node
    traverseTree(child, depth + 1)

What might we do when we are in the final case of the recursive function? (Wondering)

When we have, for example, this tree:

View attachment 3594

we call the functions [m]traverseTree(a,depth), traverseTree(b,depth+1), traverseTree(e,depth+2), traverseTree(f,depth+3), traverseTree(h,depth+4), traverseTree(i,depth+5)[/m], right? But.. what do we do after the command [m] return; [/m] , that is executed at the function [m]traverseTree(i,depth+5)[/m]? (Worried)
 

Attachments

  • tree8.png
    tree8.png
    3.5 KB · Views: 124
  • #26
evinda said:
When we have, for example, this tree:

we call the functions [m]traverseTree(a,depth), traverseTree(b,depth+1), traverseTree(e,depth+2), traverseTree(f,depth+3), traverseTree(h,depth+4), traverseTree(i,depth+5)[/m], right? But.. what do we do after the command [m] return; [/m] , that is executed at the function [m]traverseTree(i,depth+5)[/m]? (Worried)

Right. (Smile)

And btw, the depth is initially 0, so the initial call is actually [m]traverseTree(a, 0)[/m]. (Nerd)The first time we get to the [m]return[/m], we need to register the depth.
Every other time, we need to check if the depth is the same as the one we previously registered. (Thinking)
 
  • #27
I like Serena said:
Right. (Smile)

And btw, the depth is initially 0, so the initial call is actually [m]traverseTree(a, 0)[/m]. (Nerd)The first time we get to the [m]return[/m], we need to register the depth.
Every other time, we need to check if the depth is the same as the one we previously registered. (Thinking)

Now, I noticed that [m] e [/m] has no children, [m]f[/m] is a sibling of [m] e [/m].
Or am I wrong? (Thinking)
 
  • #28
evinda said:
Now, I noticed that [m] e [/m] has no children, [m]f[/m] is a sibling of [m] e [/m].
Or am I wrong? (Thinking)

No. [m]f[/m] is a child of [m]e[/m].
It's only that you've drawn them horizontally next to each other. (Mmm)
 
  • #29
I like Serena said:
No. [m]f[/m] is a child of [m]e[/m].
It's only that you've drawn them horizontally next to each other. (Mmm)

So, do we have to initialize an other variable for the next leaf? :confused:

But.. we don't know how many there are, right? (Thinking)
 
  • #30
evinda said:
So, do we have to initialize an other variable for the next leaf? :confused:

But.. we don't know how many there are, right? (Thinking)

I don't understand your questions. (Doh)
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 10 ·
Replies
10
Views
3K
Replies
1
Views
1K
  • · Replies 5 ·
Replies
5
Views
2K