Root of tree diagram.

Thread Starter

AndrieGnd

Joined Jun 25, 2019
52
Hello.
I started learning trees on my algorithm course and I really stuck and confused on that the root of the tree isn't the first one just, it may be any other element on the tree..how is that correct? maybe I should look on the tree in another approach/aspect/view? it's really obvious and clear that the first node is the tree .. so how could be another elements on the tree also a root?! much appreciated in advance for clarification about "root" in aspect of computer science and algorithms.
should the root of the tree be just the first one? it wouldn't be acceptable that root for instance in the second level .. because it would have neighbor in the second subree and it's known that root is a root doesn't have neighbor or parents ..

here's a link for a binary tree "general tree":
https://www.google.com/search?q=Tre...AUIECgB&biw=1242&bih=568#imgrc=bCdgtXzvQdQwqM:
 

WBahn

Joined Mar 31, 2012
29,978
It's just a matter of redrawing it.

Since the root node has no parent, we can "rotate" any of it's children nodes around so that the root is now a child of that node and the former child is now the root of the tree. We can simply walk down the tree from the original root to the node that we want to become the root doing this repeatedly.
 

Thread Starter

AndrieGnd

Joined Jun 25, 2019
52
It's just a matter of redrawing it.

Since the root node has no parent, we can "rotate" any of it's children nodes around so that the root is now a child of that node and the former child is now the root of the tree. We can simply walk down the tree from the original root to the node that we want to become the root doing this repeatedly.
what do you mean .. sorry frankly didn't understand
 

WBahn

Joined Mar 31, 2012
29,978
what do you mean .. sorry frankly didn't understand
Draw the following tree:

(Node: Children)
(A: B, C, D)
(B: E)
(D: F, G)

Now draw this tree:

(D: A, F, G)
(A: B, C)
(B: E)

Do you see that all that has happened is that D has been detached as a child from the root node A and that A has been made a child of D?

Let's say that you then wanted to make F the root. Simply detach F from the root D and make D a child of F.

(F: D)
(D: A, G)
(A: B, C)
(B: E)

Remember, the basic tree structure is nothing more than a graph (some nodes connected by some edges) that has a certain property (no cycles). The edges are symmetric -- the fact that A is a child of B tells you nothing different than if B is a child of A, in both cases all you know is that these two nodes are connected by and edge.
 
Top