TREE Data Structure
TREE Data Structure
STRUCTURE
Unit-IV
Tree Data Structure
• Tree is a non-linear data structure which organizes data in a
hierarchical structure and this is a recursive definition.
OR
OR
• If in a graph, there is one and only one path between every pair of
vertices, then graph is called as a tree.
Example
Properties of Tree Data Structure
• Consider we want to draw all the binary trees possible with 3 unlabeled
nodes.
• Number of binary trees possible with 3 unlabeled nodes
= 2 x 3C3 / (3 + 1)
= 6C 3 / 4
=5
• Thus, With 3 unlabeled nodes, 5 unlabeled binary trees are possible.
• These unlabeled binary trees are as follows-
Labeled Binary Tree
• A binary tree is labeled if all its nodes are assigned a label.
Example
• Consider we want to draw all the binary
trees possible with 3 labeled nodes.
• Number of binary trees possible with 3
labeled nodes
= { 2 x 3C3 / (3 + 1) } x 3!
= { 6C 3 / 4 } x 6
=5x6
= 30
struct Node {
int data;
Property-01:
Minimum number of nodes in a
binary tree of height H = H + 1
• Example-
• Here,
Number of leaf nodes = 3
Number of nodes with 2 children = 2
• Application-
• Level order traversal is used to print the data in
the same order as stored in the array
representation of a complete binary tree.
Example
Threaded Binary Tree
• In the linked representation of binary trees, more than one half of the
link fields contain NULL values which results in wastage of storage
space. If a binary tree consists of n nodes, then n+1 link fields
contain NULL values.
• So, in order to effectively manage the space, a method was devised by
Perlis and Thornton in which the NULL links are replaced with special
links known as threads. Such binary trees with threads are known as
threaded binary trees.
• A Threaded Binary Tree is a variant of a normal Binary Tree that
facilitates faster tree traversal (in-order) and does not require a Stack
or Recursion.
• It decreases the memory wastage by setting the null pointers of a
leaf node to the in-order predecessor or in-order successor.
Types of Threaded Binary tree
• There are two types of Threaded Binary Trees: