How can we add a node in a tree?

  • Context: MHB 
  • Thread starter Thread starter evinda
  • Start date Start date
  • Tags Tags
    Tree
Click For Summary

Discussion Overview

The discussion revolves around the process of adding a node to a binary tree at a specified level. Participants explore various methods to identify the appropriate location for the new node, considering conditions such as whether the target node has two children and how to traverse the tree to find the leftmost node at the specified level.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Debate/contested

Main Points Raised

  • Some participants propose a method to add a node as a child of the leftmost node at a specific level if that node does not already have two children.
  • Others suggest using a level-order traversal with a queue to find the leftmost node at the specified level before attempting to add the new node.
  • A participant questions the necessity of using a queue for traversal and suggests a recursive approach to achieve the same goal.
  • There are discussions about the structure of the node and whether certain data members need to be initialized before adding a new node.
  • Some participants express confusion regarding the parameters used in proposed functions, particularly the role of certain variables and the need for a depth parameter in recursive functions.
  • Alternative methods for tracking depth during traversal are discussed, including the use of a wrapper function or a global variable.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the best method for adding a node, with multiple competing views on traversal techniques and function structures remaining unresolved.

Contextual Notes

There are uncertainties regarding the initialization of node properties and the handling of depth tracking in recursive functions. Some assumptions about the existence of nodes at specific levels are also noted but not universally agreed upon.

Who May Find This Useful

This discussion may be useful for individuals interested in data structures, particularly binary trees, and those looking for different approaches to node manipulation within such structures.

  • #31
I like Serena said:
In your opening post it says that you have "a node $e$" instead of a node with key $e$.
So it should be like: [m]node->left=e[/m]. (Wasntme)

But at the original exercise, it says that it is a node with key $e$.. (Wasntme) (Blush)
 
Technology news on Phys.org
  • #32
evinda said:
But at the original exercise, it says that it is a node with key $e$.. (Wasntme) (Blush)

Hmm.
But doesn't that still mean you are given a "node"?
Suppose you are given a [m]nodeWithKeyE[/m] that has key $e$, then you should do:
[m]node->left = nodeWithKeyE[/m]
(Wink)
 
  • #33
I like Serena said:
Hmm.
But doesn't that still mean you are given a "node"?
Suppose you are given a [m]nodeWithKeyE[/m] that has key $e$, then you should do:
[m]node->left = nodeWithKeyE[/m]
(Wink)

So, will this node have a struct with the key, and with a pointer to the left and the right child? (Thinking)
 
  • #34
And what if the level $l$ does not exist? Do have to check this case? (Thinking)
 
  • #35
evinda said:
So, will this node have a struct with the key, and with a pointer to the left and the right child? (Thinking)

We can assume the node will be a struct with a key.
I would also assume that the left and right child have been initialized to NULL. (Nerd)
evinda said:
And what if the level $l$ does not exist? Do have to check this case? (Thinking)

Yes. We have to ensure the algorithm can also handle that. (Wink)
 
  • #36
I like Serena said:
We can assume the node will be a struct with a key.
I would also assume that the left and right child have been initialized to NULL. (Nerd)

So, do we have to do it with this command: [m] node->left->key=e [/m] ? (Thinking)
I like Serena said:
Yes. We have to ensure the algorithm can also handle that. (Wink)

How could we do this? (Thinking)
 
  • #37
evinda said:
So, do we have to do it with this command: [m] node->left->key=e [/m] ? (Thinking)

No. (Shake)
When we want to add the node with key $e$ as a left child of [m]node[/m], at that point in time [m]node->left[/m] will be equal to NULL.
If we try to access [m]node->left->key[/m] in any way, we will be redirected to chaos and madness. (Devil)
How could we do this? (Thinking)

What will happen now if level $l$ does not exist? (Wondering)
 
  • #38
I like Serena said:
No. (Shake)
When we want to add the node with key $e$ as a left child of [m]node[/m], at that point in time [m]node->left[/m] will be equal to NULL.
If we try to access [m]node->left->key[/m] in any way, we will be redirected to chaos and madness. (Devil)

So, should it be [m] node->left=e [/m] ? (Thinking)

I like Serena said:
What will happen now if level $l$ does not exist? (Wondering)

Do we have to add maybe the command of the line 11? Or am I wrong? (Worried)

Code:
1.depth=0
2.Add_node_leftmost_at_least_at_level(node, l, e)

3.  if node == NULL
4.    return failure

5.  if depth >= l and node->left == NULL
6.    node->left = e
7.    return success
8.  else if depth >= l and node->right == NULL
9.    node->right = e
10.    return success

   else
11.[B] if (node->left==NULL and node->right==NULL and depth<l) return failure;[/B]
12.  depth=depth+1
13.  result = Add_node_leftmost_at_least_at_level(node->left, l, e)
14.  if result is failure
15.    result = Add_node_leftmost_at_least_at_level(node->right, l, e)
16.  depth=depth-1
17.  return result
 
  • #39
evinda said:
So, should it be [m] node->left=e [/m] ? (Thinking)

Let's stick with that yes. (Nod)
For the purpose of our algorithm, we will require that the parameter $e$ is a node with whatever key, that has both a left and a right child of NULL.
We should state this as a precondition of the algorithm. (Mmm)
Do we have to add maybe the command of the line 11? Or am I wrong? (Worried)

That won't be necessary.
If we leave line 11 out, the algorithm will make a recursive call, and in line 3 it will be detected that we can't get to level $l$, so the algorithm will fail in line 4.
The behavior will be the same with or without line 11. (Nerd)
 
  • #40
I like Serena said:
That won't be necessary.
If we leave line 11 out, the algorithm will make a recursive call, and in line 3 it will be detected that we can't get to level $l$, so the algorithm will fail in line 4.
The behavior will be the same with or without line 11. (Nerd)

So what can we do elsewhise for the case that there are no more nodes in level $l$? I haven't understood it... (Sweating)
 
  • #41
evinda said:
So what can we do elsewhise for the case that there are no more nodes in level $l$? I haven't understood it... (Sweating)

I believe there is nothing else to do.
As it is now, the algorithm will already return a failure. (Thinking)

But don't take my word for it!
What if you program the algorithm in C and apply it to a tree that has no level $l$? (Wondering)
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
3
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K
Replies
14
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K