What is node in a tree? - data-structures

According to wiki, everything in a tree is a node.
Terminologies used in Trees
Root – The top node in a tree.
Parent – The converse notion of child.
Siblings – Nodes with the same parent.
Descendant – a node reachable by repeated proceeding from parent to child.
Ancestor – a node reachable by repeated proceeding from child to parent.
Leaf – a node with no children.
Internal node – a node with at least one child.
External node – a node with no children.
Degree – number of sub trees of a node.
Edge – connection between one node to another.
Path – a sequence of nodes and edges connecting a node with a descendant.
Level – The level of a node is defined by 1 + the number of connections between the node and the root.
Height of tree –The height of a tree is the number of edges on the longest downward path between the root and a leaf.
Height of node –The height of a node is the number of edges on the longest downward path between that node and a leaf.
Depth –The depth of a node is the number of edges from the node to the tree's root node.
Forest – A forest is a set of n ≥ 0 disjoint trees.
But then I find the following picture from SAP http://www.sapdesignguild.org/community/design/print_hierarchies2.asp
So my question - is it right to call root, leaf, parents, children, siblings in a tree as nodes?

Yes. The root is "the root node." A parent is a "parent node." A leaf is a "leaf node." The tree is made up of nodes. The terms root, parent, child, sibling, leaf, etc. just describe the relationships among nodes.
For example, the root node has no parent. Leaf nodes have no children. Sibling nodes share the same parent.

Related

How do I find a path for every leaf node in a tree?

I am using the adjacency lists for storing the tree, I want a path for every leaf node from the root node.

Fastest way to find one leaf node that is a child of a particular node in an unsorted tree

I'm implementing a Digital Search Tree's 'delete' function.
What tree traversal algorithms should I use to find a leaf node that is a child of the deleted node?
The leaf not doesn't have to be the closest one or anything. Just a leaf.
Thank you

Lowest Common Ancestor visiting every tree node only once

Can you say please, do we have a solution to find LCA of two nodes in binary tree visiting every tree node only once? Nodes only have left, right references without parent.
(O(n) time complexity).

How to prune d3 tree of 0-value leaves and of the branches that lead to them?

I have created a d3 tree layout for an n-level dataset that contains leaf nodes with value >= 0. Some branches are shorter than others, i.e., not all leaves are at the same level.
I would now like to prune the tree of those hierarchies (leaves and branches) whose sum of leaf values equals 0, and am not sure on how to do that. Any advice appreciated.
If you can navigate the tree as a normal tree, then do a post-order traversal, which visits all nodes in the tree before their parents.
When you visit each node,
Delete the node if it's a value 0 leaf
Delete the node if it has no children (clears empty branches after possibly having leaves pruned)

Is the root node an internal node?

So I've looked around the web and a couple of questions here in stackoverflow here are the definition:
Generally, an internal node is any node that is not a leaf (a node with no children)
Non-leaf/Non-terminal/Internal node – has at least one child or descendant node with degree not equal to 0
As far as i understand it, it is a node which is not a leaf.
I was about to conclude that the root is also an internal node but there seems to be some ambiguity on its definition as seen here:
What is an "internal node" in a binary search tree?
As the wonderful picture shows, internal nodes are nodes located between the root of the tree and the leaves
If we follow that definition then the root node isn't going to be counted as an internal node. So is a root node an internal node or not?
Statement from a book : Discrete Mathematics and Its Applications - 7th edition By Rosen says,
Vertices that have children are called internal vertices. The root is an internal vertex unless it is the only vertex in the graph, in which case it is a leaf.
Supportive Theorem:
For any positive integer n, if T is a full binary tree with n internal vertices, then T
has n + 1 leaves and a total of 2n + 1 vertices.
case 1:
O <- 1 internal node as well as root
/ \
O O <- 2 Leaf Nodes
case 2: Trivial Tree
O <- 0 internal vertices (no internal vertices) , this is leaf
IMHO when you are talking about a tree with more than one node we can say the root node is an internal node. When there is only one node (the root node) the question of internal node doesn't arise. Hence we can vacuously say it is an internal node.
Yes root node is an internal node.
[More explanation]
A root node is never called as a leaf node even if it is the only node present in the tree.
For ex. if a tree has only one node then we say that it is a tree with only root node, we never say that the tree has a single leaf node.
Since internal node means a non-leaf node and because root node is never considered as leaf node I would say that in case of single node tree root node is an internal node.
"A node with no children is a leaf or external node. A non-leaf node is an internal node."
Source: "Introduction To Algorithms-3rd edition" page number 1176, last line.
So, root is also an internal node except when it is the only node of the tree.

Resources