0% found this document useful (0 votes)
6 views74 pages

Unit - 3 Tree

The document provides an overview of tree data structures, defining key concepts such as root, edge, parent, child, and various types of trees including binary trees, AVL trees, and red-black trees. It explains the properties and operations associated with these trees, including tree traversals and the construction of binary search trees. Additionally, it introduces the concept of threaded binary trees, which optimize traversal by replacing NULL pointers with references to in-order predecessors and successors.

Uploaded by

Raghuveer Singh
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)
6 views74 pages

Unit - 3 Tree

The document provides an overview of tree data structures, defining key concepts such as root, edge, parent, child, and various types of trees including binary trees, AVL trees, and red-black trees. It explains the properties and operations associated with these trees, including tree traversals and the construction of binary search trees. Additionally, it introduces the concept of threaded binary trees, which optimize traversal by replacing NULL pointers with references to in-order predecessors and successors.

Uploaded by

Raghuveer Singh
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/ 74

UNIT-3

DATA STRUCTURE
Tree Data Structure

Tree is a non-linear data structure which organizes


data in a hierarchical structure and this is a
recursive definition.
OR
A tree is a connected graph without any circuits.
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
Tree Terminology
Root
The first node from where the tree originates is called
as a root node.
In any tree, there must be only one root node.
We can never have multiple root nodes in a tree data
structure.
Edge
• The connecting link between any two nodes is called
as an edge.
• In a tree with n number of nodes, there are exactly
(n-1) number of edges.
Parent
The node which has a branch from it to any other node
is called as a parent node.
In other words, the node which has one or more
children is called as a parent node.
In a tree, a parent node can have any number of child
nodes.
Child
The node which is a descendant of some node is called
as a child node.
All the nodes except root node are child nodes.
Siblings
Nodes which belong to the same parent are called
as siblings.
In other words, nodes with the same parent are sibling
nodes.
Degree
Degree of a node is the total number of children of that
node.
Degree of a tree is the highest degree of a node among
all the nodes in the tree.
Internal Node
The node which has at least one child is called as
an internal node.
Internal nodes are also called as non-terminal nodes.
Every non-leaf node is an internal node.
Leaf Node
The node which does not have any child is called as
a leaf node.
Leaf nodes are also called as external
nodes or terminal nodes.
Level
In a tree, each step from top to bottom is called as level
of a tree.
The level count starts with 0 and increments by 1 at
each level or step.
Height
Total number of edges that lies on the longest path
from any leaf node to a particular node is called
as height of that node.
Height of a tree is the height of root node.
Height of all leaf nodes = 0
Depth
• Total number of edges from root node to a particular
node is called as depth of that node.
• Depth of a tree is the total number of edges from
root node to a leaf node in the longest path.
• Depth of the root node = 0
• The terms “level” and “depth” are used
interchangeably.
Depth
Subtree
In a tree, each child from a node forms
a subtree recursively.
Every child node forms a subtree on its parent node.
Forest
• A forest is a set of disjoint trees.
Binary Tree
Binary tree is a special tree data structure in which each
node can have at most 2 children.
Thus, in a binary tree,Each node has either 0 child or 1
child or 2 children.
Types of Binary Trees
Rooted Binary Tree
A rooted binary tree is a binary tree that satisfies the
following 2 properties-
It has a root node.
Each node has at most 2 children.
Full / Strictly Binary Tree
A binary tree in which every node has either 0 or 2
children is called as a Full binary tree.
Full binary tree is also called as Strictly binary tree.
Complete / Perfect Binary Tree
A complete binary tree is a binary tree that satisfies the
following 2 properties-
Every internal node has exactly 2 children.
All the leaf nodes are at the same level.
Complete binary tree is also called as Perfect binary
tree.
Complete / Perfect Binary Tree
Almost Complete Binary Tree
An almost complete binary tree is a binary tree that
satisfies the following 2 properties-
All the levels are completely filled except possibly the
last level.
The last level must be strictly filled from left to right.
Almost Complete Binary Tree
Skewed Binary Tree
A skewed binary tree is a binary tree that satisfies the
following 2 properties-
All the nodes except one node has one and only one
child.
The remaining node has no child.
OR
A skewed binary tree is a binary tree of n nodes such
that its depth is (n-1).
Skewed Binary Tree
Binary Search Tree
Binary Search Tree is a special kind of binary tree in
which nodes are arranged in a specific order.
In a binary search tree (BST), each node contains-
Only smaller values in its left sub tree
Only larger values in its right sub tree
Binary Search Tree
Binary Search Tree Construction

