chapter 16 data structures 1 ©2000, john wiley & sons, inc. horstmann/java essentials, 2/e...

30
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 16 Data Structures 1 Chapter 16 An Introduction to Data Structures

Upload: betty-phillips

Post on 17-Jan-2016

222 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Chapter 16 Data Structures 1 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 16 An Introduction to Data Structures

©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e

Chapter 16 Data Structures1

Chapter 16

An Introduction to Data Structures

Page 2: Chapter 16 Data Structures 1 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 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

Page 3: Chapter 16 Data Structures 1 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 16 An Introduction to Data Structures

©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e

Chapter 16 Data Structures3

Figure 2A List Iterator

Page 4: Chapter 16 Data Structures 1 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 16 An Introduction to Data Structures

©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e

Chapter 16 Data Structures4

Figure 3A Conceptual Viewof the List Iterator

Page 5: Chapter 16 Data Structures 1 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 16 An Introduction to Data Structures

©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

Page 6: Chapter 16 Data Structures 1 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 16 An Introduction to Data Structures

©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()); }}

Page 7: Chapter 16 Data Structures 1 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 16 An Introduction to Data Structures

©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e

Chapter 16 Data Structures7

Figure 4Adding a Linkto the Head of aLinked List

Page 8: Chapter 16 Data Structures 1 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 16 An Introduction to Data Structures

©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e

Chapter 16 Data Structures8

Figure 5Removing a Link from the Head of aLinked List

Page 9: Chapter 16 Data Structures 1 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 16 An Introduction to Data Structures

©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e

Chapter 16 Data Structures9

Figure 6Removing a Link from the Middle ofa Linked List

Page 10: Chapter 16 Data Structures 1 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 16 An Introduction to Data Structures

©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e

Chapter 16 Data Structures10

Figure 7Adding a Link to the Middle of aLinked List

Page 11: Chapter 16 Data Structures 1 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 16 An Introduction to Data Structures

©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

Page 12: Chapter 16 Data Structures 1 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 16 An Introduction to Data Structures

©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.*/

Page 13: Chapter 16 Data Structures 1 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 16 An Introduction to Data Structures

©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; }

Page 14: Chapter 16 Data Structures 1 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 16 An Introduction to Data Structures

©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*/

Page 15: Chapter 16 Data Structures 1 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 16 An Introduction to Data Structures

©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;

Page 16: Chapter 16 Data Structures 1 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 16 An Introduction to Data Structures

©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*/

Page 17: Chapter 16 Data Structures 1 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 16 An Introduction to Data Structures

©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*/

Page 18: Chapter 16 Data Structures 1 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 16 An Introduction to Data Structures

©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;

Page 19: Chapter 16 Data Structures 1 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 16 An Introduction to Data Structures

©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();

Page 20: Chapter 16 Data Structures 1 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 16 An Introduction to Data Structures

©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; }}

Page 21: Chapter 16 Data Structures 1 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 16 An Introduction to Data Structures

©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e

Chapter 16 Data Structures21

Figure 8A Binary Search Tree

Page 22: Chapter 16 Data Structures 1 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 16 An Introduction to Data Structures

©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e

Chapter 16 Data Structures22

Figure 9A Binary Tree That Is Not a BinarySearch Tree

Page 23: Chapter 16 Data Structures 1 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 16 An Introduction to Data Structures

©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e

Chapter 16 Data Structures23

Figure 10Binary Search Tree after FourInsertions

Page 24: Chapter 16 Data Structures 1 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 16 An Introduction to Data Structures

©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e

Chapter 16 Data Structures24

Figure 11Binary Search Tree after FiveInsertions

Page 25: Chapter 16 Data Structures 1 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 16 An Introduction to Data Structures

©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.*/

Page 26: Chapter 16 Data Structures 1 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 16 An Introduction to Data Structures

©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); }

Page 27: Chapter 16 Data Structures 1 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 16 An Introduction to Data Structures

©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;

Page 28: Chapter 16 Data Structures 1 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 16 An Introduction to Data Structures

©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); } }

Page 29: Chapter 16 Data Structures 1 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 16 An Introduction to Data Structures

©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; }}

Page 30: Chapter 16 Data Structures 1 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 16 An Introduction to Data Structures

©2000, John Wiley & Sons, Inc.Horstmann/Java Essentials, 2/e

Chapter 16 Data Structures30

Figure 12An Unbalanced Bi-narySearch Tree