0% found this document useful (0 votes)
11 views

Tree (3)

Uploaded by

sanketsutar6061
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Tree (3)

Uploaded by

sanketsutar6061
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

Tree Data Structure

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

• Minimum height- Hmin=Log2N + 1

• Minimum nodes- Nmin=H

• Maximum nodes- Nmax=2H-1

• Balance factor- B=HL-HR


Representation of Binary Tree
1. Array Representation
2. Linked List Representation.
Array Representation
1. To represent a tree in one dimensional array nodes are
marked sequentially from left to right start with root node.
2. First array location can be used to store no of nodes in a tree.
Linked Representation
1. This type of representation is more efficient as compared to array.
2. Left and right are pointer type fields left holds address of left child and
right holds address of right child.
3. Struct node
{
int data;
struct node * left,*right;
};
Binary Tree Types

1. Extended Binary Tree


2. Complete Binary Tree
3. Full Binary Tree
4. Skewed Binary Tree
5. Strictly Binary Tree
6. Expression Binary tree
7. Binary Search Tree
8. AVL Tree
Complete Binary Tree

A complete binary tree is a tree in which


1.All leaf nodes are at n or n-1 level
2.Levels are filled from left to right
Extended Binary Tree

An extended binary tree is a transformation of any binary tree into a


complete binary tree. This transformation consists of replacing every null
subtree of the original tree with “special nodes” or “failure nodes” .The
nodes from the original tree are then internal nodes, while the “special
nodes” are external nodes.
Full Binary Tree

A full binary tree (sometimes proper binary tree or 2-tree) is


a tree in which every node other than the leaves has two
children or no children. Every level is completely filled up.
No of nodes= 2h+1 -1
Skewed Binary Tree

• A binary tree is said to be Skewed Binary Tree if every node in


the tree contains either only left or only right sub tree.
• If the node contains only left sub tree then it is called left-
skewed binary tree
• if the tree contains only right sub tree then it is called right-
skewed binary tree.
Strictly Binary Tree
A node will have either two children or no child at all.
Expression Binary tree

• Expression trees are a special kind of binary tree used to


evaluate certain expressions.
• Two common types of expressions that a binary expression
tree can represent are algebraic and boolean.
• These trees can represent expressions that contain both unary
and binary operators.
• The leaves of a binary expression tree are operands, such as
constants or variable names, and the other nodes contain
operators.
• Expression tree are used in most compilers.
Binary Tree Traversal

1. Preorder traversal:-In this traversal method


first process root element, then left sub tree
and then right sub tree.
Procedure:-
Step 1: Visit root node
Step 2: Visit left sub tree in preorder
Step 3: Visit right sub tree in preorder
2. Inorder traversal:-
In this traversal method first process left element,
then root element and then the right element.
Procedure:-
Step 1: Visit left sub tree in inorder
Step 2: Visit root node
Step 3: Visit right sub tree in inorder
3. Postorder traversal:-

In this traversal first visit / process left sub tree,


then right sub tree and then the root element.
Procedure:-
Step 1: Visit left sub tree in postorder
Step 2: Visit right sub tree in postorder
Step 3: Visit root node
Binary Search Tree
A binary search tree (BST) or
"ordered binary tree" is a empty or in which
each node contains a key that satisfies
following conditions:
1. All keys are distinct.
2. all elements in its left subtree are less to the root node
(<), and all the elements in its right subtree are greater
than the root node (>).
Binary search tree
Binary search tree property
For every node X
All the keys in its left
subtree are smaller than
the key value in X
All the keys in its right
subtree are larger than the
key value in X
Binary Search Trees

A binary search tree Not a binary search tree

You might also like