Data Structures and Abstractions with Java 5 th
Data Structures and Abstractions with Java™ 5 th Edition Chapter 17 Sorted Lists Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
List • Entries in a list are ordered simply by positions within list • Can add a sort operation to the ADT list • Add an entry to, remove an entry from sorted list – Provide only the entry. – No specification where entry belongs or exists Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Specifications for ADT Sorted List • DATA – A collection of objects in sorted order and having the same data type – The number of objects in the collection • Operations requiring special consideration – add(new. Entry) – remove(an. Entry) – get. Position(an. Entry) Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Specifications for ADT Sorted List • Additional Operations – These behave as they do for the ADT List ▪ get. Entry(given. Position) ▪ contains(an. Entry) ▪ remove(given. Position) ▪ clear() ▪ get. Length() ▪ is. Empty() ▪ to. Array() Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Specifications for ADT Sorted List (Part 1) /** An interface for the ADT sorted list. Entries in the list have positions that begin with 1. */ public interface Sorted. List. Interface<T extends Comparable<? super T>> { /** Adds a new entry to this sorted list in its proper order. The list's size is increased by 1. @param new. Entry The object to be added as a new entry. */ public void add(T new. Entry); /** Removes the first or only occurrence of a specified entry from this sorted list. @param an. Entry The object to be removed. @return True if an. Entry was located and removed; otherwise returns false. */ public boolean remove(T an. Entry); /** Gets the position of an entry in this sorted list. @param an. Entry The object to be found. @return The position of the first or only occurrence of an. Entry if it occurs in the list; otherwise returns the position where an. Entry would occur in the list, but as a negative integer. */ public int get. Position(T an. Entry); LISTING 17 -1 The interface Sorted. List. Interface Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Specifications for ADT Sorted List (Part 2) // The following methods are described in Segment 10. 9 of Chapter 10 // as part of the ADT list: public T get. Entry(int given. Position); public boolean contains(T an. Entry); public T remove(int given. Position); public void clear(); public int get. Length(); public boolean is. Empty(); public T[] to. Array(); } // end Sorted. List. Interface LISTING 17 -1 The interface Sorted. List. Interface Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Linked Sorted List Implementation /** A class that implements the ADT sorted list by using a chain of linked nodes. Duplicate entries are allowed. */ public class Linked. Sorted. List<T extends Comparable<? super T>> implements Sorted. List. Interface<T> { private Node first. Node; // Reference to first node of chain private int number. Of. Entries; public Linked. Sorted. List() { first. Node = null; number. Of. Entries = 0; } // end default constructor /* < Implementations of the sorted list operations go here. >. . . */ private class Node { /* < Implementations of the Node operations go here. > } // end Node } // end Linked. Sorted. List LISTING 17 -2 An outline of a linked implementation of the ADT sorted list Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Linked Sorted List Implementation FIGURE 17 -1 Places to insert additional names into a sorted chain of linked nodes Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Linked Sorted List Implementation Algorithm add(new. Entry) // Adds a new entry to the sorted list. Allocate a new node containing new. Entry Search the chain until either you find a node containing new. Entry or you pass the point where it should be Let node. Before reference the node before the insertion point if (the chain is empty or the new node belongs at the beginning of the chain) Add the new node to the beginning of the chain else Insert the new node after the node referenced by node. Before Increment the length of the sorted list Algorithm for add method. Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Linked Sorted List Implementation public void add(T new. Entry) { Node new. Node = new Node(new. Entry); Node node. Before = get. Node. Before(new. Entry); if (is. Empty() || (node. Before == null)) { // Add at beginning new. Node. set. Next. Node(first. Node); first. Node = new. Node; } else { // Add after node. Before Node node. After = node. Before. get. Next. Node(); new. Node. set. Next. Node(node. After); node. Before. set. Next. Node(new. Node); } // end if number. Of. Entries++; } // end add An iterative implementation of add Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Linked Sorted List Implementation // Finds the node that is before the node that should or does // contain a given entry. // Returns either a reference to the node that is before the node // that contains--or should contain--an. Entry, // or null if no prior node exists // (that is, if an. Entry is or belongs at the beginning of the list). private Node get. Node. Before(T an. Entry) { Node current. Node = first. Node; Node node. Before = null; while ( (current. Node != null) && (an. Entry. compare. To(current. Node. get. Data()) > 0) ) { node. Before = current. Node; current. Node = current. Node. get. Next. Node(); } // end while return node. Before; } // end get. Node. Before The private method get. Node. Before Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Recursive Add to Sorted List FIGURE 17 -2 Recursively adding Luke to a sorted chain of names Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Recursive Add to Sorted List (Part 1) FIGURE 17 -3 Recursively adding a node at the beginning of a chain Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Recursive Add to Sorted List (Part 2) [from previous slide] FIGURE 17 -3 Recursively adding a node at the beginning of a chain Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Linked Sorted List Implementation public void add(T new. Entry) { first. Node = add(new. Entry, first. Node); number. Of. Entries++; } // end add private Node add(T new. Entry, Node current. Node) { if ( (current. Node == null) || (new. Entry. compare. To(current. Node. get. Data()) <= 0) ) { current. Node = new Node(new. Entry, current. Node); } else { Node node. After = add(new. Entry, current. Node. get. Next. Node()); current. Node. set. Next. Node(node. After); } // end if return current. Node; } // end add A recursive implementation of add Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Recursive Add to Sorted List (Part 1) FIGURE 17 -4 Recursively adding a node between existing nodes in a chain Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Recursive Add to Sorted List (Part 2) [from previous slide] FIGURE 17 -4 Recursively adding a node between existing nodes in a chain Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Recursive Add to Sorted List (Part 2) [from previous slide] FIGURE 17 -4 Recursively adding a node between existing nodes in a chain Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Comparison of Implementations Operation Array Linked add(new. Entry) O(n) remove(an. Entry) O(n) get. Position(an. Entry) O(n) get. Entry(given. Position) O(1) O(n) contains(an. Entry) O(n) remove(given. Position) O(n) display() O(n) clear(), get. Length(), is. Empty() O(1) FIGURE 17 -5 The worst-case efficiencies of the operations on the ADT sorted list for two implementations Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Implementation that uses ADT List FIGURE 17 -6 An instance of a sorted list that contains a list of its entries Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Implementation That Uses the ADT List /** A class that implements the ADT sorted list by using an instance of LList. */ public class Sorted. List<T extends Comparable<? super T>> implements Sorted. List. Interface<T> { private List. Interface<T> list; public Sorted. List() { list = new LList<T>(); } // end default constructor //. . . } // end Sorted. List Our class Sorted. List will implement the interface Sorted. List. Interface Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Implementation That Uses the ADT List public void add(T new. Entry) { int new. Position = Math. abs(get. Position(new. Entry)); list. add(new. Position, new. Entry); } // end add The method add. Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Implementation That Uses the ADT List public boolean remove(T an. Entry) { boolean result = false; int position = get. Position(an. Entry); if (position > 0) { list. remove(position); result = true; } // end if return result; } // end remove The method remove. Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Implementation That Uses the ADT List FIGURE 17 -7 A sorted list in which Jamie belongs after Carlos but before Sarah Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Implementation That Uses the ADT List public int get. Position(T an. Entry) { int position = 1; int length = list. get. Length(); // Find position of an. Entry while ( (position <= length) && (an. Entry. compare. To(list. get. Entry(position)) > 0) ) { position++; } // end while // See whether an. Entry is in list if ( (position > length) || (an. Entry. compare. To(list. get. Entry(position)) != 0) ) { position = -position; // an. Entry is not in list } // end if return position; } // end get. Position The implementation of get. Position Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Comparison of Implementations Operation Array Linked add(new. Entry) O(n) remove(an. Entry) O(n) get. Position(an. Entry) O(n) get. Entry(given. Position) O(1) O(n) contains(an. Entry) O(n) remove(given. Position) O(n) display() O(n) clear(), get. Length(), is. Empty() O(1) FIGURE 17 -8 The worst-case efficiencies of the operations on the ADT sorted list for two implementations Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
Comparison of Implementations Operation Array Linked Arrary. List LList add(new. Entry) O(n) O(n 2) remove(an. Entry) O(n) O(n 2) get. Position(an. Entry) O(n) O(n 2) get. Entry(given. Position) O(1) O(n) contains(an. Entry) O(n) remove(given. Position) O(n) display() O(n) clear(), get. Length(), is. Empty() O(1) FIGURE 17 -9 The worst-case efficiencies of the ADT sorted list operations when implemented using an instance of the ADT list Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
End Chapter 17 Copyright © 2019, 2015, 2012 Pearson Education, Inc. All Rights Reserved
- Slides: 28