Construct a Binary Search Tree (BST) for the following


sequence of numbers-
50, 70, 60, 20, 90, 10, 40, 100
Solution:-
When elements are given in a sequence,
Always consider the first element as the root node.
Consider the given elements and insert them in the BST
one by one.
Binary Search Tree Construction
• Insert 50-

• Insert 70-
• As 70 > 50, so insert 70 to the right of 50.
Binary Search Tree Construction
Insert 60-
As 60 > 50, so insert 60 to the right of 50.
As 60 < 70, so insert 60 to the left of 70.
Insert 20-
As 20 < 50, so insert 20 to the left of 50.
Binary Search Tree Construction
Insert 90-
As 90 > 50, so insert 90 to the right of 50.
As 90 > 70, so insert 90 to the right of 70.
Binary Search Tree Construction
• Insert 10-
• As 10 < 50, so insert 10 to the left of 50.
• As 10 < 20, so insert 10 to the left of 20.
Binary Search Tree Construction
• Insert 40-
• As 40 < 50, so insert 40 to the left of 50.
• As 40 > 20, so insert 40 to the right of 20.
Binary Search Tree Construction
• Insert 100-
• As 100 > 50, so insert 100 to the right of 50.
• As 100 > 70, so insert 100 to the right of 70.
• As 100 > 90, so insert 100 to the right of 90.
Practice Problem
A binary search tree is generated by inserting in order
of the following integers-
50, 15, 62, 5, 20, 58, 91, 3, 8, 37, 60, 24

100,1,10,30,50,60,70,45,55

Dec,jan,march,may,june,july,aug,april,sept,oct,nov,feb
Binary Search Tree Operations
AVL TREE
• Named after their inventor Adelson, Velski & Landis,
AVL trees are height balancing binary search tree.
• AVL tree checks the height of the left and the right
sub-trees and assures that the difference is not more
than 1.
• This difference is called the Balance Factor.
• BalanceFactor =height(left-sutree)−height(right-sutree)
EXAMPLE OF AVL TREE
AVL Rotations

To balance itself, an AVL tree may perform the following


four kinds of rotations −
• Left rotation
• Right rotation
• Left-Right rotation
• Right-Left rotation
Left Rotation

• If a tree becomes unbalanced, when a node is


inserted into the right subtree of the right subtree,
then we perform a single left rotation
Right Rotation

AVL tree may become unbalanced, if a node is inserted


in the left subtree of the left subtree. The tree then
needs a right rotation.
Left-Right Rotation

• A left-right rotation is a combination of left rotation


followed by right rotation.
State Action
A node has been inserted into the
right subtree of the left subtree. This
makes C an unbalanced node. These
scenarios cause AVL tree to perform
left-right rotation.

We first perform the left rotation on


the left subtree of C. This makes A,
the left subtree of B.
Left-Right Rotation

State Action
Node C is still unbalanced, however
now, it is because of the left-subtree
of the left-subtree.

We shall now right-rotate the tree,


making B the new root node of this
subtree. C now becomes the right
subtree of its own left subtree.
The tree is now balanced.
Right-Left Rotation

• It is a combination of right rotation followed by left


rotation..
State Action

A node has been inserted into the left


subtree of the right subtree. This
makes A, an unbalanced node with
balance factor 2.

First, we perform the right rotation


