0% found this document useful (0 votes)
24 views28 pages

Unit - 3 Tree Concepts

This document covers non-linear data structures, focusing on trees and graphs, including various types of trees such as binary trees, AVL trees, and B-trees. It explains tree terminology, properties, and representations, as well as the characteristics of different binary tree types like full, complete, perfect, degenerate, and balanced binary trees. Additionally, it discusses tree representation methods, including sequential and linked representations.

Uploaded by

harinik842
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views28 pages

Unit - 3 Tree Concepts

This document covers non-linear data structures, focusing on trees and graphs, including various types of trees such as binary trees, AVL trees, and B-trees. It explains tree terminology, properties, and representations, as well as the characteristics of different binary tree types like full, complete, perfect, degenerate, and balanced binary trees. Additionally, it discusses tree representation methods, including sequential and linked representations.

Uploaded by

harinik842
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

UNIT -III

• UNIT III NON-LINEAR DATA STRUCTURE


• Trees - Binary Tree - Threaded Binary Tree -
Binary Search Tree – B-Tree - B+ Tree - AVL
Tree - Splay Tree.
• Graphs: Basic Terminologies - Directed –
Undirected - Various Representations -
Operations - Graph search and traversal
algorithms - complexity analysis -
Applications of Non-Linear Data Structures
The British Constitution
Crown

Church House of House of Suprem


of Cabinet e
Lords
England Commons Court

Minister
s
County
Metropolitan
Council
police
Rural County Borough
District
Council
Council
More Trees Examples
• Unix / Windows file structure
Definition of Tree

A tree is a finite set of one or more nodes


such that:
There is a specially designated node
called
the root.
The remaining nodes are partitioned into
n>=0 disjoint sets T1, ..., Tn, where each
of these sets is a tree.
We call T1, ..., Tn the subtrees of the root.
Terminology

Node stands for item of information.


The nodes of a tree have a parent-child
relationship. The root does not have a parent; but
each one of the other nodes has a parent node
associated to it.
The number of sub-trees /child of a node is called
its degree.
A node with degree zero is called a leaf node or
terminal nodes and other nodes are referred as
non-terminals.
Eg. The degree of A is 3, F and J is zero.
The leaf node is F and J having degree zero.
• A line from a parent to a child node is called a sub tree. If a
tree has n nodes, one of which is the root there would be n-1
sub trees.
• The degree of a tree is the maximum degree of the nodes in
the tree.
• Nodes with the same parent are called siblings. Here D & E
are all siblings.
• The level of a node is defined by initially letting the root be at
level one. If a node is at level l, there its children are at level
l+1.
• The height or depth of a tree is defined to be the
maximum level of any node in the tree.

• The ancestors of a node are all the nodes along the path
from the root to the node

• A set of trees is called forest; if we remove the root of a


tree we get a forest. In the above fig, if we remove A, we
get a forest with three trees.
Root : A
Children : B, F, G are child
of node A
Siblings : H, I,J
Leaf : D, E, H, I, J
Height of tree : 4
Degree of node A : 3
Properties of a Tree:
• Any node can be the root of the tree and each node in a
tree has the property that there is exactly one path
connecting that node with every other node in the tree.
• The tree in which the root is identified is called a
rooted tree; a tree in which the root is not identified is
called a free tree.
• Each node, except the root, has a unique parent.
Binary Trees

A special class of trees: max degree for


each node is 2
Recursive definition: A binary tree is a
finite set of nodes that is either empty or
consists of a root and two disjoint binary
trees called the left subtree and the right
subtree.
Any tree can be transformed into binary
tree.
by left child-right sibling representation
A binary tree is a tree, which is, either empty or consists
of a root node and atmost two disjoint binary trees called
the left sub-tree and right sub-tree.
• Binary Tree is a tree in which no node can have more
than two children.
• A binary tree is a tree which is either empty, or one in
which every node:
• has no children; or
• has just a left child; or
• has just a right child; or
• has both a left and a right child.
Properties of Binary Tree
• At each level of i, the maximum number of nodes is 2i.
• The height of the tree is defined as the longest path from the root
node to the leaf node.
• The tree which is shown above has a height equal to 3.
Therefore, the maximum number of nodes at height 3 is equal to
(1+2+4+8) = 15.
• In general, the maximum number of nodes possible at height h is
(20 + 21 + 22+….2h) = 2h+1 -1.
• The minimum number of nodes possible at height h is equal to
h+1.
• If the number of nodes is minimum, then the height of the tree
would be maximum. Conversely, if the number of nodes is
maximum, then the height of the tree would be minimum.
• If there are 'n' number of nodes in the binary tree
Types of Binary Tree

