Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

Arrays, Lists,

and Trees
CS 251 – Data Structures
and Algorithms
Linear Data Structures Table of Contents
01 Arrays and Linked Lists

Non-Linear Data Structures


02 Trees and Graphs*

* Just a mention. We will talk more about graphs later in this course.
01
Linear Data Structures
Arrays and Linked Lists
Static Data Structure (i.e., fixed size)

Array access: 𝐴 𝑖 ∈ Ο(1)

Q: How much work (i.e., time) is required


to insert an element into an unsorted

Array array?
Keep track of next available cell?: Ο(1)
Sorry, to tracking: Ο(𝑛)
0 1 2 … n-2 n-1
Q: How much space is required to keep
A A0 A1 A2 … An-2 An-1 track of the next available cell?
One variable: Ο(1)

Q: How much work (i.e., time) is required


to insert an element into a sorted array?
Find location + Right shifting: Ο(𝑛)
Resize an array

Algorithm resize(A, oldN, newN)


Let B be a new array of size newN
Copy/Move the elements from A to B
Return B //or A = B. Perhaps delete A?
End Algorithm

Time? 𝑇 𝑛 ≈ 𝑛 ∈ Ο(𝑛)
Space? 𝑇 𝑛 ≈ 𝑛 ∈ Ο(𝑛)

Usual strategy:
Full? Increase its size (double the current size)
Half empty? reduce its size (half the current size)
2D Array (Grid, Matrix)
A 0 1 2 … m-2 m-1

0 A0,0 A0,1 A0,2 … A0,m-2 A0,m-1


Static Data Structure (i.e., fixed size)

1 A1,0 A1,1 A1,2 … A1,m-2 A1,m-1 Array access: 𝐴 𝑖 𝑗 ∈ 𝑂(1)

Q: How much work (i.e., time) is required


2 A2,0 A2,1 A2,2 … A2,m-2 A2,m-1 to traverse a grid?
General case: 𝑂 𝑛𝑚
Squared matrix: 𝑂 𝑛2
… … … … … … …

Q: How much space is required to store a


n-2 An-2,0 An-2,1 An-2,2 … An-2,m-2 An-2,m-1 grid?
General case: 𝑂 𝑛𝑚
Squared matrix: 𝑂 𝑛2
n-1 An-1,0 An-1,1 An-1,2 … An-1,m-2 An-1,m-1
public class List<Item> {

Linked List
// Pointer to the first node of the list
private Node<Item> first;

// Number of nodes of the list


private int size;

item item item … item /**


* Constructor of the class.
*/
public List() {
first = null;
size = 0;
public class Node<Item> { }

// The item of the node /**


* Insert an item at the end of the list.
public Item item;
*/
public void insert(Item item) {
// The pointer to the next node if (first == null) {
// in the list first = new Node<Item>(item);
public Node<Item> next; size += 1;
return;
/** }
* Constructor of the class.
Node<Item> n = first;
*/
public Node(Item item) { while (n.next != null) {
this.item = item; n = n.next;
next = null; }
}
} n.next = new Node<Item>(item);
size += 1;
}
}
Dynamic Linear Data Structure

List access: Search node where


node.item == X, so 𝑂(𝑛)

Q: How much work (i.e., time) is required to

Linked List
insert an element at the start/end of a list?
Start? New node + update pointer: 𝑂(1)
End? Keep track of last node?: 𝑂(1)
End? Sorry, to tracking: 𝑂(𝑛)

item item item … item Q: How much space is required to keep


track of the last node in the list?
One pointer: 𝑂(1)

Q: How much work (i.e., time) is required to


insert an element somewhere in a list?
Find location + Update pointer: 𝑂(𝑛)
Linked list types

item item item … item


Singly linked list

item item item … item Singly circular linked list

item item … item


Doubly linked list

item item … item Doubly circular linked list


Arrays vs. Linked lists

Array Linked List


Pros Pros
Easy access to any Resizable
element
Easy insert at front and
No pointers (i.e., back of list
dynamic memory)
Arrays vs. Linked lists