along C node, making C the right
subtree of its own left subtree B.
Now, B becomes the right subtree of
A.
Right-Left Rotation
State Action
Node A is still unbalanced because of
the right subtree of its right subtree
and requires a left rotation.

A left rotation is performed by making


B the new root node of the subtree. A
becomes the left subtree of its right
subtree B.

The tree is now balanced.


• Insert the following sequence of elements into
an AVL tree, starting with an empty tree:
• 10, 20, 15, 25, 30, 16, 18, 19.
• 1,25,2,26,3,27,4,28,5,29,6,30
• Insert the following sequence of elements into
an AVL tree, starting with an empty tree:
• 63, 9, 19, 27, 18, 108, 99, 81
• 50 , 20 , 60 , 10 , 8 , 15 , 32 , 46 , 11 , 48
• April,may,june,july,march,sept,oct,feb,jan,dec,
nov ,aug
Tree Traversals
When we wanted to display a binary tree, we need to follow
some order in which all the nodes of that binary tree must be
displayed.
In any binary tree, displaying order of nodes depends on the
traversal method.
Displaying (or) visiting order of nodes in a binary tree is
called as Binary Tree Traversal.
There are three types of binary tree traversals.
In - Order Traversal
Pre - Order Traversal
Post - Order Traversal
Example
Example
• In-Order Traversal for above example of binary tree
is
I-D-J-B-F-A-G-K-C–H

• Pre-Order Traversal for above example binary tree is


A-B-D-I-J-F-C-G-K-H

• Post-Order Traversal for above example binary tree is


I-J-D-F-B-K-G-H-C-A
Red - Black Tree

• Red - Black Tree is another variant of Binary Search


Tree in which every node is colored either RED or
BLACK. We can define a Red Black Tree as follows.
• Red Black Tree is a Binary Search Tree in which every
node is colored either RED or BLACK.
Properties of Red Black Tree

Property #1: Red - Black Tree must be a Binary Search Tree.


Property #2: The ROOT node must be colored BLACK.
Property #3: The children of Red colored node must be
colored BLACK. (There should not be two consecutive RED
nodes).
Property #4: In all the paths of the tree, there should be
same number of BLACK colored nodes.
Property #5: Every new node must be inserted with RED
color.
Property #6: Every leaf (i.e. NULL node) must be colored
BLACK.
Deletion Operation in Red Black Tree

The deletion operation in Red-Black Tree is similar to


deletion operation in BST.
But after every deletion operation, we need to check
with the Red-Black Tree properties.
If any of the properties are violated then make suitable
operations like Recolor, Rotation and Rotation
followed by Recolor to make it Red-Black Tree.
B - Tree

In search trees like binary search tree, AVL Tree, Red-


Black tree, etc., every node contains only one value
(key) and a maximum of two children.
But there is a special type of search tree called B-Tree in
which a node contains more than one value (key) and
more than two children.
B-Tree was developed in the year 1972 by Bayer and
McCreight with the name Height Balanced m-way
Search Tree. Later it was named as B-Tree.
B - Tree

B-Tree is a self-balanced search tree in which every node contains