Types of Binary tree:

• Full/ proper/ strict Binary tree


• Complete Binary tree
• Perfect Binary tree
• Degenerate Binary tree
• Balanced Binary tree
Full/ proper/ strict Binary tree

• The full binary tree is also known as a strict binary tree. The
tree can only be considered as the full binary tree if each
node must contain either 0 or 2 children.
• The full binary tree can also be defined as the tree in which
each node must contain 2 children except the leaf nodes.

In the above tree, we can observe that each node is either


containing zero or two children; therefore, it is a Full Binary
tree.
Properties of Full Binary Tree

• The number of leaf nodes is equal to the number of internal nodes


plus 1. In the above example, the number of internal nodes is 5;
therefore, the number of leaf nodes is equal to 6.
• The maximum number of nodes is the same as the number of
nodes in the binary tree, i.e., 2h+1 -1.
• The minimum number of nodes in the full binary tree is 2*h-1.
• The minimum height of the full binary tree is log2(n+1) - 1.
• The maximum height of the full binary tree can be computed as:
• n= 2*h - 1

• n+1 = 2*h

• h = n+1/2
Complete Binary Tree
• A binary tree is called complete binary tree if each node has
exactly two children and all leaf nodes need not to be at same
level or depth.
• Total number of nodes in complete binary tree are 2 h+1 – 1
where h is a height of the tree.
• The number of leaf nodes n in a complete binary tree: n = 2 h
where n is total number of leaf nodes and h is the height of the
binary tree
• The complete binary tree is a tree in which all the nodes are
completely filled except the last level. In the last level, all the nodes
must be as left as possible. In a complete binary tree, the nodes
should be added from the left.
• The above tree is a complete binary tree because all the nodes are
completely filled, and all the nodes in the last level are added at the left
first.

• Properties of Complete Binary Tree


• The maximum number of nodes in complete binary tree is 2 h+1 - 1.
• The minimum number of nodes in complete binary tree is 2 h.
• The minimum height of a complete binary tree is log2(n+1) - 1.
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.

The below tree is not a perfect binary tree because all the leaf nodes are
not at the same level.
Note: All the perfect binary trees are the
complete binary trees as well as the full
binary tree, but vice versa is not true, i.e.,
all complete binary trees and full binary
trees are the perfect binary trees.
Degenerate Binary Tree
The degenerate binary tree is a tree in which all the internal nodes
have only one children.

The above tree is a degenerate binary tree because all the


nodes have only one child. It is also known as a right-
skewed tree as all the nodes have a right child only.
The above tree is also a degenerate binary tree
because all the nodes have only one child. It is
also known as a left-skewed tree as all the nodes
have a left child only.
• Left And Right Skewed Trees
• The tree in which each node is attached as a left
child of parent node then it is left skewed tree.
• The tree in which each node is attached as a right
child of parent node then it is called right skewed
tree.
Balanced Binary Tree
The balanced binary tree is a tree in which both the left and
right trees differ by atmost 1. For example, AVL and Red-
Black trees are balanced binary tree.

The above tree is a balanced binary


tree because the difference between
the left subtree and right subtree is
zero.
The above tree is not a balanced binary tree
because the difference between the left
subtree and the right subtree is greater than
1.
Tree Representation

• The sequential representation uses an array for the


storage of tree elements.
• The number of nodes a binary tree has defines the size of
the array being used. The root node of the binary tree lies
at the array’s first index.
• The index at which a particular node is stored will define
the indices at which the right and left children of the
node will be stored.
• An empty tree has null or 0 as its first index.
• For any element in position I, the left child is in position
2i, the right child is in position (2i+1) and the parent is in
position (i/2)
Linked Representation

You might also like