Open navigation menu
Close suggestions
Search
Search
en
Change Language
Upload
Sign in
Sign in
Download free for days
0 ratings
0% found this document useful (0 votes)
24 views
79 pages
DSA
Digital signal
Uploaded by
Ramzan Muhammad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here
.
Available Formats
Download as PDF or read online on Scribd
Download
Save
Save DSA For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
0 ratings
0% found this document useful (0 votes)
24 views
79 pages
DSA
Digital signal
Uploaded by
Ramzan Muhammad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here
.
Available Formats
Download as PDF or read online on Scribd
Carousel Previous
Carousel Next
Download
Save
Save DSA For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
Download now
Download
You are on page 1
/ 79
Search
Fullscreen
Data Structures and Algorithms IN 2110 Reference: Why dowe need data structures? = Their impact is broad and far reaching © Internet © Biology © Computers: o Computer graphics oo Security, © Physics ete = Old raat, new opportunities * To solve problems that could not otherwise be addressed © Ex: metwork connectivity. © For intellectual stimulation © Great algorithms are poetry of computation * To become a proficient programmer co Toorganize data © Tocreate more efficient computer programs ‘© They may unlock secrets of life and of the universe © Ex; machine learning algorithms, data mining algorithms © For funand profit Measurements for Efficiency of an algorithr/salution 1, Space 0 Memory usage of a data structure operation should be little as possible. 2. Time © Running time or execution time of operations of data structure should be small as, possible ‘Cost of an algorithm/solution (what we have to sacrifice) 1[Page1. Space (for storing each data item) 2. Time (ta perform each operation) 3. Effort (for programming) ‘note: each problem has constrains on available space and time, ‘There are two types of data structures, 1. Abstract data structures # Normally called abstract data types (ADT) 2. Concrete data structures + Generally the term ‘Data structurest refers to conerete data structures ‘Watch: https://www youtube.com /watch?v=CCaSavmuSt Abstract Data Structures Also called Abstract data types (ADT) Abstract data type is.a collection of data with a set of operations (suchas insert, delete, search etc) supported to- manipulate the structure, ‘This is a mathematical/ logical madel which looks at the abstract view of data. © Collection of data items '* Operations supported on them ‘+ Types of parameters of the operations ‘This applies the concept of abstraction ta design of data types. Examples: list, set, stack, queue, priority queue, dictionary Lists and Sets ‘The difference between list and se ist allows duplicity ‘Some operations a List data type supports: + addlelement} * clear) = copy(}:set ference(set}:set * remove(element) © intersection(set): ‘+ isDisjoint(i:boolean ‘+ isSubset():boolean 21PageStack and Queue eorr6 lor Figure 1: Stack and queue Stack Figure 2: Push (putinto stack) and Pop (pull out from stack) Last In First Qut (LIFO) ‘Some operations a stack data type supports: '* push(new_item: item_type) © Adding a new item © pop(jiitem_type (© Remove the most recently pushed item and return it ‘© Returns the last pushed item © is_empty(): Boolean © Returns true if there are na items in the stack ‘© is_fulll): Boolean © Returns true if stack is full + get_size(}: Integer © Returns the number af elements in the stack 31PageQueue Back Front I Dequeue Enquieu® 1 Figure : Enqueue and Dequewe In a queue First In First Out (FIFO) ‘Some operations a queue data type supports: * enqueue(new_item: item_type) © Adding a new item + dequeue(}item_type (© Removes the item from the front af the queue and return it ‘© front():item_type © Returns the item at the front of the queue * isLempty(h: Boolean © Returns true ifthere arena items in the queue © is full: Boolean © Returns true if queue is full + got_size(}: integer © Returns the number af elements in the queue ‘note: Data structures such as Arrays, linked lists could be used to implement these ADTS. Refer: ht n.wikibooks org/wiki/Oata Structures) nd Queu Concrete Data structures Generally the term ‘Data structures’ refers to conerete data structures ‘Concrete Data structures are the implementation of abstract data structures. Examples: arrays, linked lists, trees, heaps, hash table From here onwards | use the term ADT to Abstract data structures and the generic term data structures ‘to Concrete data structures. ‘note: Further Understanding of ADT and Data Structures 41P age© ADT isa logical description and data structure is concrete. ‘© AOTis the logical picture of the data and the operations to manipulate the component element of the data. © Data structure is the actual representation of the data during the implementation and the algorithms to manipulate the data elements © ADT isin logical level and data structure Is in implernentation level. ‘© Example: List is what you want,
implements StackcE>{ private int capacity; private E[] datastact private int top = - public Arraystack(int capacity){ ‘this.capacity-capacity; /faatastackenen E[this. capacity: datastack=(EL])new Object( this, Jicannot create a generic array &{) acity]; d public int sizet){ Hfcode goes here } public boolean 4sempty()( /Teode goes here } Public void push(E e)f /Tcade goes here ? public E peek(){ //code goes here } public E pop(){ /feode goes here t ‘The above code seement implements Stack using arrays. ‘The following cade segment implements Stack using linked lists. ‘This is the way how we serve different clients requesting for same ADT with different requirements, i.e “Arrays or linked lists or ete. TiPage//implenent ADT Stack using Linked list data structure /fclass SinglylinkedStack library can be found in net public class LinkedStackcE> implements StackcE>{ private SinglyLinkedList
List = new SinglylinkedList«>()5 public int size(){ /fcode goes here I Public boolean istmpty(){ Jieode goes here , public void pusn(E e)f /[code goes here I public € peek(){ /fcade goes here t public & pop(){ {feode goes here } itis clear that there isa separation between interface and implementation of ADTs using Data structures. Benefits of this separation: ‘© Client can't know details of implementation => Client has manyimplementation from which to choose, ‘© Implomentation can’t know details octiont needs => Many clients can re-use the same implementation, © Design: creates modular reusable libraries © Performance: use optimized implementation where it matters. Terminology: ‘+ Client: program using operations defined in interface ‘© Implomentatian: actual code implementing operations ‘+ Interface: description of data type, basic operations “note that the above implementations are nat the only way. There can be several ways. @ ‘We have discussed Data structures up to a certain level. Now let’s see what Algorithms are. S1PageAlgorithms ‘Step by step recipe for performing a task within a fine period of time. “Algorithms often operate an a collection of data, which is stored in a data structure: Aproblem can be solved by many algorithms. ‘Sorting problems can be solved using following algorithms: © Bubble sort © Selection sort + Shell sort = Merge sort ‘Character -s of a good algorithm: + Finiteness © Terminates after a finite number of steps and each step should be executable in finite amount of time © Noambiguity © Each step of an algorithm must be precisely defined © Input (© Algorithm should have a finite number of inputs * Output (© Analgorithm has one or more cutputs(at least one output) © Can be proved to produce the correct output for a given input © Effectiveness (© Steps should be simple and basic Algorithm creation techniques 1. Flow chart 2. Pseudo code 3. Programming language Numerical factors for measuring the goodness and effectiveness of an algorithm. 1. Running time 2. Total memory usage 91PageReference: Chapter G, Data Structures and Algorithms in Java 6” edition by Goodrich Stacks ‘Acollection of objects that are inserted and removed according to the Last In First Out (LIFO) principle ‘that can access or remove the mast recently inserted abject _ fa = Figure 4: items ae add (push) and removed (pop) a€ top ofthe stack Examples: ‘© Internet browsers store the addresses of recently visited sites on a stack. © Eaeh times user visite a page, push(address_ of the_visited_page_just_now) © When user its the "back" button, popi):aiidress_of_the_recently,visited_page ‘© Text editors store text changes in a stack. © “undo” mechanism use this stack ‘+ implementing function calls (ex: revise recursive functions) © Dikstra’s tworstack algorithm Methods (operations) of a stack: 1. Update methods # push{new item: item type) © Adding 3 new item = popl):item_type © Remove the most recently pushed item and return it © Returns null ifthe stack is empty 2. Accessor methods = top(jritem_type o Returns the last pushed item WW] Page© Retuens null ifthe stack is empty isEmpry(): Boolean © Retuens true if there are no items in the stack + isFulll): Boolean © Returns true if the stack is full #size(): Integer © Returns the number of elements in the stack Exercise: Assume that initially stack is empty. “Method Return Value Stack content push(5) (3) [Coushia) (531 sizel) (Sa popt) 3 (5) isEmpty() false (5) opt) 5 0 isEmpty() we 0 popl) push(6) yush(B) 1090) /peek(h (6.8) A simple Array-based Stack implementation ‘You can directly use java.util. Stack interface without defining @ new interface called Stack. 1 [Stack java //Stack interface //E As used to show the type of elements in stack ex: int, String, Dog etc public interface Stackcer { int size(); boolean isEmpty(); void push(E ¢ E peek(); E pop(); U|Page[/arraystack.java /fimplenent ADT Stack using array data structure public class Arraystackce» implements. Stack«E>{ private int copacity; private €[] datastack; HdataStack was made private eco? you cant reach array elements using HHindexes. only the top elenent that you can reach. private int top = -1; public ArrayStack(int capacity){ ‘this.capacity-capacity; //datastackenew E(this.capacity]; //cannot create a generic array F{) datastack=(E] )new Object this, capacity]; } public int size(){ return tope1; } public boolean istmpty(}{ retupn (tope=-1); } public void push(e ©){ AF(size()--capacity) Systen.out.printin("sorry, stack is full."}; else datastack[+etop] = 2; , public & peek(){ ffsometimes alsa referred ta as top() return datastack{ top]; } public pon(p{ if(isempty()) return null; else return dataStacklton--]s 4z|Page//ArrayStack java public class Test { public static void main(Stringl] ares) { ArrayStackeInteger> stack = mew ArrayStackc>(16); stack .push(18) ; System.out.printIn(stack peek(}); stack push(12); stack.push(11); system .out.printin(stack.pop())s stack push(15); ‘System.out.printin(stack.pop()); system.out.printin(stack-pop()}3 System.out.printIn¢stack.LsEmpty()); system .out.print In(stack.pop()}3 ‘System.out.printIn(stack.isEmpty()); ‘System.out-printIn(stack.pop())5 stack push(23) j stack pushC65) ; stack .push(19) stack .push(11) stack .push(18) stack push(2@) j stack .push(4a) ; stack push(67); ‘System.out.printin(stack.size()); stack.push(12) ; system.out.printIn{stack.size()); stack .push(17); System out.printLn(stack «size(}); stack -push(15); y 1 /Output a 10 nu a5 az false 10 true null 8 9 10 Sorry, stack is full. You cannot insert further. ” 4a|Page Halse Htrue inelin this example, we declare the parameterized type of the stack as the Integer wrapper class, Thiscauses the signature of the push method to accept an integer instance as a parameter, and for the return type ‘of both tap and pop tobe an integer. Of course, with Java's autoboxing and unboxing.a primitive int can be sent asa parameter to push. Drawbacks of the above Array-based implementation: © Overflow © Arraysite is fixed © Capacity exceeds from pushing iterns into the stack. © Use resizing array for array implementation, = Loitering © Holding reference to an object when it © This happens in java. © Wejust reduce the variable ‘top’ by 1, we do not delete the element after 3 popl) ‘Therefore that iter is still in the array though we don't use. ie, holding.a reference to am abject when it is no longer needed. is no longer needed. public E popt){ ie(isempty()) return null; else return dataStack[top--]; ) © Sowe can set topsnull at appropriate place in the code as @ solution. @ Haweverit is not possibie all the time, Corrected code without loitering is as follows. public & pop(){ GF(isEmpty())C return null; Jelse{ E answer = datastack{top]; datastack{top)=null; ‘top--5 return answer; d ) © Our reason for returning the cell to.2 null reference is to assist Java's garbage collection mechanism, which searches memary for objects that are na longer actively referenced and reclaims their space for future use, ‘+ Ifthe application needs much less space than the reserved capacity, memory is wasted. a[ PageResizing Array Implementation “There are so many approaches to resizing. Below shows a one-approach among them. Push: double size of the array when array is full Pop: halve size of the array when array is one quarter full ‘Your code ArrayStack java has to be changed as follows when implementing resizing property. Let's implement this feature in a new java fite ResizingArrayStack java. This wil give the same output © public ResizingArraystack(){ dataStack = (E[])new Object[1]; d public void push(e ©){ Uf (size()==datastack. length) resize(2*dataStack.Length); datastack{++top] = ©; } private void restze(int newcspacity)( EL] copy = (GC])new Object (nencapacity); for(int is6;icsize(); i++) copy[' Jdatastack[i]; dataStackecopy; ? public & peek(){ return datastack[top); } public E pop(){ Le (Sséepty() return null; yeiset E answer = dataStack[ top]; fithis step is essential here dataStack{ top)=null; /ithis step is not essential here AF (top==datastacklength/4) resize(dataStack.length/2); top--3 7/this step is essential here return answers 15|PageImplementing Stack with a Linked List Linked Ust is also a concrete data structure, or simply a data structure. ‘What is.a linked list? = Alinked list is a linear data structure where each element is a separate object, + Each element (we will call ta node) of a list is comprising of two items, 1. The data 2. Areference to the next nade = The code fora node is as follows; private class Nodece>{ E item; NodexE> nexts } * The last node has a reference to null. The entry point into a linked list is called the head of the list. Hose Reco ecard? Recordn-t Record ata ats ats Dats b~ bs b~ a ||| Figure 5: Linked Uist Using java class SinglyLinkedList is wery easy, but for learning purpases let's try to implement our own Link list. ‘note Resizing array vs Linked list; Linked list implementation: © Every aperation takes constant time in the worst case ‘+ Use extra time and space to deal with the links (have to go from item to.item, linear time) Resizing array implementation: ‘© Every operation takes constant amortized time ‘Loss time, Less wasted space (constant time) 16| Page[/Ankedderay. java /finplenent ADT Stack using linked Iist data structure Public class LinkedStackcE> implenents StackcE>{ private NodecE> first = null; Pinner class private class NodecE>t & item; Nodest> next , public int siz6(){ J [this 43 my code, there may Jie 2 more feasible method Viehan this Af (Firstesnull){ return 0; Jelse{ count=t; return fsnull (First); } 2 private int count; JImy variable private int iswull(Node x){ dimy method 4F(xanext l=null){ U/there is a better sethod fin page 23 countess return ishull(x.next); Jelse return count; } public boolean istmpty(){ return finst=-null; } public void pusn(e e)f Wade oldFirst = First; first = new Nodecs(); first.iten =e; First.next = oldFirst; t public E peek(){ return Finst.item; } public € pop(){ AF(isEmptyO)) return null; elset E item = first-item; flestefirst next; return ites; # ir|PageReversing an array using a stack Amt[] array = (1,4,2,3,4,6,2,3,9}5 LimkedStack«Integer> stack = new LinkedStacke>(); for(int i = @;icarray. Length; i++) stack.push(array(i}); for(int i = @;i¢array. Length; i++) array[SJestack.pop(); for(int i = @jicarray. Length; i++) System.out .println(array[i]}s Addition of numbers using stacks A UU sev Ul ‘igure 6: addition of two numbers using stacks ue Matching Parenthesis and HTML Tags This involves testing for pairs of delimiters. Input: (() 0) (Roadand baeedl|| oadand | posh 1 ( pwn | oah ( | “Tdi nea) Sy eal pop( | and pop ( eno? ) and | poptast( | PageQueues Acollection that keeps objects in a sequence, where element access and deletion are restricted to the first element in the queue, and element insertions is restricted to the back of the sequence. |.e. First in First Out (FIFO) principle. Back Front Nl Dequeue Enqueue Figure 7: items are added to the back (enqueue) and removed trom font (dequeue Examples: # Calls toa customer service conter = Wait: list ata restaurant © Web server responding to requests ‘Network printer responding to requests Methods (operations) of a queue: 1. Update methods # enqueue(new_item: item_type) © Adding 2 new item to the back of the queue + dequeve( item _type © Remove the item at the front af the queue and return it © Returns null ifthe stack is empty 2. Accessor methods = first()stem_type co Returns the first element © Returns null ifthe stack is empty # size(): integer © returns the number of elements in the queue + istmpty(): Boolean © returns true if the queue is empty i9|PageExercise: Assume thot initially quewe is empty. Method ‘Queue content ‘enquewe(S) ‘is&mptyl) dequeue {) ( ‘isemptyl) ) ‘dequeue () 0) “enqueue (6) (6) “enqueue (8) firstl) Four types of queues: 1, Simple queue © Insertion occurs at the rear of the queue * Deletion occurs at the front of the queue 2. Circular queue © Alllnades are treated as circular such that the first node follows the last node + Here we can meve first element to the back af the queue using rotate() method LOTT HCE HI 3. Priority queue Contain items that have same preset priority © When removing elements, items with the highest priority is removed first hee Fel Lele THe 4, Deque (Double-Ended queue } * Insertion and deletion occur at bath ends ie. front and rear of the queue UHH 20|PageAvray-Based implementation ofa Queue “This method is aso called realization of a queue by an array. You can directly use java utll.Queue without using a new interface, //Queiie. java public interface Queuect> { int size(); boolean isEmpty); void enqueue(E ©); E first(); E dequeue(); } J /arrayQueue. java public class ArrayQueuecE> implements QueuecE> { private int maxSize; JJartay size private E[] dataQueve; Ant front; J/feont locator int roar; Y/near loactor private int nj HYnunber of items public ArrayQuewe(int 5){ maxSize = 5; dataQueve = (E[])mew Object[maxsize]; front = rear = -13 n= 65 , public int sizet)( return 1; } public boolean istmpty()( return ) Public boolean isFull(){ return (nsemaxSize); + public void enqueue(E e){ if(isFull()e=false)( rear=(rear+1)%maxSize; aataqueus[reer]=e; else system.cut.printin(*Sorey you cannot add nore”); ? public & dequeve(){ Af (isEmpty ()e=false){ nes; E answer = dataQueve[ front); Front=(Front+1)&maxSize; return answers jelse return null; UiPage? public E First(){ return dataqueve[ front}; , {[Test. javo public class Test ¢ public static void main(string{} ares) ( ArrayQueuecInteger> queue = new ArrayQueue($); queue -enquewe(S)5 queue -enqueue(3); System.out.printlntqueve-size(}); ‘system.out -printIn(queue-dequeue()); systom.out.printlatqueve. isEmpty); System.out.println¢queve.dequeue()); System. out-printIn(queve.isEmpty()); ‘System.out.printIn{queue-dequeue()); queue .enqueue(s) ; queue -enquewe(8) ; system.out.printIn¢queve.first(}); /foutput I 2 5 false 3 true null 6 + “note ‘There is confusion in the implementation of a simple array anda circular array. Clear it fram Ms ‘Supunmali. © 22|PageLinked List-Based implementation of a Queue ‘Also called realitation of a queue using a linked list //Uinkedquowe. java public class LinkedQueuecE> implements QuevecE>{ private NodecE> first = null; private Nodece> last = null; (dinner class private class NodecE>t E item Node next , public int size() { AF (Finst==null){ return 85 return isnull(first); } } private int count; Jiny variable private int isNwll(Node x){ Jimy method 4 (x.next len) count++5 return isNUl1(x.hext); pelse return count; ) public boolean istmpty{) ( return Firsteenull; d public void enqueue(E ©) { AFCAsEmptyO){ First = new Mocece>()5 first, item = e; first next = mula; last = First; jelset Node newLast = new Nodexe>(); newLast.dtem = ©; newLastnext = null; last next = newLast; Jast = newlast; I } public € first() { return First.dtem; w|Pagepublic © dequeue() { if (istmpty()){ return null; Jelse{ E item = first.item; firstefinst next; return ites; Double-Ended Queues (Deque) Aqueue where insertion and deletion could be done at both front and the back Front Back [ Figure & Geque Deque: © More general than both the stack and queue '* Richer than both the stack and the queue ADTs. ‘+ Provides a symmetrical abstraction Examples: ‘© When modeling any kind of real-world waiting line: entities (bits, people, cars, words, particles, whatever) arrive with a certain frequency to the end of the line and are serviced at a different frequency at the beginning ofthe line, While waiting some entities may decide to leave the line....ec. The point is that you need "fast access" to insert/deletes at both ends of the line, hence a deque. 2 [PageMethods (operations) of a deque: 1. Update methods #addFirst (new_item: item_type) © Adding 2 new item ta the front of the queue + addLast (new Item: item_type) © Adding @ new item to the back of the queue = removeFirst():item_type Remove the iter at the front of the queue and return it © Returns null ifthe queve is empty # removelasti}item_type © Remove the item at the back of the queue and return it © Returns null ifthe queue is empty 2. Accessor methods © first():item_type © Returns the first element © Returns nul ifthe queue is empty + last(}kitem_type © Returns the last efement © Returns null ifthe queue is empty # size(): Integer © returns the number of elements in the queue + istmpty(): Boolean © returns true ifthe queue is empty Exercise: ‘Assume that initially Deque is empty. ‘Method Return Value content ‘addlast(3) 5) addFirst (3) (3,5) ‘adaFirst(7) (7.3.8) first) 7 (7.3.5) removelast() 5 [75) ‘size() z (7.5) removelast() 5 7) ‘remover irst|) 7 M ‘addFirst (6) 16) Tastl) 6 6) ‘adarirst (8) (8.6) ‘isEmpty() false [ise tast() 6 Lise, sl PageImplementation of Deque Java.uit, Deque is an interface in java that you can use directly when implementing a Deque. //Deque. java public interface DequecE> { int size(); boolean istmpty(); € first(); E last()3 void addFirst(E e); void addtast(E ©); E removeFirst(); € removetast(); Priority Queues Aqueue which stores the priority additionally with the items in the queue Priority Queue: ‘* Items are ordered by a priority value, at the insertion, © Item with the largest priority is always at the front. '* Remove the element from the queue that has the highest ps rity and return it. Methods (operations) of ap! rity queue: 1. Update methods # insert{key,value} 1. if the no: of items in the queue is maxsize, then na more items can be inserted (queue is full), else step 2. 2. Ifinitially there are ne more elements, insert new item at first position zero (0) index, otherwise if new item is larger than the existing ane’s shift those. elements upward ane by one till the larger one is found. 3. Insert item to that new location. © removeMin():[key,value] 1. if the number of items in the queue is zero then no more items to be deleted, Quit, else step 2 2. Remove the front element. 2. accessors ilkey value} if priority queue is not empty, return a queue entry (key, value) having minimal key, Ese returns null. # size(}integer + istmpty():Boolean 26|PageExercise: Assume thot initially Priority Queue is empty. Priority queue content (G.61(5,01/8.0) (3,8),(5,A},(9,C} {(S.A}(9,C)) ‘removeMin() size) |{(5.A)49.Ch} insert{7,D) {aes removeMin() {(7,0)19,CN) femoveMini) {(9.0), removeMin() emoveMini) ‘isEmptyl) Task: ‘Assume that you have a set of numbers like 5,4,3,7,1,6. ‘Consider the priorities of these numbers ate equivalent to number itself. Hint: Assume 1 has higher priority than 2. Implement a priority queue with inserting operation to illustrate the abave scenario. a7|PageLesson 03; Linked Lists ‘Alinear data structure where each element is a separate object Issues/ limitations of arrays: ‘© Changing size of the array requires a new array and copy all data fram old sized array to new sited array and then continue. © Data inthe array are next to each other sequentially in array; we need to shift some data. memory, s0 te ingert an item inside the ‘To overcome the above limitations, we need Linked structures. Linked data structures: + Adata structure which consists of a set of data records (nodes) linked tagether and organized by references (links or pointers). The link between data can also be connector. Major differences between Array data structure and Linked data structures: Linked data structures ‘Areay data structure Easier to grow organically Needs to be known the size in advance, or have to re-create when it needs to. grow references are compared with equality, no need to | have perform arithmetic operations on references «do arithmetic operations on references (pointers) _| when referring “Types of linked data structures: © Linked lists + Search trees © Expression trees “note Linked data strisctures are also key building blocks for many efficient algorithms, such as topological sort and set union-find. In this lesson we study about link lists. ‘Traversing/ link hopping/ painter hopping: ‘© Starting at the head and moving from one nade to another node by fallowing each node's next reference, 28|PageLinked list + Alinear callection of data elements called ‘nodes’ pointing to the next nade by means of pointer © The principle benefit of a linked list over a conventional array is that the list elements can be easily be inserted or removed without reallocation or reorganization of the entire structure becoz data items need not be stored contiguously in memory or on disk. ‘* The major limitation of linked lists is that inability to random access to the data due to absence of a efficient indexing, Node: + Composed of data anda reference 12| e+» 99] e —>/37| e >] Figure 9: Mow Hades are connected ina inked lst ‘Types of linked lists 1 Singly linked list START Mode 1000) 4 | 2000 ffs 3000 — Lf ¢ | ] 1090 2000 3000 \ INFO Fd LINK'er NEXT Field NULL Value Figure 10: Sing linked ist © Eachnade is divided into two parts. i.e. INFO field(data) and LINK field (pointer) 1. The first part contains the information of the element and is called ‘INFO Field. 2. The second part contains the address of the next node and is called ‘LINK Field” or ‘NEXT Pointer Field’. Implementation of singly linked list node in java: This is implemented as a class in java, and struct in c. class Node
{ E Aten; Y/data (INFO) Node
next; Viveference to the next node (successor} > “note In this lesson aur interested area is the singly linked list. wlPase2. Doubly linked list FIRST Node LAST NULL Vile PREVFidd INFO Pik | NEXTFe NULL-Vidue ‘Figure 12: Doubly linked tst © Here each node is divided into three parts: 1. The first part is ‘PREV’ part. It is previous pointer field. It contains the address of the nade which is before the current nade. 2. The second part is the ‘INFO’ part, It contains the information of the element, 3. The third partis 'NEXT" part. It is next pointer field, It contains the address of the nade which is after the current node. Implementation of singly linked list node in java: This is implemented as a class, and struct in c, class Nodest>{ E item; ffdata (INFO) Nodece> next; Hireference successor Nodece> prev; ffeeference predecessor + 3. Circular linked list START Mole 1000) " 3000 1000 INFO Feld LINK or NEXT Field Figure 12: Circular tink lst © Here the last node dees not contain NULL pointer, Instead the last node contains a pointer that has the address of first node and thus points back to the first node. © The structure and implementation of the nade is as same as the node of a singly linked lst. 30/PageSingly Linked list Features of Singly linked list ‘© Only one variable is used to access any nade in the lst. © Exif the first nodes ‘p” ‘Then the second node is ‘p.next” ‘The third node id ‘p.next.next’ etc ‘© Last node can be recognized by null reference field Figure 13.: Inserting an cloment at the Head of a Singly Linked List tall ae @ all newest ee as aby al newest ee oe ‘ Figure 14: tnarting a element at the Tal of a Singly Unked Ust 3a|Pagehead @ « head @ o bead © 1) ‘Figure 15:: Removing an element from the Head of the linked list //$LuNode: ova. (NOOE) public class SLLNodece> { public E data; public SLtNode next; public SttWode(e ){ ‘this(e, nul); , Public SLiNade(E ©,SLLNade n}{ data = @ next = 1 y y //SinglyLinkedList.java public class SinglyLinkedList ck>¢ private SLiNode head, tail; private int n= 6; public SinglyLinkedList(){ head = tail = null; - public boolean isémoty(){ return head == null; } public void addtonead(e e){ ‘head = new SLLNode(e, head); 32/PageAf (tad lemmull){ tail = head; nets public void addToTail(é e){ AF (1 istmatyC)){ tail.next = mew SLLNode(e) ; tadl = tail.next head = tail = new SLiNode(e)3 public € deleteFromieaa(){ Ee = (E) head,datas if(head=stail) /#4# only one in the List head = tail = null; else head = head next; ay return e; } public & deleteFromtail(){ Ee = (€) tail.data; if(heads=tail) /#4F only one An the List head = ‘tail = null; else( SUuNode temp; for(tesp = head; tenp.next!=null; tesp=temp.next); tail = tonp; tailsnext = null; } eed return @; + public veid delete(E e}{ //leletes the First object matches AF(tiskmptyO)){ Af (Neadestail && exchead.data){ head = tail = null; nes ad.data) = Read.next; SLiNode pred, temp; for(predshead, tempshead. next; tenplsnull && tenp.datalee; pre = pred.next, terpeteap.noxt 33/Pageif (temp! =snull){ Jif e was found pred.naxt = temp.next; if(tenp=-tail) —//if e is in the last node ‘tail = pred; ? ? } } public int size()( , atures public E getitead(){ return (£) head.data; t public € getTail(){ return (E) tail.data; } ) [rest java import java.util.Seanner; public class Test ¢ public static void main(stringl] args) ( SinglyinkedListcEnteger> sll = new SinglyLinkedtiste>(); sLl.addToHead(10) ; sllraddTaHead(1@) ; sll addTaHead(12) ; sll,addToTail (2) 5 systen.out.println(s11.getTai1())3 1128 System.out.println(s1l.getHead())5 HAR System.out.printin(sil.deleteFromlead())3 //12 system .out.printin(sil.size() 1B system .out.printin(s11.gethead()); me sll.delete(10) ; system.out.printIn(s11.getHead()); me , } smote In singly linked list, deleteFromTail() and addToTail(} lead to an issue scanning the entire list to stop right in front of the tail. Solution: Doubly linked list. 34|PageDoubly Linked List Doubly linked list node consists of three fields 1. Data/ INFO field 2. Next field (reference field to successor) 3. Prev field (reference field to predecessor) header next next next prev prev i 3 Figure 16: Doubly Linked it Implementation: /finserting to the tail public void addTotail(E e){ 4¢(Lsempty ()){ tail = new DitNode(e,mull,tail); tail. prev.next = tail; Jelse ead = tail = new DLLNodece); ness } //deleting from the tail public E doleteFrontail(){ Ee = (E) tail.data; if(hedde=tail) Jf only one din the List head = tail = null; else{ tail = tail.prev; tail.next = null » ; return €3 ) “note ‘Yau can use javavutil.LinkedList class. All the methads we implemented our self could be found here, java.titil.LinkedListeInteger> 11 = new java.util. Linkedtiste>(); Li.add(9a); ‘system.out.println(1l getFirst ())5 LLaddFinst(7); ‘system.out.printin(11.size()); ‘System.out.printin(1}.renoveLast()); ‘Systom.out.printin(11.size()); a5|PageUsage of Doubly Linked list: A great way to represent a deck of cards in a game. The browser eache which allows youto hit the BACK button (a linked list of URLS) Applications that have a Most Recently Used (MRU) list (a linked list of file names) A stack, hash table, and binary tree can be implemented using a doubly linked list. ‘© Undo functionality in Photoshop or Word (a linked list of state) Circular Linked Lists ‘Alinked lst, which is essentially 2 singularly linked list in which the next reference of the tail node is set ‘0 refer back to the head of the list (rather than null). head tail Figure 17: Circular Leked ist ‘We use this madel to design and implement a new CircularlyLinkedList alss, which supports all of the public behaviors of our SinglyLinkedlist class and one additional update method, ie. rortate() which moves the first element to the end of the Linked list. (head) tail * (head) ‘ail ' ~ = 1 (a) (b) Figore 18: rotate) method (a) before the rotation; representing sequence { LAX, MSP, ATL, BOS} {b) after the rotation; representing sequence { MSP, ATL, BOS, LAX } 36|PageFigure 19: Adding a new data item Usage of circular linked lists: ‘+ Round Robin scheduling (haw each process will come to ready queue and get chance to execute incPuy ‘+ Multiplayer board game (how players are getting chance to play) Round Robin Scheduling ‘Around-robin scheduler could be implemented with a traditional linked list, by repeatedly performing ‘the following steps on linked lst L process p = L.removeFirst( )3 Give a tine slice to process pi Leadduast(p); ‘With this new operation in circular linked list rotate(}, round-robin scheduling can be efficienty implemented by repeatedly performing the following steps on a circularly linked Hist C: Give a time slice to process C.first( )j C.rotatet ); a7 |PageLesson 04: Implementation of Stack and Queues using Linked Lists //SLUNede. Java public class SLLNodect> { public € data; public SLLNode next; public SLUMede(E «)( ‘this(e, null); } public SLUMede(E ©, SLLNode n){ data = 6 next = 1m t y ‘note sometimes we use a private inner class inside the LinkedStack or LinkedQueue to hide the: implementation of the SLLNode class. in the following codes of implementation | have used a provate Node class instead of public SLLNode class. Implementation of Stack (LIFO) Elements are added to head or the first element; elements are removed fram the head or the first ‘element. //Linkedseaek. java public class LinkedStockcE> implements StackcE>{ private Nodece> first » null; private int m = 8; (dinner class private class Nodece>{ E item Node
nexts , public int size()( return nj t public boolean 4sEnpty(){ return firstewnull; BB] Pagepublic void push(e ){ Jiada te Front Wode oldFirst = First; first = new Nodecs(); irst.item = €5 first-nect = aldFirst; nee } public E peek()4 return First.dtems ? public pon(){ Hiremeve from Front Af(isempty()) return null; else E item = first.iten; Firstefirst.next; return ites: ) ? Implementation of Queue (FIFO) Elements are added to the rear of the linked list or to the last element; elements are removed fram the ‘head or the first element. {/LinkedQueue. java public class LinkedQueuecE> implements QueuecE>{ private NodecE> first = mull; private Node
last = null; private int n = @; s/inner class private class Nodexe>{ E item; Node next; } public int size() { return n; } public boolean istmpty() { return firstesnull; 39|Pagepublic void enqueue(e e) { AF (isEmpty ()){ first = mew Nodece>(); first.iten = ¢; first.next = null; last = first; jelse{ Node newLast = new Nodexe>(}; newlast.item = ©; RewLast.next = null; last nent = newtast; Jast = newkast; } nites } public E first() ( return first-item; 1 public € dequeue() ( AFCisEmpty())4 return null; jelse{ E iten = first.item; fipsteFirst next; ns} return ites; I , 40] PageLesson 05: Recursion Aprogramming technique in which a method/ function make one or more calls to itself during execution ‘Characterist of a recursive function: © The method callsit self = At some point, this recursion should be terminated © Otherwise itleads to an infinite laop © Those points are called base cases. Base case and the recursive case: Let's consider an example; 1 n=0 in.(n — 1)! nz This definition is typical of many recursive definitions of functions. «First we have one or more base case stating that n! = I forn = 0. * Secand we have one or more recursive cases, which define the function in terms of itself Examples of the use of recursion: ‘+The factorial funetion ©The Fibonacci function © English ruler structure + Binary search © When the sequence is unsorted, the standard approach to search for a target value is to use a loap to examine every element, until either finding the target or exhausting the Gata set. This algorithm is know as linear search, or sequential search, and it runs in ‘Ofn) time (Le, linear time) since every element is inspected in the worst case. + File system A[PageExample: Factorial of n Implement with using a for loop: int factorial (int n){ int fact = 1; AF (n==1) return facts else{ for(int i=1;icen; ite) fact "= 1 return fact; ? } Implement using a recursive funetion: int factorial (int n){ if(n=n8) return 1 else return n*factorial(n-1); 2 Execution model for factorial: Calling the recursion function tactorial(5) S*actorial(4) s"{a*factorial(3)) S*{4*(3*factoriall2))} s4ar(3*{2*factorial(1))}) Returning values after the base case Seatia2"ayi} sryarqa*2)) stare) 524 120 A2|PageExample: Fibonacci Series 0 n=0 Fib(n) = 41 a=1 pion —1) + fo(n= 2) n> ‘The Fibonacel series is as follows: ® & & & % SF Bam Implementing using a for loop: int fib(int n)( int oldi = 95 int old? = 15 Af (n~1) return old; else if(n==2) return old2; else( int fib = 6; Forint 193jicen;i+4){ fib = oldi + old2; oldi » old2; old2 = fib; ) return fib; ) , Implement using a recursive funetion: int #4b(imt n){ if (n=) return 0; else if(n==2) return 1; else return Fib(n-1)+Fib(n-2)5 } Invocation tree: ‘+ Atree structure which shows the invoking precedence of a recursive function Invocation tree of Fibonacci function if n=a: eure ‘Figure 20: Invocation tree fr ib) 43|PageCategorization of Recursive functions Recursive functions can be categorize in different ways, we examine 3 of them here. 1. Tail recursive and non tail recursive 2. Direct recursion and indirect recursion 3. Linear recursion, binary recursion and multiple recursion ‘Tail recursive Vs Non tail recursive AArecursive function is said to be tail recursive if there is nothing to do after the methods returns expect. retum value. Otherwise itis called non-tail recursive Example for non-tail Recursive function: factorial!) When returning back from a recursive call, there is still one pending operation, multiplication. ‘Therefore, factorial() is a non-tail recursive function, Example far tail Recursive function: void tailrec(int i){ iF(i38) System.out.println("i = "+41)5 tailrec(i-1) ? When returning back from athe above recursive eall, there is still no pending operation, ‘Therefore, it is a tail recursive function, “note that the following example is nota tail recursion. void prog(int 1) iF (430){ prog(i~1)5 System.out.printin("i = “#i); prog(i-1); y t Converting a non tail recursive function toa tail recursion Ant tailFactorial(int n, int sofar)( Af(n==4) return sofar; else return tailfactorial(n-i,s0fartn); 4a|PageIndirectly/ Mutually Recursion Direet recursion: IER) makes a recursive call to Xl) itself, it is called direct recursion, void X(){ ; xO; Indirect recursion: If recursion methods call them indirectly through calling other methods, itis called indirect recursion. In general, indirect recursion is a circular sequence of two or more recursive calls el>f)>.->al) void B(){ #03 E void #()( 805 + void main(}{ 80: Linear recursive Vs Binary recursive Vs Multiple recursive Linear Recursion: © Ifa recursive call starts at most one other, we cal ita linear recursion. © Gx: factorial{int n), power(double x, int n) public double pewer(double x, int n) { if (n == @) return 1; else { double partial = power(x, n/2)3 double result = partial * partial; if (1% 2 == 1) result = x3 return result; AS |Pagereturn 64 « 64 « 2 = 8192 ‘Figure 21: Recursion trace for an execution of power(2, 13), Binary Recursion: © Ia recursive call may start others, we callit a binary recursion. + Ex:fibfinta), bibarySumfint{ | data, int low, int high) public static int binarySum(int[ ] date, int low, dnt high) { Af (low > high) // zero elements in subarray return O; else if (low == high) // one element in subarray return data[ low); else { int nid = (low # high) / 2; return binarySumdata, low, mid) + binarySum(data, midel, high); a5 or, ap ah dp af tp ‘Figure 22; Recursion trace for the execution of binarySum{data, 0, 7) MB] Pave‘Multiple Recursian: © Ifa recursion call may stait three or more others, we call it a multipte recursion. ~% 2. © (a8))) Figure 2%: Recursion trace for ao exertion of PutsleSalvet3, §, 4) Exercise: What the Tower of Hanol algorithm? imptementit using Java. Refer: ‘hitevivwwutorialspoint.com/data structures algorithms/tower of hanoi.htm ‘http://www. youtube,com/watch?v=MbybmBZViWk y ialepoint.com/data_ structures algorithme/tower of hanol_in_chtm source aux destination Figure 24: The Tower of Hanol aP|Page‘The mission is to move all the disks from source tower to the destination tower without violating the ‘sequence of arrangement. Rules: © Only one disk can be moved among the towers at any given time. ‘+ Only the top disk can be remaved. ‘© Notlarge disk can sit over a.small disk. ‘note Tower of Hanoi puzzle with n disks ean be solved in minimum 2°-1 steps, ‘Algorithm: Disks are numbered from top to bottam fram I to n, ‘Our ultimate goal is to move disks n from source to destination and then put all the other (n-1) disks conte Step 1 - Move n-1 disks fron souree to aux Step 2 - Move n® disk from source to destination Step 3 - Move n=1 disks from aux to destination Now we can imagine to apply the same recursive way for all given set of disks. Arecursive algorithm for tower of Hanai can be driven as fellows. start Procedure Hanoi (disk, source, destination, aux) IF disk == @, THEN Mave disk from source to destination ELSE HManoi(disk-1, source, aux, destination) ddstep 2 Move disk from source to destination distep 2 Hanoi(disk-1, aux, destination, source) distep 3 END IF END Procedure stop 48 [PageLesson 06: Trees Anon linear data structure that represents nodes connected by edges, Tree provides a natural ‘organization data. Tree cane a ADT or adata structure depending on the implementation, ‘Components of a tree: 1. Nodes 2. edges Figure 25: trae with 17 nodes representing the organization ofa fictitious corporation. Refer: hitpy//www.tutoriaispoint.com/data structures algorithm data_steucture.h Examples where trees are used: ‘© Organizational hierarchy of a company. rarchical relationship between files and directories in a computer's file system © Components of a structured document such as a repart, book ete. Binary Tree: ‘+ Aspecial data structure used for data storage purposes. A binary tree has.a special condition that each node can have only two childran at maximum, A9|PageTerms related to Tree Data Structure Parant Noss Sub-ime ‘eat todo Figure 26: Terms rented to Tree Root: Element at the top of the hierarchy ‘Child: Node below-a given node connected byits edge downward ‘Grandchid: Elements next in the hierarchy ‘Siblings: The nodes belongs to the same parent ‘Ancestors: Nodes situated in the path fram a given node to the root node Descendents: Nodes that are descendents of a given node Leaf; Node which does not have any child nades ‘Sub-tree: Descendents of a given node ‘Levels: Generation of the node ‘Visiting: Checking value of a nade when control is on the node ‘Traversing: Pasting through nudes ina specific order ‘Key: represents a value of a nade based on which a search operation is to be carried out for a node SO] PageLevel 0. Level 1. Level 2 Level 3 Figwrs 27; An sample tse Leaves = (Mike, Ai, Sue, Chris) Parent(Mary) = Joe ‘Grandparent(Sue) = Mary ‘Siblings(Mary) = (Ann, John} Ancestors(Mike) = (Ann, Joe} Descendents(Mary) = (Mark, Sue} Node degree: Number of children a given node has ‘Tree degree : Maximum of node degrees Node degrees of nades in the Figure 27 are as follows: NodeDegreelloe)=3 <= Tree degree NodeDegree(Ann} = 2 NodeDegree(Mary) = 1 NodeDegree(John) = NodeDegree(Mike) = 0 NodeDegree(Ai) =0 NodeDegree(Mark) = 1 NodeDegree(Sue) = 0 NodeDegree(Chris) = 0 Si|PagePath: if n;, Azyatu is a sequence of nades in a tree such that nis the parent of ma; for 1¢=i ‘sequence is called a path from noden; tons. then this Length of the path = number of nodes in the path= 1 ‘note Node to Node: length of path = 0 Height of a nod length of the longest path from a nade toa leaf Height of a tree: height of the root node Depth of a node: length of the path fram the raot to the given node: Recursive Definition of a Tree: A single node by itself isa tree. This nade is also the root of this tree. Letty, ty..th be disjoint trees with roots r:, r,..ft respectively, and let R be another node. We can get a new tree by making R the parent of the nodes fy, tapute Tree Traversal Methods of visiting (pracessing) each nade in the tree exactly one time Methods of traversal: 1. Breadth First 2, Depth First a, Preorder traversal b. Postorder traversal ©. Inorder traversal (for binary trees only) S2|PagePreOrder Traversal of a General Tree Traverse a tree in node-left-right sequence Figure 28: Preorder traversal of an ordered tree Paper, Title, Abstract, $ 1,$1.1,$1.2, $2, $2.1, $2.2, $2.3, $3, $3.1, $3.2, References ‘Algorithm for PreOrder Traversal: Procedure perOrder(raot) if(root {s not null) process(root) predrder(leftSubtree) preorder(rightSubtree) End if End Procedure Processing order: Figure 29: Processing Order in PreOrder Teaversat S3|PagePostOrder Traversal ofa General Tree Traverse a tree in left-right-node sequence Figure 30: Postorder traversal of the ordered tree Title, Abstract, $1.4, $ 1.2, $ 1, $2.1,$ 2.2, $ 2.3, $2,$3.1,$ 3.2, $3, Reference Algorithm for PostOrder Traversal: Procedure PostOrder (root) if(root is not null) PostOrder (leftsubtree) PostOrder (rightsubtree) process(root) End if End Procedure Processing order: Figure 31: Processing Order in PostOrdee Traversal S4|PageInOrder Traversal of a Binary Tree Traverse a tree in left-node-right- sequence Figore 32: inorder traversalota binary tree (2+ 1)x3)/(9=5)+2) = (3x (7-4) #6) Algorithms for indrder Traversal: Procedure inOrder (root) ‘if(root is not null) inOrder (LeftSubtree) process(roct) inOrder (rightsubtree) End i End Procedure Processing order ‘Figure 31; Processing Order in inGrder Traversal S5|PageEuler Tour ‘We can unify the tree traversal algorithms into a single framework known as a Euler tour traversal. The Euler tour traversal of a tree T can be informally defined as a walk round T, where we start by going from roat forward its left mast child, viewing the edge of T as being walls that we always keep to our left. Figure 34: Euler Tour Traversal Lets us consider an example; ‘igne 35: PreOrder Traversal A,B,C, Figure 36: RostOrder Travertal¢,,8, Figure 37:taOrier Traversale,8,0,A,6, F nF REA S6|PageBreadth First Visit all the positions at depth d before visit the positions at depth d+1. This is a level by level approach from layer 0 {root layer) to upwards, ‘Figure 3 Geeadth frst traversal ofthe ordered tree Paper, Title, Abstract, $ 1, $ 2, $ 3, References, $ 1.1,$12,$2.41,$2.2,$2.8,$3.1, $3.2 S7|Page‘Adata structure, in which a record is linked to two successor records, could be either a ADT ora data structure ‘Tynes of Binary trees: ‘* Full binary tree (proper + Complete binary tree © Balanced binary tree ry tree or 2 tree) Full binary tree: Abinary tree in which every node other than leaves has two children Fiore 39: Afullbinary tree ‘note leaves are in blue color. Complete binary tree ‘Abinary tree in which every level, except possibly the last, is completely filled and all nodes are as far left as possible. ie. all nodes of the last layer should be filled from left ta right Figura 40:4 compote binary tree ‘note last level is in blue colour. 5B|PageExamples: Neither compiete nor full Complete but not full Full but not complete Complete and full Exercise: Udentify whether the following Binary tree is a full ree, a complete tree or a full and complete tree or mane af above mentioned. ‘Answer: this is nat full, not complete, therefore none of above mentioned types Balanced Binary tree Height of the left and the right sub tress cen vary by ane level at most (maximum) Vas Figure 41: fo} non-balanced, (6 batanced S9|PageImplementation of Binary tree ADT Arrays based implementation and linked lists based implementation is discussed here. Methads (operations) of a Binary Tree: = addRoot(vaive) © addLeft{position, value) ‘+ addRight{position, value) ‘© set(position, value) © replace existing element of the given position ‘+ attach{positien, 71, T2) © Attaches the trees of T1 and 72 a5 left and right sub trees of leaf position p © remove(position): value © find(value): node Array based implementation For every position p of treeT, let f(p) be the integer defined as Fallows * If pis the root of T, then flp) =0 ‘© If pisthe left child of position g, then ffp)= 2f(q)+1, ‘+ fp isthe right child of position g, then f{p) = 2F(q)+2 ‘The numbering function f is known as a level numbering of the position ina binary tree T, for it numbers the positions on each level of Tin increasing order from left to right. ‘note Level numbering is based on potential positions within a tree, not actual shape of a specific tree, ‘so they are nat necessarily consecutive, 60] PaceExample: OrzzeeS GTS Oo MND ‘note The space usage of an array based representation depends greatly an the shape of the tree. Linked List based implementation Anatural way to realize @ binary tree T is to tse a linked structure, with a node that maintains: ‘references to the elements stored at a position pand to the nodes associated with the children’and ‘parent of p. If pis the root element, then the parent node reference is null. IF pis leaf node, both children references are null, co) Figure 43: (a) Node structure (0) Example Binary tree “nate GL|Pagerout variable is a Node reference that keeps reference for the root node of the tree, size variable is a int variable that keeps the track of the total number of nodes in the tree. Node class of the linked structure used to implement the Binary tree ADT class Nodect>( E elenents Nodece> parent; NodexE> leftchild; WodesE> rightehild; t ‘note Sometimes in the Node class the reference to the parent node is absent. class Nodece> E elonent; WadesE> leftChild; Nadece> eightchild; Binary Search Tree (BST): ABST isa binary tree in symmetric order. ‘Symmetric Order: '* Each node contains one key (also known as data). ‘© The keys in the left sub tree are less than the key in its parent node. © The keys in the right sub tree are greater than the key in its parent node. © Duplicate keys are not allowed. ‘ABinary Search Tree is where each node has a comparable key and an associated value and satisfies the restriction that the key in any node is larger than the keys in all the nodes in that node’s left sub tree and smaller than the keys in all nodes in that nade’s right sub tree. ‘Other names for BST: © Ordered tree © Sorted binary tree ‘A node in a 8ST comprised of four fields: = key © value © left subtree © right sub tree 62/PageNode Class of a Binary Search Tree: class NodecKey extends ComparablecKey> ,Valuc>{ Key key; H/sonetines a tree do not have a value field Value val; Wade leftChild; Node rightchild; Nade(Key key, Value val){ this.key = key fate —— i lett right ST with larger keys Figura 44; Binary Search tree Binary Search: If less, go left; If greater, go right; equal, search hit Binary Insert: If less, go lefts Mtgreater, go cee Deen pope) Figure 45: Order of growth of the running time For Ordered linked list and w binary tree if null, insert a new node, else if the key already exists reset existing value €3|PageBinary Search Tree Implementation using a Linked Structure //This is a Amplementation of Map ADT using a 8ST //T say this is a mop since there are entries with a key and a value in each entry. public class BST
{ private Node root; private class Node
{ Integer key; Mikay is always a integer value in this example Value vals Node leftchild; Node rightchild; Node(Integer key, Value val){ this.key = keys this.val = val , public void put(Integer key, Value val){ rest = put{reet, key, val); + private Node put(Node x, Integer key, Value val)}{ ‘1F(xe=nul) return neu Node(key, val); Af(key
x.key) w= werightChildy else return (Value) x.vals —//soareh nit } return null Jfonsuccessful search y public void delete(Integer key){ /ythis 1s some what complex, you can try it later } e4|Pagey “note In the above implementation of binary insertian, many BSTs correspond ta same set of keys depending ‘on the order that we insert. i we insert keys according to an order, that leads toa warst case. Number of compares for search/insert is equal to 1+ depth af node. Figure 46: Diferost BSF for same set of keys public Node get(int p, ant k)( if (pc) return pj //unsuccessful search else if(ke= key(p)) return pz //successful search else if (ks key(p)) //recur on left sub tree return get(left(ny, k); else //recur on right sub tree if kokey(p) return get(right(p), k); ? tote ‘The above cade segment is. recursive function for binary search, Binary Deletion: © There are three cases that we have to pay attention. © (Case 1: Deleting alesf node Set nullthe pointer to that node © Case2: Deleting a node having only one child Lethe single child replace the parent © Case: Deleting anodic has two children © Find the leftmost node of its right sub tree (inOrder success: © Let successor replaces node to be deleted * Or find the right most node of the left sub tree (inOrder predecessor) * Letpredecessar replaces nade tobe deleted 65 /PageAVL (Adelson, Velski & Landis) Trees ABST where the height of the left and right sub trees of each node differ by at most 1. Balance factor = height(lett sub tree) ~ height(right sub tree) ‘Accepted balance factors for a BST to become a AVL tree are -1, 0 and 1. ‘note ‘What happens of input to binary search tree comes in sorted ascending order and descending manner? Figure 47: Balanced not balanced ot balanced In second tree, the left sub tree of C has height 2 and right sub tree has height 0, so the difference is. In third tree, the right sub tree of A has height 2 and left is missing, so itis O, and the difference is 2 again. AVL tree permits difference balance factor to be only 1. ‘Ta make itself balanced, an AVL tree may perform 4 kinds of ratations: 1. Left rotation 2. Right rotation 3. Loft— Right rotation 4, t— Left rotation: Refers http://www.tutorialspoint.com/data structures algorithms/avi tree algorithm.hm Figure 48: Let rotation 66|Page‘igure $0: Example fora AVL tree However there are many different varieties if trees and Binary trees. Ex: tries, red black trees, etc 67 [PageLesson 08: Maps ‘An ADT designed to store and retrieve values based upon a uniquely identifying search key for each. Key value pairs of {k,w) are called entries where k for Key and for Value of the entry. Keys are required to be unique. Maps are known as associative arrays, because the key serves somewhat like an index into the map, in ‘that it assists the map in efficiently locating the associated entry. Unlike a standard array, a key of a map need not to be numeric, and it does not directly designates a position within the structure. ‘Comman applications of maps: ‘= Auniversity’s information system relies on some form of a student ID as key that is mapped to that student's associated recard serving as the value, ‘+ The Domain-Name System (DNS) maps.a host name to an IP address. ‘+ Asocial media site typically relies on a (nonnumeric) username as.a key that can be efficiently mapped to a particular user's associated information, Methods (operations) of a Map: size(}integer = istmpty(}:Baolean + got(key:data_type_of koy}idata_type_of value ‘© putlkey:data_type_of_key, value: data_type_of_value):data_type_of_value © return the existed value if there already an entry existing from that key = remove(keyidata_type_of_key) + keySett):data_type_of_keyl] © Returns an iterable callection containing all the keys stored © Valuest):data_type_of_value(] © Returns an iterable callection containing all the values of entries stored © entrySet():all key-value entries © Returns an iterable collection containing all the key-value ent in Map In java, we can use java.util. Map interface to implement a map. 68) PageExercise: Assume thot initially Deque is empty. Method ‘isEmptyl) (SAL. i C)A8,0)) ut2.E) c [(5.91.42,8),2.61,8,0)) | get(?) {(5,A).7,8),(2,£) (8,0)) get(4) {(5,A)(7,8).(2,€) (8,0) get(2) {(5,A)A7,8)42,£).(8,0)) sizel) {5.A)A7.8).(2,€),(8,0)) {(7,8).(2,E).(8,0)) {(7.8)48.0)) 4(7,8)48,0)) comovel2) null ((7.8).(8.0)) isEmpty) false {(7,8),(8,0)) centrySet() {(7,8)18,0)) {(7,8)(8,0}} ‘keySet() {7.8} {(7,8)48,0)) ‘valuesi) {B,D} {7,848.00} Map implementation //Map interface map. java public interface MapsK,V> { int size(); boolean istmpty(); V get(K key) V put(k key, V value); V remave(k key); Iterableck> keySet(); Iterableev> values(); Iterablecentryck,V>> entrySet(); , Implementation of the above Map interface could be done in many ways Array (ex: ArrayList in java) Linked List ast Hash Table €9[Page‘The following code segment shows the structure of » entry(nede) in linked list implementation af the Map class Napentry«k,¥V5{ Mfeiwivalent to Node class keys V value; Mapentry next; Mapentry prev Hash Tables ‘Gne-of the most efficient data structures for implementing map ‘A map M supports the abstraction of using keys as addresses that help focate an entry. As 3 mental ‘warm-up, consider 2 restricted setting in which a map with n entries uses keys that are known to be ‘integers in 3 range from 0 to N-1. in this case we can represent the map using a lookup table of length N. o ot 2 3 4 5 6 F 8 9 0 Fizare $1: lookep tabla with length 31 fora mop containing entries (1,0), (3.2].(6.) ond (7.0 Basic Map operations get, put, and remove can be implemented in O(1) worst-case time, ‘The challenges in extending this framework to the more general setting of a map: ‘+ fnew keys that are not in the range (ex: 11) appear to put into the map, the lookup table should be lengthen ‘+ Unnecossarity allocating space * Only integer values can be used as keys: ‘The solution is the use of a Hash function to map general keys to corresponding indices in a table. “The keys will be distributed in the range from 0 £6 Nel by a hash function, but in practice there may be ‘two or more distinct keys that get mapped to the came intiex. So we declare our table as a Bucket array. Exercise: Assume that yau have following entries to enter ino hash table with 11 buckets indexed fram Oca 10. (4,0), (25,C), (3.1, (14,2), (6-4), (39,C) and (7,Q) Use the following hash function Hlkey) = key % 1 TW|PageFigure 52: A bucket areay of eapacity 18 with enteles (1,0), (25,C), (3, (14,2), (6A), (29, C)nd (7,0) using &slple hash function Hash function is used to decide which bucket a key, value pair should be placed. The most simple hash function is the modulus, In the above exercise, ‘Hikey) = key 6.11 isthe hash function, Here 11 is the number of buckets in the bucket array. Therefare the output of any key value through hash function lies between 0 and 10. Collision: ‘© Collisions occur if you have more than one item in 2 bucket, If you have perfect hash function, there should bea very less number of collisions. '* The problem is how you are going ta distribute entries within the range of indices of the bucket array. Hash function consists of two parts 1. Hash code 2. Compression function “The advantage of separating hash function into two such components is that the hash code portion of ‘that computation is independent of a specific hash table size. This allows the development of a general hash code for each abject that can beused for a hash table of any size; only the compression function depends upon the table size TAlPage= & & Cut s Crea Tg (yetae! ‘Figure $3:-Two parts of ahash function T2[PageLesson 09: Sorting Algorithms ‘Sorting means to put elements of a list in a certain order. ‘There are 5 popular algorithms for sorting: © Simple sorting 1. Bubble sort 2, Selection sort 3. Insertion sort * Advance sorting: 4. Mergesort 5. Quick sort ‘Classification of sorting: 4. Internal sorting © The.amount of data to be sorted is sufficient to process and the process is carried out in ‘the computer RAM. 2. External sorting © The amount of data to be sorted is too much and pracess can't carry out in the ‘computer RAM at once. © Therefore data are stored on a secondary storage device. TalPageBubble Sort (Exchange sort) Reference: Lecture Slides Bubble search focuses on successive adjacent pairs of elements in the list, compares them, and either ‘swaps them or not. (51428)->(15428)->(14528)->(14258)->(14258) (14259)>(14258)>(12458)->(12458) (12458)->(12458)>(12458) (12458)->(12458) (12458) Implementation of bubble sort public class Test ¢ static int[] bubblesort(int{] array) for (int i = Q; i ¢ array.length-1; its) { for (int j= 95 4 < array.length-L-is j++) { Af (array{j array jo2})( int temp = array[3]; array{3] = arrayljei array[J#1] = temp; } return arrays } public static void main(String[] ares) { Ane[] array = new dmt(]{2,2,2,4,3,29,8,2}5 array © bubblesort¢array); for (int i = Q; i 2 array-lengths 444) { System. out .printin(array[i]}3 d TAlPageSelection Sort ‘The basic operation of the selection sorts to identify the smallest element fram the sequence of ‘elements. ‘As the first step scan the elements from the beginning to end and find the smallest and swap it with the: first one etc. (51428)>(15428) (15428)->(12458) (12458)>(12458) (12458)-5(12458) (12458) ‘Smallest value is 1, swap Land's Next smallest value is 2, swap 2 and 5 Next smallest value is 4, no change Next smaliest value is 5, no change Done Implementation of insertion sort public class Test ( static int[] selectionsort(int{] array) ( for (int i = Q; i ¢ array.length-1; i++){ int ninIndex = for(int j= ist; jcarray. lenge! jee af(array[wintndex]earrayl3)){ minindex = J; t int temp = array[i]; arrayfi] = arrayfmintndex); array[minindex] = temp; Bi return orray; t public static void main(string{] args) ( int() array = new int(}(1,2,3,4,3,29,8,2)5 array = setectionsore (array); for (int i = @; i ¢ array-length; i++) { System.out.printIn(array[1])3 } TS|PageInsertion Sort ‘The basie operation is insertion of a single element into a sequence of sorted elements. Generally, suppose that we have already sarted first i-1 number of elements. Then we take i® element and sean through the sorted list to see where to insert item i (51428)-5(15428) —_ FicS and take Land see where to put. Putit. (15428)>(14528) —Fin1,S.and take and see where to put, Put it (14528)>(12458) Fix 14,5 and take 2 and see where to put, Put it. (12458)5(12458 Fic 1,2.4,5 and take 8 and see where to put. Put it. (12asay Done Implementation of insertion sort public class Test ¢ static int[] insertianSort(int{] array){ for (int £ = 3; 1 ¢ array.lengeny i4+){ int j=i-33 Ant nusber = array{ils whi Le(nunbercarray[j HL array{j+4] = arrayfj}s a array( Jel] = number; + return array; ? public static void main(stringl] args) ( int{] array = new int(](1,2,3,4,3,29,8,2)5 array = insere{onsore (array: for (int i = 8; i < array.lengths i++) { System. out .printin(array[1])3 ? /feest case if array is already in ascending order, J/worst case if the array is in descending order Te|PageClassic Sorting Algorithms (Advanced Sorting Algorithms) Critical components in the world’s computational infrastructure: ‘© Fall scientific understanding of their properties has enabled us to develop them into practical system sorts. '* Quicksort honored as one of the top 10 algorithms of 20 century in Science and engineering. Classie sorting algo: © Shellsort © Embedded systems + Mergesort © Java sort for objects © Quicksort (© Java sort for primitive types Divide and Conquer Merge sort and quick sort use recursion in an algorithmic design pattern called divide and conquer. ‘Three steps of divide and conquer pattern |. Divide + Ifthe input size is smaller than a certain threshold (one or two elements}, solve the problem directly using a straightforward method and return the solution so obtained. ‘Otherwise, divide the input data into twa or mare disjoint subsets. 2. Conquer * Recursively solve the subprablems associated with the subsets. 3. Combine * Take the solution to the subproblems and merge them into a solution to the original problem Merge-Sort ‘We can visualize an execution of the merge-sort algorithm by means of a binary tree T, called merge- ‘sort tree. Each mode of T represents a recursive invocation (or call) of the merge-sort algorithm. ‘The following two figures summarizes the execution of the merge-sort algorithm for unsorted array of (85, 24, 63, 45, 17, 31, 96, 50). ‘The array is divided until it become a single element. ‘Then it is sorted backwards and keep on merging. Trl PageImplementation of Merge-sort ‘When talk about the implementation of the meige-sort algorithm, we have to use separate functions for ‘merging and sarting. TalPagepublic class Test { private static void merge(Integer{] array, Integer[] aux,int 1o,int mid,int hi){ for(int k = lo; ke=hi; kre) aux[k] = array[k]; int i = lo; int j= mid +4; forint k = lo; kenhi; kee){ AF(ionid) array[k] = aux[jee]; else Lf(j9hi) acrayfk] = oux{ite]: else {f(aux[j}eaux(1}) array(k} = aux[jee]i else array[k] = aux[it+]s t private static void sort(Integer{] array,Integer(] aux, int lo, int hipf AF (hix=to) return; int mid = (lo + hi/2; sort(array,avX, 10,mid); sort (array,aux,miGet hi); merge(array,aux,10,e\d, i); , static Integar[] mergesort(Inteper(] array) Integer{] aux = new Integer[array. length]; -sore(anray ,aux,@, array. Length-1); return aux} ? public static void main(string{} args) ( Integer[] array = new Integer(]{4,2,3,4,3,29,8,235 array = mergesort (array); for (int i = @; is array-Jengths i++) { System. out .printin(array[i])3 } T9|Page
You might also like
Data Structures Notes 2 - TutorialsDuniya
PDF
100% (1)
Data Structures Notes 2 - TutorialsDuniya
138 pages
Dsa Codes Using Python
PDF
No ratings yet
Dsa Codes Using Python
131 pages
DS Unit 1
PDF
No ratings yet
DS Unit 1
135 pages
Data Structures For First Year
PDF
No ratings yet
Data Structures For First Year
23 pages
Chen
PDF
No ratings yet
Chen
132 pages
Data Structures - SCSA1203: Unit - I
PDF
No ratings yet
Data Structures - SCSA1203: Unit - I
27 pages
SCSA1205
PDF
No ratings yet
SCSA1205
142 pages
Data Structures and Algorithms
PDF
No ratings yet
Data Structures and Algorithms
304 pages
Data Structures and Algorithm
PDF
No ratings yet
Data Structures and Algorithm
159 pages
Iare DS Lecture Notes 2
PDF
No ratings yet
Iare DS Lecture Notes 2
135 pages
Abstract Data Type Is A Definition of New Type, Describes Its Data Structure Is An Implementation of ADT. Many ADT
PDF
No ratings yet
Abstract Data Type Is A Definition of New Type, Describes Its Data Structure Is An Implementation of ADT. Many ADT
12 pages
Notes Dsa
PDF
No ratings yet
Notes Dsa
159 pages
Data-Structures-And-Algorithm Notes Edited
PDF
No ratings yet
Data-Structures-And-Algorithm Notes Edited
159 pages
Data Structure Lecture Notes-1-8
PDF
No ratings yet
Data Structure Lecture Notes-1-8
8 pages
Basic Concepts: Introduction To Data Structures
PDF
No ratings yet
Basic Concepts: Introduction To Data Structures
84 pages
DS Unit1 Merged
PDF
No ratings yet
DS Unit1 Merged
166 pages
Dsa Notes
PDF
No ratings yet
Dsa Notes
159 pages
DSA Unit 1 - Merged
PDF
No ratings yet
DSA Unit 1 - Merged
98 pages
DS Unit 1
PDF
No ratings yet
DS Unit 1
50 pages
DS Notes
PDF
No ratings yet
DS Notes
118 pages
EE36-Data Structure and Algorithms Ii Eee
PDF
No ratings yet
EE36-Data Structure and Algorithms Ii Eee
159 pages
0.introduction To DS - Sorting - Searching
PDF
No ratings yet
0.introduction To DS - Sorting - Searching
79 pages
DSA - Module 1 - Part 1
PDF
No ratings yet
DSA - Module 1 - Part 1
39 pages
Notes - DS Using C++ Sem IV CBCS - Opt
PDF
No ratings yet
Notes - DS Using C++ Sem IV CBCS - Opt
49 pages
CS312 - ATBU Lecture Note 7 3-03-2025 Data Structure
PDF
No ratings yet
CS312 - ATBU Lecture Note 7 3-03-2025 Data Structure
52 pages
DS Unit - 1
PDF
No ratings yet
DS Unit - 1
47 pages
Data Structure Lecture 1
PDF
No ratings yet
Data Structure Lecture 1
39 pages
Introduction and Analysis of An Algorithm
PDF
No ratings yet
Introduction and Analysis of An Algorithm
38 pages
Introduction
PDF
No ratings yet
Introduction
41 pages
Unit 1 - Data Structures
PDF
No ratings yet
Unit 1 - Data Structures
31 pages
Chapter 1 - Concept of Data Type
PDF
No ratings yet
Chapter 1 - Concept of Data Type
36 pages
DS Unit1
PDF
No ratings yet
DS Unit1
41 pages
Intro - To - Data Structure - Lec - 1
PDF
No ratings yet
Intro - To - Data Structure - Lec - 1
29 pages
Data Structures and Algorithms PDF
PDF
No ratings yet
Data Structures and Algorithms PDF
32 pages
II CSM B DS Unit-1
PDF
No ratings yet
II CSM B DS Unit-1
31 pages
ch#1
PDF
No ratings yet
ch#1
37 pages
Ds Unit 1
PDF
No ratings yet
Ds Unit 1
27 pages
U.I CS8391 Data Structures
PDF
No ratings yet
U.I CS8391 Data Structures
41 pages
Week 01 - Introdutcion To Data Structures and ADT
PDF
No ratings yet
Week 01 - Introdutcion To Data Structures and ADT
24 pages
Data Structure and Algorithms Lecture 1
PDF
No ratings yet
Data Structure and Algorithms Lecture 1
20 pages
Week 01 - Lecture 1 2 Introdutcion To Data Structures and ADT
PDF
No ratings yet
Week 01 - Lecture 1 2 Introdutcion To Data Structures and ADT
29 pages
Unit 1 Data Structure
PDF
No ratings yet
Unit 1 Data Structure
21 pages
Data Structure and Algorithms Lecture 1
PDF
No ratings yet
Data Structure and Algorithms Lecture 1
20 pages
Data Structures and Algorithms: Computer Science Department
PDF
No ratings yet
Data Structures and Algorithms: Computer Science Department
19 pages
Data Structure Basics
PDF
No ratings yet
Data Structure Basics
13 pages
Class2 DSA ADT Array Sorting 05jan2023
PDF
No ratings yet
Class2 DSA ADT Array Sorting 05jan2023
15 pages
Unit One
PDF
No ratings yet
Unit One
14 pages
Data Structure and Algorithms Lecture 1
PDF
No ratings yet
Data Structure and Algorithms Lecture 1
17 pages
Data Structure and Algorithms Lecture 1
PDF
No ratings yet
Data Structure and Algorithms Lecture 1
17 pages
CSE225 Lecture01 Introduction To DS
PDF
No ratings yet
CSE225 Lecture01 Introduction To DS
20 pages
CSC2203 Algorithms Data Structure Lecture Notes 1
PDF
No ratings yet
CSC2203 Algorithms Data Structure Lecture Notes 1
16 pages
CSE 2001 - Data Structures and Algorithms
PDF
No ratings yet
CSE 2001 - Data Structures and Algorithms
15 pages
1 Data Structure and Algorithms Lecture 1
PDF
No ratings yet
1 Data Structure and Algorithms Lecture 1
20 pages
A2 Basic Concept
PDF
No ratings yet
A2 Basic Concept
14 pages
Unit 1 Data Structure
PDF
No ratings yet
Unit 1 Data Structure
15 pages
BSC Unit 1
PDF
No ratings yet
BSC Unit 1
10 pages
Introduction To Data Structure
PDF
No ratings yet
Introduction To Data Structure
5 pages
Data Structures Tutorial 1
PDF
No ratings yet
Data Structures Tutorial 1
4 pages
EE36 Data Structures and Algorithm
PDF
No ratings yet
EE36 Data Structures and Algorithm
0 pages