Advanced Data StructuresChapter II: Data Structures FAP:UC-BCF 1 Terminologies Data Types: Refer to the different kinds of data that a variable may hold in a programming languages Examples: integer real float char string FAP:UC-BCF 2 Terminologies Data Objects: Refer to the set of elements Example: data object integer refers to D = {0, ±1, ±2, ±3, … } FAP:UC-BCF 3 Terminologies Data Structures: FAP:UC-BCF Describe the set of objects and how they are related. Describe the set of operations that can be applied on the elements of a data object. Typically this describe the more complex data types such as arrays, stacks, queues, trees, graphs and operations of how to manipulate these. 4 fieldN FAP:UC-BCF 5 . RecordType field1 field1. field2 field3 .Records A structure that can have a number of heterogeneous elements. field2. Declaration: typedef struct { <data type1> <data type2> <data type3> <data typeN> }RecordType. fieldN. field3. . Records Field1. printf(“%d:4:2”. Assigning a value to a record field: A.field3). float. A. real.field1 = <value>.field1). integer. A. A. FAP:UC-BCF 6 . printf(“%c”. field2. field3 … fieldN can be of any data type (i.e. record) To define a variable for the record: RecordType A.field2).field2 = <value>. A. To retrieve a value from a record field: printf(“%d”. Arrays Consecutive set of memory locations is a set of pairs – index and a value finite.one-dimensional array . Forms: . ordered set of homogeneous elements.n-dimensional array FAP:UC-BCF 7 . Arrays 2 data types Declaration Base type of component type Index type int A[10]. char B[45]. 2 basic operations Extraction Storing FAP:UC-BCF 8 . then: n = number of elements If an array is declared to be A[n][m].c. then n*m = number of elements if given n-dimensional array declaration A[b][c][d]…[n] = Πb.Arrays If an array is declared to be A[n]. … n FAP:UC-BCF 9 . Arrays To determine the ith element of a Single-dimension array: A[i] = α + (i) * esize where: α .base or starting address i – element esize – element size in bytes Example: Determine the address of 5th element of an integer array A with a starting address of 2000 FAP:UC-BCF 10 . Arrays To determine the ith element of a Twodimension array: A[i][j] = α + [(i)*(UB2)+(j)] * esize where: UB2 – upper bound of the 2nd dimension α .base or starting address esize – element size in bytes FAP:UC-BCF 11 . Arrays To determine the ith element of a Threedimension array: A[i][j][k]= α +[(i)*(UB2)*(UB3)+(j)*(UB3)+(k)]*esize where: UB3 – upper bound of the 3rd dimension UB2 – upper bound of the 2nd dimension α .base or starting address esize – element size in bytes FAP:UC-BCF 12 . find the formula to represent an element in a 4dimensional array.find the address of X[0][2][5][1][2] FAP:UC-BCF 13 . find the address of A[2][2][0][4] 2. b. α =3000.find the total number of elements c.find the total number of elements b. esize=3 bytes: a. Given A[10][3][3][6].Arrays Exercises: 1. α =2000. Given X[8][3][6][2][3]. esize=4 bytes: a. FAP:UC-BCF 14 . Assume that we do not know the size of RECTYPE in number of bytes but we know that the address of A1[20][2][3] is 2160.Arrays 3. }rectype. Give the size of RECTYPE in bytes. b. char D. a. compute the address of element A1[120][3][3] given the base address at 2000. typedef rectype matrix[121][4][5]. Consider the following declaration: typedef struct{ int A. char B[10]. Assume the base address is at 2000. float C. matrix A1. LIFO (Last In First Out) A B C D E G F TOP A B C D E F G TOP FAP:UC-BCF 15 .Stacks An ordered list in which all insertions and deletions are made at one end called the TOP. top.Stacks Operations: Create(top) – Create an empty stack Push(Stack. top) – Returns the top element of the stack Empty(top) – Determines whether the stack is empty or not. item) – Inserts an element item into the stack Pop(Stack. FAP:UC-BCF Stack full: Top = n-1 Stack empty: Top = -1 16 . item) – Removes the top element of the stack and stores the value in item S_top(Stack. top. Stacks Representation: One-dimensional array A B C D E F TOP Singly linked-list A B C D TOP FAP:UC-BCF 17 . Example: Processing of procedure calls and their terminations FAP:UC-BCF 18 .Stacks Declaration #define n <constant value> typedef <data type> elementtype. typedef elementtype Stack[n]. } void push(stack S.elementtype item) { if(top==n-1) stackfull. S[*top] = item. int *top. else { *top++.Stacks Procedures for Stack Operations: void create(int *top) {*top = -1. } } FAP:UC-BCF 19 . elementtype *item) { if(top==1) stackempty.Stacks void pop(stack S. else{ *item = S[top]. int top) { if(top==-1) error_routine. } FAP:UC-BCF 20 . else return S[top]. int *top. } int empty(int top) { if(top == 1) return 1. else return 0. *top--.} } elementtype s_top(stack S. &top1. push(s1. printf(“%d”. push(s1. top1). s_empty = empty(top1). 9). s_empty = empty(top1). 8). &top1.Stacks void main() { create(&top1). &top1. push(s1. printf(“%d”. &top1. j=s_top(s1. FAP:UC-BCF 21 . s_empty). printf(“Top is %d”. create(&top2). push(s1. j). 10). push(s1. s_empty). 16). 7). printf(“%d\n”. top1). &top1. &top2. push(s2. 10). printf(“%d”. 9). push(s2. } FAP:UC-BCF 22 . push(s2. push(s2.Stacks s_empty = empty(top2). printf(“Top is %d”. top2). &top1. 8). &top2. j). &top2. pop(s2. 12). printf(“%d\n”. top2). &top2. &item). j=s_top(s2. &top2. push(s2. push(s2. pop(s1. 4). s_empty). 7). &top2. &item). &top2. >=. Operands can be any legal variable names or constants in programming languages.+ Relational operators: ==. <= FAP:UC-BCF 23 . operators and delimiters. >.* / Unary operators: . Operations are described by operators: Basic arithmetic operators: + .Evaluation of Expressions Expression is made up of operands. <. Evaluation of Expressions Our concern: how the expressions are evaluated The compiler accepts expressions and produce correct result by reworking the expressions into postfix form. Other forms include infix and prefix. Prefix : <Operator> <Operand1> <Operand2> Postfix : <Operand1> <Operand2> <Operator> Infix : <Operand1> <Operator> <Operand2> FAP:UC-BCF 24 . Evaluation of Expressions Expression: A + B Prefix : +AB Postfix: AB+ Expression: (A+B*C)/D Prefix : /+A*BCD Postfix: ABC*+D/ FAP:UC-BCF 25 . SYMBOL ) ^ *. IN-Coming Priority (ICP) – The priority of the operator as current token.Conversion from Infix to Postfix using Stacks IN-Stack Priority (ISP) – The priority of the operator as an element of the stack.( FAP:UC-BCF ISP ICP -3 2 1 0 -4 2 1 4 26 . / +. Conversion from Infix to Postfix using Stacks Rule: Operators are taken out of the stack (POP) as long as their ISP is greater than or equal to the ICP of the new operator. Note: ISP and ICP of # sign = -1 FAP:UC-BCF 27 . else if x ==‘)’ {//unstack until ‘(’ while stack[top] != ‘(’ { pop(y). printf(y).} pop(y). x = nexttoken(E). while x != ‘#’ { if x is an operand printf(x).Conversion from Infix to Postfix using Stacks Algorithm: void POSTFIX(expression E) token x. //takes the first token and remove it from the original expression. //delete ‘(’ } FAP:UC-BCF 28 .y. top = 0. stack[0] = ‘#’. } } FAP:UC-BCF 29 . printf(y). } x = nexttoken(E). printf(y). } if(!empty(stack)) { while stack[top] != ‘#’ { pop(y). } push(x).Conversion from Infix to Postfix using Stacks else{ while isp[stack[top]] >= icp[x] { pop(y). called the REAR. while all deletions take place at the other end.Queues An ordered list which all insertions take place at one end. FAP:UC-BCF 30 . called the FRONT. FIFO (First-In-First-Out) Elements are processed in the same order as they were received. The first element inserted in the queue will be the first one to be removed. Qfront(Queue. Rear. Front. Rear) – creates an empty queue Insert(Queue.Queues Conventions for FRONT and REAR: Front is always 1 less than the actual front of the queue. Front. Initial value: Front = Rear = -1 Operations: Createq(Front. Item) – inserts the element item to the rear of the queue. Rear. Delete(Queue. Rear always points to the last element in the queue. Rear) – returns the front element of the queue FAP:UC-BCF 31 . Item) – removes the front element from the queue and assigns is to variable item. Queues Qempty(Front. Rear) – determines if the queue is empty or not Returns 1 if true Otherwise return 0 Front = Rear means queue is empty. FAP:UC-BCF 32 . Queues Representation One-dimensional Array A B C D E Front F G Rear Singly linked-list A Rear FAP:UC-BCF B C D Front 33 . FAP:UC-BCF 34 . rear. typedef elementtype Queue[n]. typedef int FR FR front.Queues Declaration: #define n <constant value> typedef <data type> elementtype. Queue Q. elementtype item) { if(rear == n-1) queuefull. bank-related) Procedures for Queue Operations: void createq(FR *front.e. FR *rear.Queues Example: Processing of customers’ transactions (i. } } FAP:UC-BCF 35 . FR *rear) { *front = *rear = -1 } void insert(queue Q. cashiers. Q[*rear] = item. else { (*rear)++. item = Q[front]. FR *rear. else return 0.Queues void delete(queue Q. else { (*front)++.} } elementtype qfront(queue Q. FR *front. FR front. } FAP:UC-BCF 36 . else return(Q[front + 1]) } int quempty() { if(front == rear) return 1. FR rear) { if(front == rear) errorroutine. elementtype *item) { if(*front == rear) queueempty. move the entire queue to the left so that the first element is again at Q[0] and front = -1. To solve this. FAP:UC-BCF 37 .Queues Notes: QUEUEFULL signal does not necessary imply that there are N elements in the queue. . 0 4 1 3 FAP:UC-BCF n-1 2 38 . ..Circular Queues . } } FAP:UC-BCF 39 . else { *front = (*front+1)%n. else { *rear = (*rear+1)%n. Q[*rear] = item. FR *rear. FR front. *item = Q[front]. elementtype *item) { if(front == rear) then cqueueempty. FR *front. FR rear.Circular Queues void insert(queue *Q. elementtype item) { if(front == (rear+1)%n) queuefull.} } void delete(queue Q. else return 0. else return(Q[front+1]%n). } FAP:UC-BCF 40 . FR front. FR rear) { if(front == rear) errorrouting. } int cquempty() { if(front == rear) return 1.Circular Queues elementtype cqfront(queue Q. FAP:UC-BCF A+B*C/D A/B^C+D*E-A*C (A+B)*D+E/(F+A*D)+C A+B*D^E^F^G/H^J*K-L !(A&&!(B<C)||(C>D))||(C<E) 41 . 2. 4. 5.Exercises Convert the following infix expressions to postfix and prefix: 1. 3. AB&&C||EF<=!|| FAP:UC-BCF 42 .Exercises Convert the following prefix expressions to infix: 6. || || &&ABC!<=EF Convert the following postfix expressions to infix: 9. AB-C+DEF-+^ 11. AB+CDE-F+*G-/ 10. ^+-ABC+D-EF 8. -+-^ABC*D^EFG 7. A+B-C*D*(E+F-G*H^I^J)+L/M+(N*O/P+ (Q^R^S^T))+U FAP:UC-BCF 43 . B+C/D^(E+F*G^H)+Z 14. B+C^(E+F*G^H)/(K-L/M)+N 13.Exercises Convert the following infix expressions to postfix using stack: 12.