Non Linear Data Structure

Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

Tree

Non Linear Data Structure


Types of Data Structure
• A Linear data structure have data elements arranged in sequential
manner and each member element is connected to its previous and
next element.
• It allows to traverse a linear data structure in a single level and in
single run.
• Examples of linear data structures are List, Queue, Stack, Array etc.

• In non-linear data structure, each element can have multiple


paths to connect to other elements.
• It supports multi-level storage and often cannot be traversed in
single run.
• Examples of non-linear data structures are Tree, BST, Graphs etc.
What is Tree
• A tree is hierarchical data
structure.
• Organization structure of a
corporation.

• A tree is defined as finite set of


one or more data items (node)
such that
▫There is special node called
root of the tree.
▫ The remaining nodes are
partitioned into n>=0 disjoint
subsets, each of which is
itself a tree, and they are
called sub-trees.
Tree
• Node may have any number of children

• General/ Normal Tree


Tree (Non Linear Data Structure)
Tree (Non Linear Data Structure)
Tree (Non Linear Data Structure)
Glossary
• The root of a tree is a node with no parents.
• An edge refers to the link from parent to child.
• A node with no children is called leaf node/terminal node.
• Children of same parent are siblings.

• A node p is an ancestor of node q if there exists a path from root to


q an p appears on the path. The node q is called a descendant of p.

• The set of all nodes at a given depth is called the level of the tree.
The root node is at level zero.

• The number of sub-trees of a node is called its degree.


• The degree of the tree is maximum degree of the nodes in the tree.
Glossary
• Sequence of consecutive edges is called path.
• Path ending in a leaf is called a branch.
• The height of a node is the length of the path from that
node to the deepest node.
• Height of the tree is the maximum level of any node in the tree.
• The depth of a node is the length of the path from the root to
the node.
• Depth of the tree is the maximum depth among all the nodes in
the tree.
Binary tree
A binary tree T is defined as a
finite set of elements, called nodes,
such that.

1. T is empty (called the null tree or


empty tree), or

2. T contains a distinguished node R,


called the root of the T, and
remaining nodes of T form an
ordered pair of disjoint binary
trees T1 and T2.

The root of T is the node A at the


top of the diagram.
Most frequently used
Binary Tree

• A left downward slanted line from a node N indicates a left successor of


N, and a right downward slanted line from N indicates a right successor of
N.

Note: The definition of binary tree is recursive.


Binary Tree

The maximum number of nodes on level 𝑙 is 2𝑙 , where 𝑙 ≥ 0


Binary Tree

The maximum number of nodes in a binary tree of height


ℎ is 2ℎ+1 − 1

The minimum number of nodes in a binary tree of height


ℎ is ℎ + 1

The height of a binary tree with n no. nodes is


log 2 𝑛 + 1 − 1

The maximum height of a binary tree with n no. nodes is


𝑛−1
Types of binary tree
A full binary tree (sometimes proper binary tree or 2-tree)
is a tree in which every node has either 0 or 2 children.
Types of binary tree

A Binary Tree is perfect binary tree if every internal node has exactly 2
children and all the leaf node are at the same levels.
Types of binary tree

A Binary Tree is a complete Binary Tree if All the levels are completely
filled except possibly the last level. The last level must be strictly filled
from left to right.
Binary tree
Complexity
• Refer class notes

You might also like