multiple keys and has more than two children.
B-Tree of Order m has the following properties...
Property #1 - All leaf nodes must be at same level.
Property #2 - All nodes except root must have at least [m/2]-1 keys
and maximum of m-1 keys.
Property #3 - All non leaf nodes except root (i.e. all internal nodes)
must have at least m/2 children.
Property #4 - If the root node is a non leaf node, then it must have at
least 2 children.
Property #5 - A non leaf node with n-1 keys must have n number of
children.
Property #6 - All the key values in a node must be in Ascending Order.
Threaded Binary tree
• A binary tree can be represented using array representation or
linked list representation.
• When a binary tree is represented using linked list
representation, the reference part of the node which doesn't
have a child is filled with a NULL pointer.
• In any binary tree linked list representation, there is a number of
NULL pointers than actual pointers.
• Generally, in any binary tree linked list representation, if there
are 2N number of reference fields, then N+1 number of
reference fields are filled with NULL ( N+1 are NULL out of 2N ).
• This NULL pointer does not play any role except indicating that
there is no link (no child).
Threaded Binary tree
• A. J. Perlis and C. Thornton have proposed new binary tree
called "Threaded Binary Tree", which makes use of NULL
pointers to improve its traversal process.
• In a threaded binary tree, NULL pointers are replaced by
references of other nodes in the tree.
• These extra references are called as threads.
• Threaded Binary Tree is also a binary tree in which all left
child pointers that are NULL (in Linked list representation)
points to its in-order predecessor, and all right child
pointers that are NULL (in Linked list representation)
points to its in-order successor.
Representation of tree in Memory
• Array Representation
• Linked List Representation
Threaded Binary tree
• A. J. Perlis and C. Thornton have proposed new binary tree
called "Threaded Binary Tree", which makes use of NULL
pointers to improve its traversal process.
• In a threaded binary tree, NULL pointers are replaced by
references of other nodes in the tree. These extra
references are called as threads.
• Threaded Binary Tree is also a binary tree in which all left
child pointers that are NULL (in Linked list representation)
points to its in-order predecessor, and all right child
pointers that are NULL (in Linked list representation) points
to its in-order successor.
• If there is no in-order predecessor or in-order successor,
then it points to the root node.
Example
Example
• To convert the above example binary tree into
a threaded binary tree, first find the in-order
traversal of that tree...
• In-order traversal of above binary tree...
• H-D-I-B-E-A-F-J-C–G
Example
• When we represent the above binary tree using linked
list representation, nodes H, I, E, F, J and G left child
pointers are NULL. This NULL is replaced by address of
its in-order predecessor respectively (I to D, E to B, F to
A, J to F and G to C), but here the node H does not have
its in-order predecessor, so it points to the root node A.
And nodes H, I, E, J and G right child pointers are NULL.
These NULL pointers are replaced by address of its in-
order successor respectively (H to D, I to B, E to A, and J
to C), but here the node G does not have its in-order
successor, so it points to the root node A.
Threaded Binary tree
B tree
• In search trees like binary search tree, AVL Tree,
Red-Black tree, etc., every node contains only one
value (key) and a maximum of two children.
• But there is a special type of search tree called B-
Tree in which a node contains more than one
value (key) and more than two children.
• B-Tree was developed in the year 1972 by Bayer
and McCreight with the name Height Balanced
m-way Search Tree. Later it was named as B-Tree.
• CONSTRUCT A B TREE OF ORDER 3 and 4
using following elements:-
• 50,11,21,80,75,65,98,23,1,91,63,29,87,77,25,
35,82
B tree
• B-Tree is a self-balanced search tree in which every node contains
multiple keys and has more than two children.
• B-Tree of Order m has the following properties...
• Property #1 - All leaf nodes must be at same level.
• Property #2 - All nodes except root must have at least [m/2]-1 keys and
maximum of m-1 keys.
• Property #3 - All non leaf nodes except root (i.e. all internal nodes)
must have at least m/2 children.
• Property #4 - If the root node is a non leaf node, then it must
have atleast 2 children.
• Property #5 - A non leaf node with n-1 keys must have n number of
children.
• Property #6 - All the key values in a node must be in Ascending Order.
Red - Black Tree

• Red Black Tree is a Binary Search Tree in which every node is


colored either RED or BLACK.
• Properties of Red Black Tree
• Property #1: Red - Black Tree must be a Binary Search Tree.
• Property #2: The ROOT node must be colored BLACK.
• Property #3: The children of Red colored node must be colored
BLACK. (There should not be two consecutive RED nodes).
• Property #4: In all the paths of the tree, there should be same
number of BLACK colored nodes.
• Property #5: Every new node must be inserted with RED color.
• Property #6: Every leaf (e.i. NULL node) must be colored BLACK.

You might also like