Array Linked List


Cons Cons
Fixed size Space overhead due to
pointers
Enough consecutive
space in memory No direct access to
internal nodes
Java Array Lists
Array Lists are not linked lists, they are arrays

Not allowed in programming projects unless stated in the


description file
02
Non-Linear Data Structures
Trees and Graphs*

* Just a mention. We will talk more about graphs later in this course.
Tree
A Dynamic Non-Linear Data Structure

B C Access point through the root (i.e., pointer


to the top-most node of the tree)

Each node may have none to many


D E F G children.

Recursive: A tree is made of subtrees.


H I J K L
Search(something)? 𝑂(log 𝑛 ) if ordered.
Otherwise 𝑂(𝑛).
M N O
Concepts & Operations
• size() = 15
A • isEmpty() = false
• root() = A
B C • parent(D) = B
• grandparent(O) = G
• sibling(D) = {E, F}
• children(E) = {I, J}
D E F G • isInternal(G) = true
• isExternal(M) = true
• isRoot(L) = false
H I J K L • isLeaf(K) = true
• height() = 4
• height(G) = 2
M N O • depth(B) = 1
• depth(A) = 0
Binary tree
A
Every node has at most two children: left and
right.
B C
leftChild(G): K
rightChild(G): L

D E G Full binary tree: every node has two children.

Max number of leaves in a binary tree?


2ℎ
H I J K L
Max number of nodes in a binary tree?

෍ 2𝑖 = 2ℎ+1 − 1
M N O 𝑖=0
Full binary tree facts

Let 𝑇 be a nonempty, full binary tree.

If 𝑇 has 𝐼 internal nodes, the number of leaves is 𝐼 + 1


If 𝑇 has 𝐼 internal nodes, the total number of nodes is 2𝐼 + 1
If 𝑇 has a total of 𝑁 nodes, the number of internal nodes is (𝑁 − 1)/2
If 𝑇 has a total of 𝑁 nodes, the number of leaves is (𝑁 + 1)/2
If 𝑇 has 𝐿 leaves, the total number of nodes is 2𝐿 − 1
If 𝑇 has 𝐿 leaves, the number of internal nodes is 𝐿 − 1

Proofs? Use induction on full binary trees.


Binary tree traversal: Preorder
A
Visit a node before its descendants.

B C Function preorder(node)
if (node != null)
visit(node)
preorder(leftChild(node))
D E G preorder(rightChild(node))
end if
End function

H I J K L preorder(A) = A, B, D, H, E, I, J, M, N, C, G, K, L O

M N O
Binary tree traversal: Inorder
A
Visit a node after its left descendants and
before its right descendants.
B C
Function inorder(node)
if (node != null)
inorder(leftChild(node))
D E G visit(node)
inorder(rightChild(node))
end if
End function
H I J K L
inorder(A) = H, D, B, I, E, M, J, N, A, C, K, G, O, L

M N O
Binary tree traversal: postorder
A
Visit a node after its descendants.

B C Function postorder(node)
if (node != null)
postorder(leftChild(node))
postorder(rightChild(node))
D E G visit(node)
end if
End function

H I J K L postorder(A) = H, D, I, M, N, J, E, B, K, O, L, G, C, A

M N O
Balancing matters

Balanced Unbalanced Unbalanced


Height ∈ 𝑂(log 𝑛 ) Height ∈ 𝑂(𝑛) Height ∈ 𝑂(𝑛)
Graph
Dynamic Non-Linear Data Structure.
B C
Nodes (e.g., data points) and edges (e.g., data
relationships).
A E G
Computational analysis strongly influenced by
J Graph Theory.

I Useful to represent connectivity between data


K points:
H N • Route Maps
• Network Topologies
L • Social Networks
D M O • State Machines

More about graphs during weeks 7-9.


EOF
Do you have any questions?

CREDITS: This presentation template was created by Slidesgo, including


icons by Flaticon, infographics & images by Freepik and illustrations by
Stories

You might also like