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
4
5. DEFINITION OF LINKED LISTS
A linked list is a sequence of items (objects)
where every item is linked to the next.
Graphically:
CS 103
5
data data data data
head_ptr tail_ptr
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:
CS 103
6
John Mary Dan Sue
head_ptr
tail_ptr
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:
CS 103
7
10 8 6 2
head_ptr tail_ptr
8
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
8
9. INSERT– AT THE HEAD
Insert a new data A. Call new: newPtr
List before insertion:
After insertion to head:
CS 103
9
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 = old head_ptr
•The new value of head_ptr = newPtr
10. INSERT – AT THE TAIL
Insert a new data A. Call new: newPtr
List before insertion
After insertion to tail:
CS 103
10
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
11. INSERT – INSIDE THE LIST
Insert a new data A. Call new: newPtr
List before insertion:
After insertion in 3rd position:
CS 103
11
data data data data
head_ptr tail_ptr
data
data A data data
head_ptr
tail_ptr
data
•The link-value in the new item = link-value of 2nd item
•The new link-value of 2nd item = newPtr
12. DELETE – THE HEAD ITEM
List before deletion:
List after deletion of the head item:
CS 103
12
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
13. DELETE – THE TAIL ITEM
List before deletion:
List after deletion of the tail item:
CS 103
13
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
14. DELETE – AN INSIDE ITEM
List before deletion:
List after deletion of the 2nd item:
CS 103
14
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
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.
CS 103
15
17. 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)
18. 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.
19.
20. 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);
21. IMPLEMENTING STACKS USING TEMPLATES
Templates allow the compiler to
generate multiple versions of a class
type or a function by allowing
parameterized types.
22. TREES AND
BINARY TREES
Become Rich
Force Others
to be Poor
Rob
Banks
Stock
Fraud
The class notes are a compilation and edition from many sources. The
instructor does not claim intellectual property or ownership of the lecture
notes.
23. 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&DManufacturing
Laptops DesktopsUS International
Europe Asia Canada
24. 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
25. LEFT CHILD, RIGHT SIBLING REPRESENTATION
Data
Left
Child
Right
Sibling A
B C D
IHGFE
J K L
26. 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
27. 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 Stories2. Methods
2.1 Get a
CS PhD
2.2 Start a
Web Site
1.1 Enjoy
Life
1.2 Help
Poor Friends
2.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)
28. 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
1K
programs/
DDR.java
10K
Stocks.java
25K
h1c.doc
3K
h1nc.doc
2K
Robot.java
20K
9
3
1
7
2 4 5 6
8
29. 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 expressions
decision processes
searching
A
B C
F GD E
H I
30. 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
31. 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
k
k
i
i
32. FULL BINARY TREE
A full binary tree of a given height k has 2k+1–1 nodes.
Height 3 full binary tree.
33. 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 15141312
Full binary tree of depth 3
1
2 3
75
9
4
8
6
Complete binary tree
34. Binary Tree Traversals
Let l, R, and r stand for moving left, visiting
the node, and moving right.
There are six possible combinations of traversal
lRr, lrR, Rlr, Rrl, rRl, rlR
Adopt convention that we traverse left before
right, only 3 traversals remain
lRr, lrR, Rlr
inorder, postorder, preorder
35. 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