Tree (3)
Tree (3)
Concept of Tree
Concept of the Tree
Tree Definition
Tree is a non-linear data structure which
organizes data in hierarchical structure and
this is a recursive definition.
A tree consists of finite set of elements called
nodes and finite set of directed lines, called
branches, that connects nodes
Tree Terminologies
• Degree of a node: The number of branches
associated with node
• Indegree: When branch is directed towards
the node it is an indegree branch
• Outdegree: When branch is directed away
from the node it is an indegree branch
Degree=indegree+outdegree
1.Root:
The first node is called as Root Node.
Every tree must have root node, there must be
only one root node.
Root node doesn't have any parent i.e. indegree
of the root is 0.
Except the root all other nodes in the tree must
have indegree of exactly 1 and outdegree of 0,1 or
more
2. Edge/Branch
In a tree data structure, the connecting link between any
two nodes is called as EDGE. In a tree with 'N' number of
nodes there will be a maximum of 'N-1' number of edges.
3. Parent
In a tree data structure, the node which is predecessor of any
node is called as PARENT NODE.
In simple words, the node which has branch from it to any
other node is called as parent node i.e. outdegree is greater
than 0
4. Child
The node which has a link from its parent node is called as
child node.
Has indegree 1
In a tree, any parent node can have any number of child
nodes.
In a tree, all the nodes except root are child nodes.
5. Siblings
The nodes with same parent are called as
Sibling nodes.
6. Leaf Node
The node which does not have a child or outdegree of 0 is
called as LEAF Node.
The leaf nodes are also called as External Nodes or
'Terminal' node.
7. Internal Nodes
An internal node is a node with at least one child.
Nodes other than leaf nodes and root are called as Internal
Nodes.
The root node is also said to be Internal Node if the tree has
more than one node. Internal nodes are also called as 'Non-
Terminal' nodes.
8. Degree
In a tree data structure, the total number of
children of a node is called as DEGREE of that
Node.
9. Level
The level of node is its distance from the root
In a tree data structure, the root node is said to be at Level 0 and
the children of root node are at Level 1 and the children of the
nodes which are at Level 1 will be at Level 2 and so on...
In a tree each step from top to bottom is called as a Level and the
Level count starts with '0' and incremented by one at each level
(Step).
10. Height
The height of the tree is the level of the leaf node in the longest path
from the root plus 1
In a tree, height of the root node is said to be height of the tree.
Height of the empty tree is -1
11. Depth
In a tree data structure, the total number of egdes from root node to
a particular node is called as DEPTH of that Node.
12. Path
In a tree data structure, the sequence of Nodes and Edges from one
node to another node is called as PATH between that two Nodes.
Length of a Path is total number of nodes in that path. In below
example the path A - B - E - J has length 4.
13. Sub Tree
Subtree is any connected structure below leaf
First node in the subtree is considered as root
of subtree and used to name the subtree
Type of Trees
• General tree
• Binary tree
• Multiway Tree
General Tree
• A general tree is a data structure in that each node can have infinite
number of children .
• In general tree, root has in-degree 0 and maximum out-degree n.
• Height of a general tree is the length of longest path from root to the leaf
of tree. Height(T) = {max(height(child1) , height(child2) , … height(child-
n) ) +1}
•
Binary tree
• A Binary tree is a data structure in that each node has at most two
nodes left and right or no node can have more than 2 subtrees
• In binary tree, root has in-degree 0 and maximum out-degree 2.
• In binary tree, each node have in-degree one and maximum out-
degree 2.
• Height of a binary tree is : Height(T) = { max (Height(Left Child) ,
Height(Right Child) + 1}
• Maximum height- Hmax=N