linked lists saloni
TRANSCRIPT
WELL COME
Guide ByDEVENDRA KUSHWAH
Presented by SALONI TIWARI
MANALI MAHESHWARI
.........................
CS 103
4
LINKED LISTS
Definition of Linked Lists Examples of Linked Lists Operations on Linked Lists Linked List as a Class Linked Lists as Implementations of
Stacks, Sets, etc.
CS 103
5
DEFINITION OF LINKED LISTS A linked list is a sequence of items
(objects) where every item is linked to the next.
Graphically:data data data data
head_ptr tail_ptr
CS 103
6
EXAMPLES OF LINKED LISTS(A WAITING LINE)
A waiting line of customers: John, Mary, Dan, Sue (from the head to the tail of the line)
A linked list of strings can represent this line:
John Mary Dan Sue
head_ptr tail_ptr
CS 103
7
EXAMPLES OF LINKED LISTS(A STACK OF NUMBERS)
A stack of numbers (from top to bottom): 10, 8, 6, 8, 2
A linked list of ints can represent this stack:
10 8 6 2
head_ptr tail_ptr
8
CS 103
8
OPERATIONS ON LINKED LISTS Insert a new item
At the head of the list, or At the tail of the list, or Inside the list, in some designated position
Search for an item in the list The item can be specified by position, or by
some value Delete an item from the list
Search for and locate the item, then remove the item, and finally adjust the surrounding pointers
size( ); isEmpty( )
CS 103
9
INSERT– AT THE HEAD Insert a new data A. Call new: newPtr List before insertion:
After insertion to head:
data data data data
head_ptr tail_ptr
A
data data data data
head_ptr tail_ptrA
•The link value in the new item = old head_ptr•The new value of head_ptr = newPtr
CS 103
10
INSERT – AT THE TAIL Insert a new data A. Call new: newPtr
List before insertion
After insertion to tail:
data data data data
head_ptr tail_ptr
A
data data data data
head_ptr tail_ptr
A
•The link value in the new item = NULL•The link value of the old last item = newPtr
CS 103
11
INSERT – INSIDE THE LIST Insert a new data A. Call new: newPtr
List before insertion:
After insertion in 3rd position:
data data data data
head_ptr tail_ptr
data
data A data data
head_ptr tail_ptrdata
•The link-value in the new item = link-value of 2nd item•The new link-value of 2nd item = newPtr
CS 103
12
DELETE – THE HEAD ITEM List before deletion:
List after deletion of the head item:
data data data data
head_ptr tail_ptr
data data data data
head_ptr tail_ptr
data
•The new value of head_ptr = link-value of the old head item•The old head item is deleted and its memory returned
data
CS 103
13
DELETE – THE TAIL ITEM List before deletion:
List after deletion of the tail item:
data data data data
head_ptr tail_ptr
data data data
head_ptr tail_ptr
•New value of tail_ptr = link-value of the 3rd from last item•New link-value of new last item = NULL.
data
datadata
CS 103
14
DELETE – AN INSIDE ITEM List before deletion:
List after deletion of the 2nd item:
data data data data
head_ptr tail_ptr
data data
head_ptr tail_ptr
•New link-value of the item located before the deleted one = the link-value of the deleted item
data
data datadata
CS 103
15
SEARCHING FOR AN ITEM Suppose you want to find the item whose
data value is A You have to search sequentially starting from
the head item rightward until the first item whose data member is equal to A is found.
At each item searched, a comparison between the data member and A is performed.
STACKS
STACK SPECIFICATION Definitions: (provided by the user)
MAX_ITEMS: Max number of items that might be on the stack
ItemType: Data type of the items on the stack
Operations MakeEmpty Boolean IsEmpty Boolean IsFull Push (ItemType newItem) Pop (ItemType& item) (or pop and top)
POP (ITEMTYPE& ITEM) Function: Removes topItem from stack
and returns it in item. Preconditions: Stack has been
initialized and is not empty. Postconditions: Top element has been
removed from stack and item is a copy of the removed element.
Stack overflow The condition resulting from trying
to push an element onto a full stack.
if(!stack.IsFull()) stack.Push(item);
Stack underflow The condition resulting from trying
to pop an empty stack.if(!stack.IsEmpty()) stack.Pop(item);
IMPLEMENTING STACKS USING TEMPLATES Templates allow the compiler to
generate multiple versions of a class type or a function by allowing parameterized types.
TREES AND BINARY TREES
Become Rich
Force Others to be Poor
Rob Bank
s
StockFrau
dThe class notes are a compilation and edition from many sources. The instructor does not claim intellectual property or ownership of the lecture notes.
WHAT IS A TREE A tree is a finite
nonempty set of elements.
It is an abstract model of a hierarchical structure.
consists of nodes with a parent-child relation.
Applications: Organization charts File systems Programming
environments
Computers”R”Us
Sales
R&D
Manufacturing
Laptops
Desktops
US
International
Europe
Asia
Canada
subtree
TREE TERMINOLOGY Root: node without parent (A) Siblings: nodes share the same
parent Internal node: node with at least
one child (A, B, C, F) External node (leaf ): node
without children (E, I, J, K, G, H, D) Ancestors of a node: parent,
grandparent, grand-grandparent, etc.
Descendant of a node: child, grandchild, grand-grandchild, etc.
Depth of a node: number of ancestors
Height of a tree: maximum depth of any node (3)
Degree of a node: the number of its children
Degree of a tree: the maximum number of its node.
A
B DC
G HE F
I J K
Subtree: tree consisting of a node and its descendants
LEFT CHILD, RIGHT SIBLING REPRESENTATION
Data
Left Child
Right Sibling A
B C D
IHGFE
J K L
TREE TRAVERSAL Two main methods:
Preorder Postorder
Recursive definition
Preorder: visit the root traverse in preorder the children (subtrees)
Postorder traverse in postorder the children (subtrees) visit the root
PREORDER TRAVERSAL A traversal visits the nodes of a
tree in a systematic manner In a preorder traversal, a node
is visited before its descendants Application: print a structured
document
Become Rich
1. Motivations
3. Success Stories
2. Methods
2.1 Get a CS PhD
2.2 Start a Web Site
1.1 Enjoy Life
1.2 Help Poor
Friends2.3 Acquired
by Google
1
2
3
5
4 6 7 8
9
Algorithm preOrder(v)visit(v)for each child w of v
preorder (w)
POSTORDER TRAVERSAL In a postorder traversal, a
node is visited after its descendants
Application: compute space used by files in a directory and its subdirectories
Algorithm postOrder(v)
for each child w of v
postOrder (w)visit(v)cs16
/
homeworks/
todo.txt
1Kprograms
/
DDR.java
10K
Stocks.java
25K
h1c.doc
3K
h1nc.doc
2K
Robot.java
20K
9
3
1
7
2 4 5 6
8
BINARY TREE A binary tree is a tree with the
following properties: Each internal node has at most
two children (degree of two) The children of a node are an
ordered pair
We call the children of an internal node left child and right child
Alternative recursive definition: a binary tree is either
a tree consisting of a single node, OR
a tree whose root has an ordered pair of children, each of which is a binary tree
Applications:arithmetic expressionsdecision processessearching
A
B C
F GD E
H I
Examples of the Binary Tree
A
B C
GE
I
D
H
F
Complete Binary Tree
1
2
3
4
A
B
A
B
Skewed Binary Tree
E
C
D
5
Maximum Number of Nodes in a Binary Tree
The maximum number of nodes on depth i of a binary tree is 2i, i>=0.
The maximum nubmer of nodes in a binary tree of height k is 2k+1-1, k>=0.
Prove by induction.122 1
0
kk
i
i
FULL BINARY TREE
A full binary tree of a given height k has 2k+1–1 nodes.
Height 3 full binary tree.
Complete Binary Trees
A labeled binary tree containing the labels 1 to n with root 1, branches leading to nodes labeled 2 and 3, branches from these leading to 4, 5 and 6, 7, respectively, and so on.A binary tree with n nodes and level k is complete iff its nodes correspond to the nodes numbered from 1 to n in the full binary tree of level k.
1
2 3
75
11
4
10
6
98 15141312Full binary tree of depth 3
1
2 3
75
9
4
8
6
Complete binary tree
Binary Tree Traversals
Let l, R, and r stand for moving left, visiting the node, and moving right.
There are six possible combinations of traversallRr, lrR, Rlr, Rrl, rRl, rlR
Adopt convention that we traverse left before right, only 3 traversals remain
lRr, lrR, Rlrinorder, postorder, preorder
INORDER TRAVERSAL In an inorder traversal a
node is visited after its left subtree and before its right subtree
Algorithm inOrder(v)if isInternal (v)
inOrder (leftChild (v))visit(v)if isInternal (v)
inOrder (rightChild (v))
3
1
2
5
6
7 9
8
4