chapter 16 data structures 1 ©2000, john wiley & sons, inc. horstmann/java essentials, 2/e...
TRANSCRIPT
©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e
Chapter 16 Data Structures1
Chapter 16
An Introduction to Data Structures
©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e
Chapter 16 Data Structures2
Figure 1Inserting an Element Into aLinked List
©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e
Chapter 16 Data Structures3
Figure 2A List Iterator
©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e
Chapter 16 Data Structures4
Figure 3A Conceptual Viewof the List Iterator
©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e
Chapter 16 Data Structures5
Program ListTest1.java
import java.util.LinkedList;import java.util.ListIterator;
public class ListTest1{ public static void main(String[] args) { LinkedList staff = new LinkedList(); staff.addLast("Dick"); staff.addLast("Harry"); staff.addLast("Romeo"); staff.addLast("Tom");
// | in the comments indicates the iterator position
ListIterator iterator = staff.listIterator(); // |DHRT iterator.next(); // D|HRT iterator.next(); // DH|RT
©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e
Chapter 16 Data Structures6
// add more elements after second element
iterator.add("Juliet"); // DHJ|RTiterator.add("Nina"); // DHJN|RT
iterator.next(); // DHJNR|T// remove last traversed element
iterator.remove(); // DHJN|T
// print all elements
iterator = staff.listIterator(); while (iterator.hasNext()) System.out.println(iterator.next()); }}
©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e
Chapter 16 Data Structures7
Figure 4Adding a Linkto the Head of aLinked List
©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e
Chapter 16 Data Structures8
Figure 5Removing a Link from the Head of aLinked List
©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e
Chapter 16 Data Structures9
Figure 6Removing a Link from the Middle ofa Linked List
©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e
Chapter 16 Data Structures10
Figure 7Adding a Link to the Middle of aLinked List
©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e
Chapter 16 Data Structures11
Program ListTest2.java
import java.util.NoSuchElementException;
public class ListTest2{ public static void main(String[] args)
{ LinkedList staff = new LinkedList(); staff.addFirst("Tom"); staff.addFirst("Romeo"); staff.addFirst("Harry"); staff.addFirst("Dick"); // | in the comments indicates the iterator position LinkedList.Iterator iterator = staff.listIterator(); // |DHRT iterator.next(); // D|HRT iterator.next(); // DH|RT // add more elements after second element iterator.add("Juliet"); // DHJ|RT iterator.add("Nina"); // DHJN|RT
©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e
Chapter 16 Data Structures12
iterator.next(); // DHJNR|T // remove last traversed element
iterator.remove(); // DHJN|T // print all elements
iterator = staff.listIterator(); while (iterator.hasNext()) System.out.println(iterator.next()); }}
/** A linked list is a sequence of links with efficient element insertion and removal.*/
©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e
Chapter 16 Data Structures13
class LinkedList{ /** Constructs an empty linked list. */ public LinkedList() { first = null; }
/** Returns the first element in the linked list. @return the first element in the linked list */
public Object getFirst() { if (first == null) throw new NoSuchElementException(); return first.data; }
©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e
Chapter 16 Data Structures14
/** Removes the first element in the linked list. @return the removed element*/public Object removeFirst(){ if (first == null) throw new NoSuchElementException(); Object obj = first.data; first = first.next; return obj;}
/** Adds an element to the front of the linked list. @param obj the object to add*/
©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e
Chapter 16 Data Structures15
public void addFirst(Object obj){ Link newLink = new Link(); newLink.data = obj; newLink.next = first; first = newLink;}
/** Returns an iterator for iterating through this list. @return an iterator for iterating through this list*/public Iterator listIterator(){ return new Iterator();}
private Link first;
©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e
Chapter 16 Data Structures16
private class Link{ public Object data; public Link next;}
public class Iterator{ /** Constructs an iterator that points to the front of the linked list. */
public Iterator(){ position = null; previous = null;}/** Moves the iterator past the next element. @return the traversed element*/
©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e
Chapter 16 Data Structures17
public Object next(){ if (position == null) { position = first; return getFirst(); } else { if (position.next == null) throw new NoSuchElementException(); previous = position; // remember for remove position = position.next; return position.data; }}/** Tests whether there is an element after the iterator position. @return true if there is an element after the iterator position*/
©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e
Chapter 16 Data Structures18
public boolean hasNext(){ if (position == null) return first != null; else return position.next != null;}
/** Adds an element before the iterator position and moves the iterator past the inserted element. @param obj the object to add*/public void add(Object obj){ if (position == null) addFirst(obj); else { Link newLink = new Link(); newLink.data = obj; newLink.next = position.next;
©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e
Chapter 16 Data Structures19
position.next = newLink; position = newLink; previous = null; }}
/** Removes the last traversed element. This method may be called only after a call to the next() method.*/public void remove(){ if (position == first) removeFirst();
©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e
Chapter 16 Data Structures20
else { if (previous == null) throw new IllegalStateException(); previous.next = position.next; position = previous; } previous = null; } private Link position; private Link previous; }}
©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e
Chapter 16 Data Structures21
Figure 8A Binary Search Tree
©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e
Chapter 16 Data Structures22
Figure 9A Binary Tree That Is Not a BinarySearch Tree
©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e
Chapter 16 Data Structures23
Figure 10Binary Search Tree after FourInsertions
©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e
Chapter 16 Data Structures24
Figure 11Binary Search Tree after FiveInsertions
©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e
Chapter 16 Data Structures25
Program TreeTest.java
public class TreeTest{ public static void main(String[] args) { Tree staff = new Tree(); staff.insert("Romeo"); staff.insert("Juliet"); staff.insert("Tom"); staff.insert("Dick"); staff.insert("Harry");
staff.print(); }}
/** This class implements a binary search tree whose nodes hold objects that implement the Comparable interface.*/
©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e
Chapter 16 Data Structures26
class Tree{ /** Constructs an empty tree. */ public Tree() { root = null; }
/** Inserts a new node into the tree. @param obj the object to insert */ public void insert(Comparable obj) { Node newNode = new Node(); newNode.data = obj; newNode.left = null; newNode.right = null; if (root == null) root = newNode; else root.insertNode(newNode); }
©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e
Chapter 16 Data Structures27
/** Prints the contents of the tree in sorted order.*/public void print(){ if (root != null) root.printNodes();}
private Node root;
©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e
Chapter 16 Data Structures28
private class Node{ /** Inserts a new node as a descendant of this node. @param newNode the node to insert */ public void insertNode(Node newNode) { if (newNode.data.compareTo(data) < 0) { if (left == null) left = newNode; else left.insertNode(newNode); } else { if (right == null) right = newNode; else right.insertNode(newNode); } }
©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e
Chapter 16 Data Structures29
/** Prints this node and all of its descendants in sorted order. */ public void printNodes() { if (left != null) left.printNodes(); System.out.println(data); if (right != null) right.printNodes(); }
public Comparable data; public Node left; public Node right; }}
©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e
Chapter 16 Data Structures30
Figure 12An Unbalanced Bi-narySearch Tree