Data Structures



  1. What is a data structure?

  2. The general problem of data structure design

  3. When do we need a data structure?

  4. Review of Stacks and Queues

  5. A stack is a data structure S that supports three operations:

  6. The pushing (insertion) and popping (detetion) are done at one and the same end of the stack, namely, its top.

  7. One implementation is an array S[1:N] and a global index I pointing to the top of the stack, assuming that S[i] is empty but S[i-1] is full
    Procedure Push (S,a)
    begin
    S[I] := a;
    I := I+1;
    end

    function Pop(S)
    begin
    I := I-1;
    return(S[I]);
    end

    function Top(S)
    begin
    return(S[I-1]);
    end

  8. A queue is a data structure Q that supports two operations:

  9. One implementation is a circular array Q[0:N-1] and two global indexes H and T, pointing to the head and tail elements of Q.

    Q[H] is considered full, Q[T] is considered empty.

    By circular we mean: the element before Q[0] is Q[N-1],
    and the element after Q[N-1] is Q[0].
    Procedure enqueue(Q,a)
    begin
    Q[T] := a;
    T = T - 1 modulo N;
    end

    function dequeue(Q)
    begin
    H := H-1 modulo N;
    return (Q[H+1 modulo N]);
    end

  10. Records: A record is a built-in structure data type. It is an aggregate of several elements called fields, where each field is a variable of a standard type or of a record type.

  11. Syntax of a record:


    record name
    begin
    field declarations;
    field declarations;
    ...
    field declarations;
    end


    Example:
    record employee
    begin
    char name[1:30]
    integer SSN;
    char address[1:100]
    real salaray;
    end


    Once a record has been defined, it becomes a new data type that can be used in declaring record variables:

    employee X;
    employee Y[1:100];
    record company
    begin
    char name[1:50]
    char address[1:100]
    employee Z[1:1000]
    end

    to access an individual field F of a record R, use
    R.F

    Examples: X.SSN := 123456789;
    X.salary := X.salary + 1000;

  12. Pointers data types
    A pointer is a data type whose contents are addresses of other variables of a specified type.


    Examples: charpointer p; intpointer q[1:n]; employeepointer r;
    charptr p; intptr q[1:n]; employeeptr r;

    to access the field F of a record pointed to by a record pointer r, use
    r ---> F

  13. A self-referential record is a record that has a pointer field that points to a record of the same type.

  14. Example:
    record employee
    begin
    char name[1:30]
    integer SSN;
    employeeptr next;
    end

  15. Singly Linked Lists:

  16. Doubly linked lists: these are like single linked lists except that each record has a field that points to the previous record, and another field that points to the next record.

  17. Introduction to graphs

  18. Trees

    In the three algorithms below, T is a pointer to the tree root, and a a is a key.


    The search function returns a pointer to the record containing a, if any. Otherwise, it returns nil.

    functionsearch(T,a)
    begin
    nodeptr p;
    p=T;
    while (p != nil and p --> key != a) do
    if a < p --> key then
    p := p --> left;
    else
    p := p --> right;
    endif
    endwhile
    return (p);
    end search

    The insert procedure inserts the key a into T.
    procedure insert(T,a)
    begin
         nodeptr p;
         p=T;
         Bool done = false;
         while not done do
            if a <= p --> key then
                 if p --> left != nil then
                     p = p --> left;
                 else
                     p --> left = new(node);
                     p --> left --> key := a;
                     done =true;
                 endif
             else
                 if p ---> right != nil then
                     p = p --> right;
                 else
                     p --> right = new(node);
                     p --> right -- > := a;
                     done = true;
                 endif
             endif
         endwhile
    end insert

    procedure  delete(T,a)	 
    begin 
    	nodeptr  p,q,r,s; 
    	integer  direction; 
    	p = T; 
    	while  (p != nil and p-->key != a) do 
    		if a < p-->key then 
    			q := p; 
                           	p := p-->left; 
    		 	direction := 0; 
                 	else 
    		 	q := p; 
                    	p := p-->right; 
    			direction := 1; 
                 	endif 
            endwhile 
    	if p == nil then 	 
    	 	return; 
    	elseif p-->left == nil and p-->right == nil then 
    		/* p has no children */ 
    		/* delete node pointed to by p and exit */ 
    		if direction == 0 
    			q-->left = nil; 
    		else 
    			q-->right = nil 
    		endif 
    		free (p); 
    	elseif p-->left == nil then 	
    		/* p has only one child, the right one */ 
    		if direction == 0 then 
    			q-->left := p-->right; 	
    			/* shortcut from parent to grandchild */ 
    		else 
    			q-->right := p-->right; 
    		endif 
    	elseif p-->right == nil then 
    		/* p has only one child, the left one */ 
    		if direction == 0 then 
    			q-->left := p-->left; 
    		else 
    			q-->right := p-->left; 
    		endif
    	else	/* p has two children*/ 
    		/* find the maximum node in the left subtree*/
    		/* subtree of p */ 
    		s := p-->left; 
    		q := p;	
    		/* now q will be the parent of s , and direction*/
    		/* will indicate the type of child s is to q*/ 
    		direction = 0; 
    		while s-->right != nil do 
    			q := s; 
    			s := s-->right; 
    			direction := 1; 
    		endwhile 
    		/*Now s points to the maximum node in the */
    		/*left subtree of p*/ 
    		p-->key := s-->key; 
    		/* now node s must be deleted. But since s has */
    		/* no right child, the deletion is done by */ 
    		/* deletion or shortcutting */ 
    		if s-->left == nil then 	/* s is a leaf*/ 
    			if direction == 0 then 
    				q-->left := nil; 
    			else 
    				q-->right := nil; 
    			endif 
    			free (s); 
    			return; 
    		else		/* s has a left child */ 
    			if direction == 0 then 
    				q-->left := s-->left; 
                           	else 
                                   	q-->right := s-->left; 
    			endif 
    			free(s) 
    			return; 
    		endif 
    	endif 
    end delete 
    


  19. the time complexity of insert, delete, and search is O(h) each, where h is the height of the tree.

  20. Full trees and almost complete trees:

  21. Heaps (min-heaps and max-heaps)

  22. Sets and the Union-Find Data Structure

    Abdou Youssef 2001-05-28