Algorithms and Data Structures PD Dr. rer. nat. habil. Ralf Peter Mundani Computation in Engineering / BGU Scientific Computing in Computer Science / INF Summer Term 2018
Part 2: Data Structures PD Dr. Ralf Peter Mundani Algorithms and Data Structures Summer Term 2018 2
overview prelude linked lists stacks queues trees PD Dr. Ralf Peter Mundani Algorithms and Data Structures Summer Term 2018 3
Prelude fundamentals data structure way of organising data for processing by some algorithm or more general by some computer program often the choice of proper data structures is the only major decision for implementations for the same information some data structures require more or less space for storing the information for the same operations (on the data) some data structures lead to more or less efficient algorithms hence, the choice of algorithms and data structures is closely intertwined design of algorithms also means design of sufficient data structures PD Dr. Ralf Peter Mundani Algorithms and Data Structures Summer Term 2018 4
overview prelude linked lists stacks queues trees PD Dr. Ralf Peter Mundani Algorithms and Data Structures Summer Term 2018 5
Linked Lists fundamentals elementary data type, but not (yet) defined as primitive in all languages advantages compared to arrays easy growth and shrinkage in size over lifetime, hence, maximum size must not be known in advance high flexibility w.r.t. rearrangement of data items (nevertheless, this comes up with higher expenses for the access of arbitrary items) general description set of sequentially organised items, so called nodes explicit organisation of nodes via link to next node n 1 n 2 n 3 n k 1 n k linked list with nodes n i, 1 i k PD Dr. Ralf Peter Mundani Algorithms and Data Structures Summer Term 2018 6
Linked Lists fundamentals (cont d) each node contains one container for storing data item s value (integer, float, boolean,...) one container for storing link to next node... value i 1 value i value i 1... problem: what to specify as next node in case of last node hence, dummy node NIL to indicate end of list for reasons of convenience also dummy node HEAD at the other end HEAD n 1 n 2 n k 1 n k NIL linked list with nodes n i, 1 i k PD Dr. Ralf Peter Mundani Algorithms and Data Structures Summer Term 2018 7
Linked Lists fundamentals (cont d) assume, there s a primitive list with two containers value and link which can be accessed via list value and list link furthermore, a dummy node NIL should also be provided hence, the following program creates an empty list list L L link NIL which only consists of the two dummy nodes HEAD and NIL HEAD NIL for adding nodes to list L some further understanding is necessary PD Dr. Ralf Peter Mundani Algorithms and Data Structures Summer Term 2018 8
Linked Lists fundamentals (cont d) linked lists are dynamic structures easy rearrangement of nodes possible (just changing links instead of moving items in case of arrays) example: last node should be moved to the front (3 links to be changed) HEAD 1 2 3 4 5 NIL HEAD 1 2 3 4 5 NIL HEAD 5 1 2 3 4 NIL PD Dr. Ralf Peter Mundani Algorithms and Data Structures Summer Term 2018 9
Linked Lists list operations typical operations on linked lists are insertion of new nodes (which makes it grow by one in length each) deletion of old nodes (which makes it shrink by one in length each) for inserting a new node only two links have to be changed 4 HEAD 1 2 3 5 6 NIL 4 HEAD 1 2 3 5 6 NIL PD Dr. Ralf Peter Mundani Algorithms and Data Structures Summer Term 2018 10
Linked Lists list operations (cont d) the following procedure appends a new node at the end of list L by recursively processing all nodes starting from HEAD procedure APPEND (L, new_node) if L link NIL then new_node link L link L link new_node else APPEND (L link, new_node) fi end changing the condition allows to insert a new node anywhere in the list PD Dr. Ralf Peter Mundani Algorithms and Data Structures Summer Term 2018 11
Linked Lists list operations (cont d) the following procedure inserts a new node before the one with value val procedure INSERT (L, new_node, val) case L link NIL : exit L link value val : new_node link L link L link new_node else INSERT (L link, new_node, val) esac end the first test for NIL is necessary in case there is no node with value val and the recursion reaches the end of list L PD Dr. Ralf Peter Mundani Algorithms and Data Structures Summer Term 2018 12
Linked Lists list operations (cont d) when deleting node n i from a list only one link has to be moved (i.e. linking from node n i 1 to node n i 1 ) this does not delete node n i itself (which still points to n i 1 ), but it is no longer accessible by following the links from HEAD HEAD 1 2 3 4 5 NIL HEAD 1 2 3 4 5 NIL PD Dr. Ralf Peter Mundani Algorithms and Data Structures Summer Term 2018 13
Linked Lists list operations (cont d) the following procedure deletes a node (with value val) from lists L procedure DELETE (L, next, val) case next NIL : exit next value val : L link next link else DELETE (L link, next link, val) esac end the temporary node next is necessary to link from n i 1 to n i 1 (again, the dummy element HEAD makes everything more convenient) for deleting some node from list L the procedure is called with parameters DELETE (L, L link, val) PD Dr. Ralf Peter Mundani Algorithms and Data Structures Summer Term 2018 14
Linked Lists doubly linked lists as shown, linked lists are very efficient for certain operations, but there is no direct access to single nodes hence, finding the k th item has a complexity of (n) as all nodes have to be processed successively until the k th item is reached (compared to (1) for arrays) furthermore, there is no possibility of going backwards in case a node that has already been processed is needed (such as n i 1 for deleting n i ) idea: maintain two links for each node, one to the item before and one to the item after so called doubly linked lists HEAD NIL n 1 n 2... n k 1 n k NIL doubly linked list with nodes n i, 1 i k TAIL PD Dr. Ralf Peter Mundani Algorithms and Data Structures Summer Term 2018 15
overview prelude linked lists stacks queues trees PD Dr. Ralf Peter Mundani Algorithms and Data Structures Summer Term 2018 16
Stacks fundamentals for many applications rather hard restrictions w.r.t. accessibility of data structures can be made in order to hide (often complicated and, thus, error prone) implementation details such as links or indices allow more flexibility with fewer operations famous representatives of such restricted access data structures are so called stacks or pushdown stacks, i.e. one dimensional structures with LIFO (last in, first out) ordering that support two types of basic operations push (val): inserting one element with value val at end (top) of stack source: wordpress.com pop: removing one element from end (top) of stack and returning its value PD Dr. Ralf Peter Mundani Algorithms and Data Structures Summer Term 2018 17
Stacks fundamentals (cont d) stacks are, for instance, used for evaluating arithmetic expressions (e.g. within pocket calculators) arithmetic expressions can be written in different notations prefix: operands appear after the operator infix: the customary way of writing arithmetic expressions (but parentheses may be required) postfix or reverse Polish: operands appear before the operator therefore, stacks require arithmetic expressions to appear in postfix notation so the operands can be on the stack when the operator is encountered (as expression are processed from left to right) and, thus, intermediate results can again be pushed to the stack PD Dr. Ralf Peter Mundani Algorithms and Data Structures Summer Term 2018 18
Stacks implementation of stacks stacks to be implemented, for instance, as arrays (in case the maximum size can be predicted in advance) linked lists assume, there s a primitive stack (of fixed size n) implemented as array further assumptions and requirements are necessary it s not possible to push an element on a full stack it s not possible to pop an element from an empty stack index last indicates the last used element in a stack last 3 9 7 size n 8 hence, operations push and pop ready to be implemented 4 1 PD Dr. Ralf Peter Mundani Algorithms and Data Structures Summer Term 2018 19
Stacks implementation of stacks (cont d) push (here with variables n and last with global visibility ) procedure PUSH (S, val) if last n 1 then last last 1 S(last) val fi end where S indicates a stack and val the value to be pushed on the stack note: there s no error in case stack is full PD Dr. Ralf Peter Mundani Algorithms and Data Structures Summer Term 2018 20
Stacks implementation of stacks (cont d) pop (here with variables n and last with global visibility ) procedure POP (S) tmp false if last 0 then tmp S(last) last last 1 fi return (tmp) end where S indicates a stack PD Dr. Ralf Peter Mundani Algorithms and Data Structures Summer Term 2018 21
Stacks example evaluation of an arithmetic expression A B C (D E) using a stack in postfix notation: B, C, D, E,,, stack S push (S, B) push (S, C) push (S, D) push (S, E) E push (S, pop (S) pop (S)) D push (S, pop (S) pop (S)) C C push (S, pop (S) pop (S)) B B B A E D C A B note: operators and have to switch operands due to LIFO ordering PD Dr. Ralf Peter Mundani Algorithms and Data Structures Summer Term 2018 22
overview prelude linked lists stacks queues trees PD Dr. Ralf Peter Mundani Algorithms and Data Structures Summer Term 2018 23
Queues fundamentals other representatives of restricted source: callcentrehelper.com access data structures are queues one dimensional structures with FIFO (first in, first out) ordering that support two types of basic operations insert (val): inserting one element with value val at end of queue remove: removing one element from queue s front and returning its value in comparison to stacks, the order of processing reflects the order of arrival of elements (also referred to as first come, first serve) queues to be implemented, for instance, as arrays (in case the maximum size can be predicted in advance) linked lists PD Dr. Ralf Peter Mundani Algorithms and Data Structures Summer Term 2018 24
Queues implementation of queues assume, there s a primitive queue (of fixed size n) implemented as array further assumptions and requirements are necessary it s not possible to insert an element into a full queue it s not possible to remove an element from an empty queue variable used indicates the number of already used cells indices first and last indicate the first and the last used element in a queue ( circular array: wraparound back to 0 at end of array) used 5 array index last 1 first 7 0 1 2 3 4 5 6 7 8 9 15 18 4 7 11 size n 10 hence, operations insert and remove ready to be implemented PD Dr. Ralf Peter Mundani Algorithms and Data Structures Summer Term 2018 25
Queues implementation of queues (cont d) insert (here with variables n, used, first, and last with global visibility ) procedure INSERT (Q, val) if used n then last last 1 if last n then last 0 fi Q(last) val used used 1 fi end where Q indicates a queue and val the value to be inserted into the queue note: there s no error in case queue is full PD Dr. Ralf Peter Mundani Algorithms and Data Structures Summer Term 2018 26
Queues implementation of queues (cont d) remove (here with variables n, used, first, and last with global visibility ) procedure REMOVE (Q) tmp false if used 0 then tmp Q(first) used used 1 first first 1 if first n then first 0 fi fi return (tmp) end where Q indicates a queue PD Dr. Ralf Peter Mundani Algorithms and Data Structures Summer Term 2018 27
overview prelude linked lists stacks queues trees PD Dr. Ralf Peter Mundani Algorithms and Data Structures Summer Term 2018 28
Trees fundamentals definition: a tree is a finite set of nodes such that 1) there is one specially designated node called root 2) remaining nodes are partitioned into n 0 disjoint sets T 1,, T n where each of these sets is a tree (so called subtree of the root) 3) there exists exactly one path between the root and each of the other nodes in the tree root links between nodes might point downwards (away from the root) or upwards (towards the root) depending upon the application PD Dr. Ralf Peter Mundani Algorithms and Data Structures Summer Term 2018 29
Trees fundamentals (cont d) further definitions each node (except the root) has exactly one node above it which is called its parent hence, the nodes directly below a node are called its children nodes with no children are called leaves or terminal nodes hence, nodes with children are called internal or non terminal nodes parent children internal nodes leaves PD Dr. Ralf Peter Mundani Algorithms and Data Structures Summer Term 2018 30
Trees fundamentals (cont d) further definitions children of the same parent are said to be siblings a forest is a set of n 0 disjoint trees (removing the root of a tree leads to a forest) the level of a node is defined by initially letting the root be at level one; hence, if a node is at level k, then its children are at level k 1 the height or depth of a tree is the maximum level of any node in the tree level 1 level 2 siblings level 3 height 3 PD Dr. Ralf Peter Mundani Algorithms and Data Structures Summer Term 2018 31
Trees representation of trees idea: using linked lists where one node in the list corresponds to one node in the tree problem: nodes can have a varying number of links difficult to write algorithms for data structures with varying sizes hence, already known list elements to be extended by one additional link as follows value siblings children PD Dr. Ralf Peter Mundani Algorithms and Data Structures Summer Term 2018 32
Trees representation of trees (cont d) example A B E G C D F H I J HEAD A B E G C D F H I J i.e. link pointing to NIL PD Dr. Ralf Peter Mundani Algorithms and Data Structures Summer Term 2018 33
Trees binary trees definition: a binary tree is a finite set of nodes which is either empty or consists of a root node and two disjoint binary trees called the left and right subtree hence, any node can have at most two children ( name binary tree ) examples of binary trees skewed binary tree binary tree full binary tree PD Dr. Ralf Peter Mundani Algorithms and Data Structures Summer Term 2018 34
Trees binary trees (cont d) properties a tree with n nodes has n 1 edges (i.e. links); this is true for all kind of trees and follows directly from the recursive definition max. number of nodes n in a full binary tree of depth k 0 is 2 k 1 the height of a full binary tree with n nodes is about ld(n) (i.e. log 2 (n)) for representing binary trees, linked lists can be used again (labelling one link as left and one link as right) value left right drawback: determination of node s parent due to missing upward link PD Dr. Ralf Peter Mundani Algorithms and Data Structures Summer Term 2018 35
Trees binary trees (cont d) example A B F HEAD C G A D E B F C G D E i.e. link pointing to NIL PD Dr. Ralf Peter Mundani Algorithms and Data Structures Summer Term 2018 36
Trees binary trees (cont d) adding a node to a binary tree is a quite simple task 1) find a corresponding node X with at least one free link left or right 2) link from X to the new node Y, thus, X becomes parent of Y Y X X Y PD Dr. Ralf Peter Mundani Algorithms and Data Structures Summer Term 2018 37
Trees binary trees (cont d) deleting a node from a binary tree is a more complex task to be distinguished a) deleting a leaf node b) deleting an internal node with one child c) deleting an internal node with two children cases a) and b) are quite easy to process, but case c) requires further understanding case a) case b) case c) PD Dr. Ralf Peter Mundani Algorithms and Data Structures Summer Term 2018 38
Trees binary trees (cont d) case a: deleting a leaf node 1) find the corresponding parent of the leaf node to be deleted 2) set the parent s link to NIL node to be deleted PD Dr. Ralf Peter Mundani Algorithms and Data Structures Summer Term 2018 39
Trees binary trees (cont d) case b: deleting an internal node with one child 1) find the corresponding parent of the node to be deleted 2) set parent s link to child of the node to be deleted node to be deleted PD Dr. Ralf Peter Mundani Algorithms and Data Structures Summer Term 2018 40
Trees binary trees (cont d) case c: deleting an internal node with two children 1 1) find the in order successor (IOS) of the node to be deleted 2) copy IOS to the node to be deleted 3) i. if IOS has no children, simply delete it ii. if IOS has a right child ( ), set IOS s parent ( ) to IOS s right child node to be deleted in order successor IOS 1 as suggested by T. HIBBARD in 1962, guarantying that the heights of the subject subtrees are changed by at most one PD Dr. Ralf Peter Mundani Algorithms and Data Structures Summer Term 2018 41
overview prelude linked lists stacks queues trees PD Dr. Ralf Peter Mundani Algorithms and Data Structures Summer Term 2018 42