Unit 6 - Trees
Unit 6 - Trees
INTRODUCTION OF TREES
• A tree is recursively defined as a set Root node
of one or more nodes where one A
node is designated as the root of the T1 T3
tree and all the remaining nodes can T2
be partitioned into non-empty sets B C D
each of which is a sub-tree of the
root
E F G H I
• Figure shows a tree where node A is
the root node; nodes B, C, and D are
children of the root node and form J K
sub-trees of the tree rooted at node
A.
Basic Terminology
• Root node The root node R is the topmost node in the tree. If R = NULL, then it
means the tree is empty.
• Sub-trees If the root node R is not NULL, then the trees T1 , T2 , and T3 are called
the sub-trees of R.
• Leaf node A node that has no children is called the leaf node or the terminal node.
• Path A sequence of consecutive edges is called a path. For example, in Fig. 9.1, the
path from the root node A to node I is given as: A, D, and I
• Ancestor node An ancestor of a node is any predecessor node on the path from root
to that node. The root node does not have any ancestors. In the tree given in Fig.
nodes A, C, and G are the ancestors of node K.
• Descendant node A descendant node is any successor node on any path from the
node to a leaf node. Leaf nodes do not have any descendants. In the tree given in
Fig. nodes C, G, J, and K are the descendants of node A.
continue
• Level number Every node in the tree is assigned a level number in such a
way that the root node is at level 0, children of the root node are at level
number 1. Thus, every node is at one level higher than its parent. So, all
child nodes have a level number given by parent’s level number + 1.
• Degree Of Node Degree of a node is equal to the number of children that a
node has. The degree of a leaf node is zero.
• Degree Of Tree In a tree data structure, the degree of a tree is the highest
degree of any node in the tree.
• In-degree In-degree of a node is the number of edges arriving at that node
• Out-degree Out-degree of a node is the number of edges leaving that node.
• Height of tree –The height of a tree is the number of edges on the longest
downward path between the root and a leaf.
TYPES OF TREES
• General trees
• Binary trees
• Binary search trees
• Expression trees
• Heap Tree
General trees
• General trees are data structures that store elements hierarchically.
• The top node of a tree is the root node and each node, except the root, has a parent.
• A node in a general tree (except the leaf nodes) may have zero or more sub-trees. General
trees which have 3 sub-trees per node are called ternary trees. However, the number of
sub-trees for any node may be variable. For example, a node can have 1 sub-tree, whereas
some other node can have 3 sub-trees.
• Although general trees can be represented as ADTs, there is always a problem when
another sub-tree is added to a node that already has the maximum number of sub-trees
attached to it.
• Even the algorithms for searching, traversing, adding, and deleting nodes become much
more complex as there are not just two possibilities for any node but multiple
possibilities.
• To overcome the complexities of a general tree, it may be represented as a graph data
structure thereby losing many of the advantages of the tree processes.
• Therefore, a better option is to convert general trees into binary trees.
Binary trees
• A binary tree is a data structure that is defined as a collection of elements
called nodes.
• In a binary tree, the topmost element is called the root node, and each node
has 0, 1, or at the most 2 children.
• A node that has zero children is called a leaf node or a terminal node. Every
node contains a data element, a left pointer which points to the left child,
and a right pointer which points to the right child.
• The root element is pointed by a 'root' pointer. If root = NULL, then it means
the tree is empty
Binary trees
Root node
• In the figure, R is the root node and the two
trees T1 and T2 are called the left and right 1
sub-trees of R.
• T1 is said to be the left successor of R. T1 2 T2 3
The left and the right child of the root node have
a level number 1.Similarly, every node is at one
level higher than its parents. So all child nodes
are defined to have level number as parent's level (level 3) 8 9 10 11 12
number + 1.
Sibling All nodes that are at the same level and
Degree of a node It is equal to the number of share the same parent are called siblings
children that a node has. The degree of a leaf
node is zero. (brothers). For example, nodes 2 and 3; nodes
4 and 5; nodes 6 and 7; nodes 8 and 9; and
For example, in the tree, degree of node 4 is 2, nodes 10 and 11 are siblings.
continue.. • Similar Binary Trees
• Leaf node A node that has no children is called a leaf F
node or a terminal node. The leaf nodes in the tree A
are: 8, 9, 5, 10, 11, and 12. G H
B C
• Similar binary trees Two binary trees T and T' are said
to be similar if both these trees have the same
D I
structure.
• Copies Two binary trees T and T' are said to be copies J
E
if they have similar structure and if they have same • T' is a copy of T
content at the corresponding nodes. •
Tree T Tree T'
• Edge It is the line connecting a node N to any of its
successors. A binary tree of n nodes has exactly n – 1
edges because every node except the root node is A
connected to its parent via an edge A
• Min Heap: The value of the • Max Heap: The value of the
parent node should be less parent node is greater than or
than or equal to either of equal to its children.
its children.