You are on page 1of 23

Data Structures and Algorithms

Lecture No.17 & 18


Today topics…………….

Doubly Linked List


Primitive operation on Doubly Linked List

Tree

Terminology of Tree
Doubly Linked List
 In Doubly Linked List each node in such list
contain two pointers, one to its predecessor and
another to its successor.
 The nodes in the doubly linked list consist three
fields: an info field that contains the information
stored in the node, and left and right fields that
contain pointers to the nodes on either side.

left Info right


Doubly Linked List
 The doubly linked list may or may not be
circular.
 Struct node
{
int info;
struct node *left, *right;
} *Nodeptr;

^ ^

A B C D
Insertion at right
^ ^

P
10 20 30 40
r

q= new node
q->info= x; 25
q
r=p->right
Insertion at right
^ ^

P
10 20 30 40
r

q= new node
q->info= x; 25
q
r=p->right
r->left =q
q->right=r;
Insertion at right
^ ^

P
10 20 30 40
r

q= new node
q->info= x; 25
q
r=p->right
r->left =q
q -> left = p
q->right=r;
p-> right=q
Insertion at right
^ ^

P
10 20 30 40
r

25
q
Deletion
^ ^

P
A q B C D

r
item= p->info
q= p -> left
r= p-> right
Deletion
^ ^

P
A q B C D

r
item= P->info
q= p -> left
r= p-> right
q->right =r
r-> left = q;
Deletion
^ ^

A q B D

r
item= P->info
q= p -> left
r= p-> right
q->right =r
r-> left = q;
delete p
TREE
Nature View Of A Tree

leaves

branches

root
Computer Scientist’s View
root leaves

branches

nodes
Tree

A tree is a finite set of one or more nodes such that


(1) There is a specially designated node called the
root.
(2) The remaining node are partitioned into n>0
disjoint set T1,… Tn where each of these sets is
a tree T1,…..Tn are called the subtrees of the
root.
Tree Terminology
 The element at the top of the Tree is the
root.
 Elements next in the Level are the children
of the root.
 Elements next in the Level are the
grandchildren of the root, and so on.
 Elements that have no children are leaves.
A
root
children of root
B C D

E F G H
grand children of root

I
great grand child of root
Subtrees
A
root

B C D

E F G H

I
Levels
A
Level 1
Level 2
B C D

E F G H
Level 3

I
Level 4
Caution

 Some texts start level numbers at 0 rather


than at 1.
 Root is at level 0.
 Its children are at level 1.
 The grand children of the root are at level
2.
 And so on.
height = depth = number of levels
A Level 1
Level 2
B C D

E F G H
Level 3

I
Level 4
Node Degree = Number Of Children
A 3

2 B 1 C 1 D

0 0 1 0
E F G H

I 0
Tree Degree = Max Node Degree
A 3

2 B 1 C 1 D

0 0 1 0
E F G H

I 0
Degree of tree = 3.

You might also like