You are on page 1of 11

DATA STRUCTURES

TREES
A tree data structure is a non-linear data structure because it does
not store in a sequential manner. It is a hierarchical structure as
elements in a Tree are arranged in multiple levels. In the Tree data
structure, the topmost node is known as a root node.

ROOT NODE
BASIC TERMINOLOGY

• The connecting link between any two nodes is called an edge.


– In a tree with n number of nodes there are n-1 number of edges.
• The node which is descendant of any node is called as child
node. So all the nodes except root node are child nodes.
– In a tree any parent can have any number of child nodes.
• Children’s of the same parent are siblings.
– B,C,D are siblings
– E,F are siblings
– J,K are siblings
– H,I are siblings
BASIC TERMINOLOGY

• The total number of children of a node is called as degree of


that node.
– Degree of Node A is 3
• The node with degree 0 is a leaf or terminal node. They are
also called as External nodes.
– E,F,J,K,H,I
• Degree of a tree is the highest degree of a node among all the
nodes in the tree.
– Degree of the above tree is 3.
BASIC TERMINOLOGY

• The node which has at least one child is called Internal node.
They are also called non-leaf or non-terminal nodes.
– A,B,C,D,G are internal nodes
• The node which does not have a child is called External node.
They are also called leaf or terminal nodes.
– E,F,J,K,H,I are external nodes
• In a tree each step from top to bottom is called as level and
the level count starts with 0 and incremented by 1 at each
step.
– A is at level 0. B,C,D are at level 1. E,F,G,H,I are at level 2. J,K are at
level 3.
BASIC TERMINOLOGY

• The total number of edges from particular node to leaf node


in the longest path is called the height of that node.
– Height of C is 2, Height of A is 3.
– Height of the tree = Height of the root node
– Height of all leaf nodes = 0
• The total number of edges from root node to a particular
node is called Depth of that node.
– Depth of A is 0, Depth of B is 1, Depth of K is 3.
• In a tree each child from a node forms a sub-tree.
• Forest is a set of disjoint trees.
A

B C

D E F G
BINARY TREES

• A binary tree, is a tree in which no node can have more than


two children.
• Degree of the binary tree is always 2.
• There are two types of binary tree
– Full binary tree
– Perfect binary tree
FULL BINARY TREE
• The tree can only be considered
as the full binary tree if each
node must contain either 0 or 2
children.

PERFECT BINARY TREE


• A tree is a perfect binary tree if all
the internal nodes have 2
children, and all the leaf nodes
are at the same level.
BINARY SEARCH TREE
• A binary search tree follows some
order to arrange the elements. In
a Binary search tree, the value of
left node must be smaller than
the parent node, and the value of
right node must be greater than
the parent node. This rule is
applied recursively to the left and
right sub-trees of the root.
Advantages of Binary search tree
• Searching an element in the
Binary search tree is easy as we
always have a hint that which
sub-tree has the desired element.
• As compared to array and linked
lists, insertion and deletion
operations are faster in BST.
TRAVERSAL IN BINARY TREE
• There are three ways to traverse in
binary tree.
– In-order traversal
– Pre-order traversal
– Post-order traversal
IN-ORDER TRAVERSAL
• Visit left node first then root node
and last right node. (left-root-right)
• In order traversal of given tree is:
– 25,30,35,40,45,50,60
PRE-ORDER TRAVERSAL
• Visit root node first then left node
and last right node. (root-left-right)
• Pre order traversal of given tree is:
– 40,30,25,35,50,45,60
POST-ORDER TRAVERSAL
• Visit left node first then right node
and last root node. (left-right-root)
• In order traversal of given tree is:
– 25,35,30,45,60,50,40

You might also like