Data Structure: Instructor: Dr. Wei (Lisa) Li Department of Computer Science, GSU
Data Structure: Instructor: Dr. Wei (Lisa) Li Department of Computer Science, GSU
Data Structure
Dynamic sets (Data structures):
Change a dictionary, add/remove words
Reuse of structured information
On-line algorithms - very fast updating
record
index key
Elements:
Index: element ID
Key: value of element
Operations
Query/Search: return information about the set, e.g., maximum,
minimum
Modifying Operations: change data sets, e.g., insert, delete
Stack
Stack: S[1, 2, , S.top]
S.top points to the most recently inserted element
Stack (cont.)
Empty: O(1)
Stack-Empty(S)
if S.top == 0
return TRUE
else return FALSE
Pop(S)
Delete: O(1)
Pop (S, x)
if S.top == 0
Return Empty
else S.top = S.top -1
Return S[S.top+1]
Queue
Queue: Q[1, 2, , n]
Q.head points to head
Q.tail points to location at which a newly arriving element will be
inserted into the queue
Empty: Q.head == Q.tail
Full: Q.head == Q.tail + 1
Queue (cont.)
Insert: O(1)
Enqueue(Q, x)
Q[Q.tail] = x
if Q.tail == Q.length
Q.tail = 1
else Q.tail = Q.tail + 1
Queue (cont.)
Delete: O(1)
Dequeue(Q)
x = Q[Q.head]
if Q.head == Q.length
//Q.head is at the end
Q.head = 1
//Place Q.head in a circular order
else Q.head = Q.head + 1
return x
Dequeue (Q)
Linked List
Doubly Linked List: prev and next
Head: x.prev == NIL
Tail: x.next == NIL
Empty: x.head == NIL
List Search
List-Search(L, k): O(n)
x = L.head
while x NIL and x.key k
x = x.next
return x
List Insertion
List-Insert(L, x): O(1)
x.next = L.head
if L.head NIL
L.head.prev = x
L.head = x
x.prev = NIL
10
List Deletion
List-Delet(L, x): (any element O(1); a given element O(n))
if x.prev NIL
x.prev.nex = x.next
else L.head = x.next
//x is head of list
if x.next NIL
x.next.prev = x.prev
11
Binary Tree
Structure Attributes: parent (p), left child (left),
and right child (right)
Root: x.p == NIL
Leaf: x.left == x.right == NIL
Empty: T.root == NIL
12
Exercise
Draw the binary tree rooted at index 6 that is represented
by the following attributes:
13