Final DS Mod3
Final DS Mod3
Final DS Mod3
The difficulties with single linked lists is that, it is possible to traversal only in one
direction, ie., direction of the links.
The only way to find the node that precedes p is to start at the beginning of the list.
The same problem arises when one wishes to delete an arbitrary node from a
singly linked list. Hence the solution is to use doubly linked list
Doubly linked list: It is a linear collection of data elements, called nodes, where
each node N is divided into three parts:
1. An information field INFO which contains the data of N
2. A pointer field RLINK (FORW) which contains the location of the next node in
the list
3. A pointer field LLINK (BACK) which contains the location of the preceding
node in the list
The declarations are:
typedef struct node *nodePointer;
typedef struct {
nodePointer llink;
element data;
nodePointer rlink;
} node;
Insertion into a doubly linked list
Assume there are two nodes, node and newnode, node may be either a header node
or an interior node in a list. The function dinsert performs the insertion operation in
constant time.
void dinsert(nodePointer node, nodePointer newnode)
{/* insert newnode to the right of node */
newnode→llink = node;
newnode→rlink = node→rlink;
node→rlink→llink = newnode;
node→rlink = newnode;
}
Inserting a node at the end
COMP104 Doubly Linked Lists / Slide 12
Inserting a Node
Insert a node New before Cur (not at front or
rear)
New->next = Cur;
New->prev = Cur->prev;
Cur->prev = New;
(New->prev)->next = New;
10 20 55 70
Head 40
Cur
New
Deletion from a doubly linked list
Deletion from a doubly linked list is equally easy. The function ddelete deletes the
node deleted from the list pointed to by node.
To accomplish this deletion, we only need to change the link fields of the nodes that
10 20 40 55 70
Head Cur
Delete a node Cur in the middle
(Cur->prev)->next = Cur->next;
(Cur->next)->prev = Cur->prev;
free Cur; // same as delete front!
10 20 40 55 70
Cur
Head
Circular Linked List
typedef struct node
{
int info;
struct node * link;
}NODE;
NODE * last=NULL:
Insert at Front
void insert_front (int data)
{
NODE *newnode=NULL;
newnode = (NODE *)malloc(sizeof(NODE));
if (last == NULL)
{
newnode->info = data;
newnode->link=newnode;
last = newnode;
}
else
{
newnode->info = data;
newnode->link = last->link;
last->link = newnode;
}
}
Insert at End
void insert_end (int data)
{
NODE *newnode=NULL;
newnode = (NODE *)malloc(sizeof(NODE));
if (last == NULL)
{
newnode->info = data;
newnode->link=newnode;
last = newnode;
}
else
{
newnode->info = data;
newnode->link = last->link;
last->link = newnode;
last=newnode;
}
}
Delete at Front
void del_front ( )
{
NODE * temp;
if (last == NULL)
printf(“list is empty\n”);
else
{
if(last->link==last)
{
free(last);
last=NULL;
}
else
{
temp=last->link;
last->link=temp-> link;
free(temp);
}
Delete at end
void del_end ( )
{
NODE * temp;
if (last == NULL)
printf(“list is empty\n”);
else
{
if(last->link==last)
{
free(last);
last=NULL;
}
else
{
temp=last->link;
while(temp->link!=last)
temp=temp->link
temp-> link=last->link;
last=temp;
}
Display()
void display()
{
if (last == NULL)
printf("\nList is empty\n");
else
{
NODE *temp;
temp = last->link;
do {
printf("\nData = %d", temp->info);
temp = temp->link;
} while (temp != last->link);
}
}
HEADER LINKED LISTS
A header linked list is a linked list which contains a special node, called the header
node, at the beginning of the list.
The following are two kinds of widely used header lists:
1. A grounded header list is a header list where the last node contains the null
pointer.
2. A circular header list is a header list where the last node points back to the header
node.
Algorithm: (Traversing a Circular
Header List)
Let LIST be a circular header list in memory. This algorithm traverses LIST,
applying an operation PROCESS to each node of LIST.
1. Set PTR: = START→LINK. [Initializes the pointer PTR.]
2. Repeat Steps 3 and 4 while PTR ≠ START:
3. Apply PROCESS to PTR→INFO.
4. Set PTR: = PTR→LINK. [PTR now points to the next node.]
[End of Step 2 loop.]
5. Exit
Algorithm: SRCHHL (INFO, LINK, START,
ITEM, LOC)
LIST is a circular header list in memory. This algorithm finds the location LOC of
the node where ITEM first appears in LIST or sets LOC = NULL.
1. Set PTR: = START→LINK
2. Repeat while PTR→INFO [PTR] ≠ ITEM and PTR ≠ START:
Set PTR: =PTR→LINK.
[PTR now points to the next node.]
[End of loop.]
3. If PTR→INFO = ITEM,
then: Set LOC: = PTR.
Else:Set LOC: = NULL.
[End of If structure.]
4. Exit
The two properties of circular header lists:
1. The null pointer is not used, and hence all pointers contain valid addresses.
2. Every (ordinary) node has a predecessor, so the first node may not require a special
case.
There are two other variations of linked lists
3. A linked list whose last node points back to the first node instead of containing the
null pointer, called a circular list
4. A linked list which contains both a special header node at the beginning of the list
and a special trailer node at the end of the list
Introduction
A tree structure means that the data are organized so that
items of information are related by branches
It is a Nonlinear non primitive data structure.
Examples:
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.
H
3
D E F G
I
4
Representation of Trees
There are several ways to represent a given tree such as
1. List Representation
2. Left Child- Right Sibling Representation
3. Representation as a Degree-Two tree
List Representation:
The tree can be represented as a List. The tree of figure (A) could be written as the list.
(A (B (E (K, L), F), C (G), D (H (M), I, J) ) )
To obtain the degree-two tree representation of a tree, simply rotate the right-sibling pointers in a left
child-right sibling tree clockwise by 45 degrees.
This gives us the degree-two tree displayed in Figure (E).
In the degree-two representation, a node has two children as the left and right children.
Binary Trees
Definition:
A binary tree T is defined as a finite set of nodes such that,
T is empty or
T consists of a root and two disjoint binary trees called
the left subtree and the right subtree.
Binary Trees
Binary trees are characterized by the fact that any node can have at
most two branches
Definition
A binary tree T 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
Thus the left subtree and the right subtree are distinguished
A A
B B
A A 1 A
B B 2 B C
4 H I
E 5
Skewed Tree
A skewed tree is a tree, skewed to the left or skews to the right.
or
It is a tree consisting of only left subtree or only right subtree.
A tree with only left subtrees is called Left Skewed Binary Tree.
A tree with only right subtrees is called Right Skewed Binary Tree.
Complete Binary Tree
A binary tree T is said to complete if all its levels,
except possibly the last level, have the maximum
number node 2i, i ≥ 0 and if all the nodes at the last
level appears as far left as possible.
Strictly Binary Tree(Full Binary Tree)
If every non-leaf node in a binary tree has nonempty left
and right subtrees , the tree is called a strictly binary tree.
A binary tree in which every node has either two or zero number
of children is called Strictly Binary Tree
A binary tree has 2i nodes in any given level i is
called strictly binary tree.
Every node other than leaf node has 2 children.
Level 0- 20 =1 node
Level 1-21= 2 nodes
Level 2=22 =4 nodes
Full Binary Tree (Strictly binary tree)
k k
i=0 i=0
Lemma 2: [Relation between number of leaf nodes
and degree -2 nodes]:
For any nonempty binary tree, T, if n0 is the number of leaf nodes and n2 the
number of nodes of degree 2, then n0 = n2 + 1.
Proof: Let n1 be the number of nodes of degree one and
n the total number of nodes.
Since all nodes in T are at most of degree two,
we have to Count the number of branches in a binary tree.
If B is the number of branches, then n =B + 1.
All branches stem from a node of degree one or two. Thus,
Subtracting Eq. (2)
n = n0 +Bn=n 1+
1+ n2 2n2. (1)
Hence, we obtain
n = B + 1= n 1+ 2n2 +
1
from Eq. (1) and rearranging terms, we get (2)
n0 = n2 +1
Here, For Figure (b) n2=4, n0= n2+1= 4+1=5
Therefore, the total number of leaf node=5
Binary Tree Representation
The storage representation of binary trees can be classified
as
Array representation
Linked representation
Binary tree representations (using array)
Lemma: If a complete binary tree with n nodes is represented
sequentially, then for any node with index i, 1 i n, we have
1. parent(i) is at i /2 if i 1.
If i = 1, i is at the root and has no parent.
2. LeftChild(i) is at 2i if 2i n.
If 2i n, then i has no left child.
3. RightChild(i) is at 2i+1 if 2i+1 n.
If 2i +1 n, then i has no right child
Binary tree representations (using array)
A tree can be represented using an array, which is called
sequential representation.
The nodes are numbered from 1 to n, and one dimensional
array can be used to store the nodes.
Position 0 of this array is left empty and the node numbered i
is mapped to position i of the array.
For complete binary tree the array representation is ideal,
as no space is wasted.
For the skewed tree less than half the array is utilized.
Binary tree representations (using array)
◦ Wastage of space: in the worst case, a skewed tree of depth k
requires 2k-1 spaces. Of these, only k spaces will be occupied
◦ Insertion or deletion
of nodes from the
middle of a tree
requires the
movement of
potentially many nodes
to reflect the change in
the level of these nodes
Linked representation of Binary Tree:
The problems in array representation are:
It is good for complete binary trees, but more memory is wasted
for skewed and many other binary trees.
The insertion and deletion of nodes from the middle of a tree
int data;
}node;
Binary tree representations (using link)
• Binary tree representations (using link)
BINARY TREE TRAVERSALS
Visiting each node in a tree exactly once is called tree
traversal
Let ptr is the pointer which contains the location of the node N currently
being scanned. L(N) denotes the leftchild of node N and R(N) is the right
child of node N
Recursion function:
The inorder traversal of a binary tree can be recursively defined as
Traverse the left subtree in inorder.
Visit the root.
Traverse the right subtree in inorder.
void inorder(treepointerptr)
{
if (ptr)
{
inorder (ptr→leftchild);
printf (“%d”,ptr→data);
inorder (ptr→rightchild);
}
}
• Inorder traversal (LVR) (recursive version)
Output: A / B * C * D + E
Binary Tree Traversals
Inorder traversal (LVR) (recursive version)
output A / B * C * D + E
:
ptr
L
V
R
Preorder:
Preorder is the procedure of visiting a node, traverse left and continue. When you cannot
continue, move right and begin again or move back until you can move right and resume.
Recursion function:
The Preorder traversal of a binary tree can be recursively defined as
Output: + * * / A B C D E
Binary Tree Traversals
Preorder traversal (VLR) (recursive version)
output + * * / A B C D E
:
V
L
R
Postorder:
Postorder traversal calls for moving down the tree towards the left until you
can go no further. Then move to the right node and then visit the node and
continue.
Recursion function:
The Postorder traversal of a binary tree can be recursively defined as
Traverse the left subtree in postorder.
Traverse the right subtree in postorder.
Visit the root
void postorder(treepointerptr)
{
postorder (ptr→rightchild);
printf (“%d”,ptr→data);
}
Postorder traversal (LRV) (recursive version)
Output: A B / C * D * E +
Binary Tree Traversals
Postorder traversal (LRV) (recursive version)
output A B / C * D * E +
:
L
R
V
Iterative inorder Traversal:
5 4 11
8 3 14
2 17
1
A B
/ *C D
* E
+
L
V
FIFO
ptr
Binary Tree Traversals
Advanatges:
No need for stacks or recursion: Unlike binary trees, threaded
binary trees do not require a stack or recursion for their
traversal.
Optimal memory usage: Another advantage of threaded
binary tree data structure is that it decreases memory wastage.
Disadvantages:
Insertion and deletion operation becomes more difficult.
Tree traversal algorithm becomes difficult.
Memory required to store a node increases.
Each node has to store the information whether the links is
normal links or threaded links
Threaded Binary Trees
A Threaded Binary Tree
root A
t: true thread
f: false child
dangling
f B f C
D t E t F G
dangling
inorder traversal:
H I
H D I B E A F C G
Threaded Binary Tree
Inorder
Threaded Binary Trees
Inorder traversal of a threaded binary tree
void tinorder(threaded_pointer tree){
/* traverse the threaded binary tree inorder */