Trees
Trees
Example
Parent
• The node which has a branch from it to any other node is called as a parent node.
• In other words, the node which has one or more children is called as a parent
node.
• In a tree, a parent node can have any number of child nodes.
Example-
Here,
• Node A is the parent of nodes B and C
• Node B is the parent of nodes D, E and F
• Node C is the parent of nodes G and H
• Node E is the parent of nodes I and J
• Node G is the parent of node K
Ancestors of node
• Ancestors: The nodes obtained in the path from the specified node x while
moving upwards towards the root node are called ancestors..
• Descendants:
• The nodes in the path below the parent are called descendants.
• In other words, the nodes that are all reachable from a node x while moving
downwards are all called descendants of x.
Siblings
• Nodes which are belong to the same parent are called as siblings.
• In other words, nodes with the same parent are sibling nodes.
Example
• A non-terminal node is any node that exists in the graph between other
nodes.
Edge
• The connecting link between any two nodes is called as an edge.
• In a tree with n number of nodes, there are exactly (n-1) number of edges.
Example
Child
Example
Here,
• Nodes B and C are the children of node A
• Nodes D, E and F are the children of node B
• Nodes G and H are the children of node C
• Nodes I and J are the children of node E
• Node K is the child of node G
Siblings
Degree
Example
Here,
• Degree of node A = 2
• Degree of node B = 3
• Degree of node C = 2
• Degree of node D = 0
• Degree of node E = 2
• Degree of node F = 0
• Degree of node G = 1
• Degree of node H = 0
• Degree of node I = 0
• Degree of node J = 0
• Degree of node K = 0
Level
Example
Path
Path refers to the sequence of nodes along the edges of a tree.
Depth
The depth of the node is the distance from the root node to that
particular node.
• Total number of edges from root node to a particular node is called as depth of
that node.
• Depth of a tree is the total number of edges from root node to a leaf node in the
longest path.
• Depth of the root node = 0
• The terms “level” and “depth” are used interchangeably.
Example
Here,
• Depth of node A = 0
• Depth of node B = 1
• Depth of node C = 1
• Depth of node D = 2
• Depth of node E = 2
• Depth of node F = 2
• Depth of node G = 2
• Depth of node H = 2
• Depth of node I = 3
• Depth of node J = 3
• Depth of node K = 3
• Height: The height of the node is the distance from that node to the
deepest node of that subtree.
• Height of tree: The Height of the tree is the maximum height of any
node. This is same as the height of root node
• Internal Node:- Internal Node means that a node which has at least
a single child.
Binary trees
Definition: A binary tree is a tree which has finite set of nodes that is either empty
or consist of a root and two subtrees called left subtree and right subtree.
Or
A binary tree is a tree data structure in which each node has at most two
children, which are referred to as the left child and the right child.
A binary tree can be partitioned into three subgroups namely root, left subtree and
right subtree.
• Root – If tree is not empty, the first node in the tree is called root node.
• left subtree – It is a tree which is connected to the left of root. Since this
tree
• comes towards left of root, it is called left subtree.
• right subtree – It is a tree which is connected to the right of root. Since this
tree comes towards right of root, it is called right subtree.
Note: In general, a tree in which each node has either zero, one or two subtrees is
called a binary tree.
The pictorial representation of a typical node in a binary tree is shown below:
Properties of binary trees
a) The maximum number of nodes on level i of a binary tree = 2i for i ≥ 0
b) The maximum number of nodes in a binary tree of depth k = 2k – 1
c) The number of leaf nodes is equal to number of nodes of degree 2
A full binary tree (Strictly binary tree or 2-tree) is a tree in which every node
other than the leaves has two children
or
A binary tree having 2i nodes in any given level i is called strictly binary tree.
Here, every node other than the leaf node has two children. A strictly binary
tree is also called full binary tree or proper binary tree.
Skewed tree
Definition: A skewed tree is a tree consisting of only left subtree or only right
subtree. A tree with only left subtrees is called left skewed binary tree and a
tree with only right subtree is called right skewed binary tree.
Complete binary tree
Definition: A complete binary tree is a binary tree in which every level, except possibly
the last level is completely filled. If the nodes in the last level are not completely filled,
then all the nodes in that level should be filled only from left to right.
For example, all the trees shown below are complete binary trees.
A binary search tree is a binary tree in which for each node say x in the tree, elements
in the left-subtree are less than info(x) and elements in the right subtree are greater
than info(x). Every node in the tree should satisfy this condition.
Basic Operations
Insert Operation