1 / 44

Chapter 5 – Data Structures

Chapter 5 – Data Structures. Outline 5.1 Introduction 5.2 Self-Referential Classes 5.3 Dynamic Memory Allocation 5.4 Linked Lists 5.5 Stacks 5.6 Queues 5.7 Trees. 5.1 Introduction. Dynamic data structures Grow and shrink at execution time Several types Linked lists Stacks

abena
Download Presentation

Chapter 5 – Data Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 5 – Data Structures Outline 5.1 Introduction5.2 Self-Referential Classes5.3 Dynamic Memory Allocation5.4 Linked Lists5.5 Stacks5.6 Queues5.7 Trees

  2. 5.1 Introduction • Dynamic data structures • Grow and shrink at execution time • Several types • Linked lists • Stacks • Queues • Binary trees

  3. 5.2 Self-Referential Classes • Self-referential class • Contains instance variable referring to object of same class class Node {private int data;private Node nextNode; // reference to next linked node} • Member nextNode is a link • nextNode “links” a Node object to another Node object

  4. 5.2 Self-Referential Classes (cont.) 15 10 Fig 5.1 Self-referential-class objects linked together.

  5. 5.3 Dynamic Memory Allocation • Dynamic memory allocation • Obtain more memory at execution time to store new objects • Declaration and class-instance creation expression Node nodeToAdd = new Node ( 10 );

  6. 5.4 Linked Lists • Linked list • Linear collection of self-referential classes (nodes) • Connected by reference links • Nodes can be inserted and deleted anywhere in linked list • Last node is set to null to mark end of list

  7. 5.4 Linked Lists (cont.) firstNode lastNode ... Q H D Fig 5.2 Linked list graphical representation.

  8. Self-referential class ListNode contains data and link to nextNode 1 // Fig. 5.3: List.java 2 // ListNode and List class declarations. 3 package com.deitel.jhtp5.ch20; 4 5 // class to represent one node in a list 6 class ListNode { 7 8 // package access members; List can access these directly 9 Object data; 10 ListNode nextNode; 11 12 // create a ListNode that refers to object 13 ListNode( Object object ) 14 { 15 this( object, null ); 16 } 17 18 // create ListNode that refers to Object and to next ListNode 19 ListNode( Object object, ListNode node ) 20 { 21 data = object; 22 nextNode = node; 23 } 24 25 // return reference to data in node 26 Object getObject() 27 { 28 return data; // return Object in this node 29 } 30 List.javaLines 6-10

  9. Reference to first node in linked list Reference to last node in linked list First and last nodes in empty list are null 31 // return reference to next node in list 32 ListNode getNext() 33 { 34 return nextNode; // get next node 35 } 36 37 } // end class ListNode 38 39 // class List declaration 40 publicclass List { 41 private ListNode firstNode; 42 private ListNode lastNode; 43 private String name; // string like "list" used in printing 44 45 // construct empty List with "list" as the name 46 public List() 47 { 48 this( "list" ); 49 } 50 51 // construct an empty List with a name 52 public List( String listName ) 53 { 54 name = listName; 55 firstNode = lastNode = null; 56 } 57 58 // insert Object at front of List 59 publicsynchronizedvoid insertAtFront( Object insertItem ) 60 { List.javaLine 41Line 42Line 55

  10. If list is empty, the first and last node should refer to the newly inserted node If list is not empty, the first node should refer to the newly inserted node If list is empty, the first and last node should refer to the newly inserted node If list is not empty, the last node should refer to the newly inserted node If list is empty, removing a node causes an exception 61 if ( isEmpty() ) // firstNode and lastNode refer to same object 62 firstNode = lastNode = new ListNode( insertItem ); 63 64 else// firstNode refers to new node 65 firstNode = new ListNode( insertItem, firstNode ); 66 } 67 68 // insert Object at end of List 69 publicsynchronizedvoid insertAtBack( Object insertItem ) 70 { 71 if ( isEmpty() ) // firstNode and lastNode refer to same Object 72 firstNode = lastNode = new ListNode( insertItem ); 73 74 else// lastNode's nextNode refers to new node 75 lastNode = lastNode.nextNode = new ListNode( insertItem ); 76 } 77 78 // remove first node from List 79 publicsynchronized Object removeFromFront() throws EmptyListException 80 { 81 if ( isEmpty() ) // throw exception if List is empty 82 thrownew EmptyListException( name ); 83 84 Object removedItem = firstNode.data; // retrieve data being removed 85 86 // update references firstNode and lastNode 87 if ( firstNode == lastNode ) 88 firstNode = lastNode = null; 89 else 90 firstNode = firstNode.nextNode; List.javaLines 61-62Line 65Lines 71-72Line 74Lines 81-82

  11. If list is empty, removing a node causes an exception If list is not empty, the second-to-last node becomes the last node 91 92 return removedItem; // return removed node data 93 94 } // end method removeFromFront 95 96 // remove last node from List 97 publicsynchronized Object removeFromBack() throws EmptyListException 98 { 99 if ( isEmpty() ) // throw exception if List is empty 100 thrownew EmptyListException( name ); 101 102 Object removedItem = lastNode.data; // retrieve data being removed 103 104 // update references firstNode and lastNode 105 if ( firstNode == lastNode ) 106 firstNode = lastNode = null; 107 108 else { // locate new last node 109 ListNode current = firstNode; 110 111 // loop while current node does not refer to lastNode 112 while ( current.nextNode != lastNode ) 113 current = current.nextNode; 114 115 lastNode = current; // current is new lastNode 116 current.nextNode = null; 117 } 118 119 return removedItem; // return removed node data 120 121 } // end method removeFromBack List.javaLine 100Lines 112-117

  12. Traverse list and print node values 122 123 // determine whether list is empty 124 publicsynchronizedboolean isEmpty() 125 { 126 return firstNode == null; // return true if List is empty 127 } 128 129 // output List contents 130 publicsynchronizedvoid print() 131 { 132 if ( isEmpty() ) { 133 System.out.println( "Empty " + name ); 134 return; 135 } 136 137 System.out.print( "The " + name + " is: " ); 138 ListNode current = firstNode; 139 140 // while not at end of list, output current node's data 141 while ( current != null ) { 142 System.out.print( current.data.toString() + " " ); 143 current = current.nextNode; 144 } 145 146 System.out.println( "\n" ); 147 } 148 149 } // end class List List.javaLines 141-144

  13. Exception thrown when program attempts to remove node from empty list 1 // Fig. 5.4: EmptyListException.java 2 // Class EmptyListException declaration. 3 package com.deitel.jhtp5.ch20; 4 5 public class EmptyListException extends RuntimeException { 6 7 // no-argument constructor 8 public EmptyListException() 9 { 10 this( "List" ); // call other EmptyListException constructor 11 } 12 13 // constructor 14 public EmptyListException( String name ) 15 { 16 super( name + " is empty" ); // call superclass constructor 17 } 18 19 } // end class EmptyListException EmptyListException.javaLines 5-19

  14. Create linked list Create values (Objects) to store in linked-list nodes Insert values in linked list 1 // Fig. 5.5: ListTest.java 2 // ListTest class to demonstrate List capabilities. 3 import com.deitel.jhtp5.ch5.List; 4 import com.deitel.jhtp5.ch5.EmptyListException; 5 6 publicclass ListTest { 7 8 publicstaticvoid main( String args[] ) 9 { 10 List list = new List(); // create the List container 11 12 // objects to store in list 13 Boolean bool = Boolean.TRUE; 14 Character character = new Character( '$' ); 15 Integer integer = new Integer( 34567 ); 16 String string = "hello"; 17 18 // insert references to objects in list 19 list.insertAtFront( bool ); 20 list.print(); 21 list.insertAtFront( character ); 22 list.print(); 23 list.insertAtBack( integer ); 24 list.print(); 25 list.insertAtBack( string ); 26 list.print(); 27 ListTest.javaLine 10Lines 13-16Lines 19-26

  15. Remove values from linked list 28 // remove objects from list; print after each removal 29 try { 30 Object removedObject = list.removeFromFront(); 31 System.out.println( removedObject.toString() + " removed" ); 32 list.print(); 33 34 removedObject = list.removeFromFront(); 35 System.out.println( removedObject.toString() + " removed" ); 36 list.print(); 37 38 removedObject = list.removeFromBack(); 39 System.out.println( removedObject.toString() + " removed" ); 40 list.print(); 41 42 removedObject = list.removeFromBack(); 43 System.out.println( removedObject.toString() + " removed" ); 44 list.print(); 45 46 } // end try block 47 48 // catch exception if remove is attempted on an empty List 49 catch ( EmptyListException emptyListException ) { 50 emptyListException.printStackTrace(); 51 } 52 } 53 54 } // end class ListTest ListTest.javaLines 30-44

  16. The list is: true The list is: $ true The list is: $ true 34567 The list is: $ true 34567 hello $ removed The list is: true 34567 hello true removed The list is: 34567 hello hello removed The list is: 34567 34567 removed Empty list ListTest.javaProgram Output

  17. 5.4 Linked Lists (cont.) (a) firstNode 7 11 new Listnode 12 (b) firstNode 7 11 new Listnode 12 Fig 5.6 Graphical representation of operation insertAtFront.

  18. 5.4 Linked Lists (cont.) (a) firstNode lastNode new Listnode 12 7 11 5 (b) firstNode lastNode new Listnode 12 7 11 5 Fig 5.7 Graphical representation of operation insertAtBack.

  19. 12 7 11 5 5.4 Linked Lists (cont.) (a) firstNode lastNode (b) firstNode lastNode 12 7 11 5 removeItem Fig 5.8 Graphical representation of operation removeFromFront.

  20. 12 7 11 5 5.4 Linked Lists (cont.) (a) firstNode lastNode (b) firstNode current lastNode 12 7 11 5 removeItem Fig 5.9 Graphical representation of operation removeFromBack.

  21. 5.5 Stacks • Stack • Constrained version of a linked list • Add and remove nodes only to and from the top of the stack • Push method adds node to top of stack • Pop method removes node from top of stack

  22. StackInheritance extends List, because a stack is a constrained version of a linked list Method push adds node to top of stack Method pop removes node from top of stack 1 // Fig. 5.10: StackInheritance.java 2 // Derived from class List. 3 package com.deitel.jhtp5.ch20; 4 5 publicclass StackInheritance extends List { 6 7 // construct stack 8 public StackInheritance() 9 { 10 super( "stack" ); 11 } 12 13 // add object to stack 14 publicsynchronizedvoid push( Object object ) 15 { 16 insertAtFront( object ); 17 } 18 19 // remove object from stack 20 publicsynchronized Object pop() throws EmptyListException 21 { 22 return removeFromFront(); 23 } 24 25 } // end class StackInheritance StackInheritance.javaLine 5Lines 14-17Lines 20-23

  23. Create stack Create values (Objects) to store in stack Insert values in stack 1 // Fig. 5.11: StackInheritanceTest.java 2 // Class StackInheritanceTest. 3 import com.deitel.jhtp5.ch5.StackInheritance; 4 import com.deitel.jhtp5.ch5.EmptyListException; 5 6 publicclass StackInheritanceTest { 7 8 publicstaticvoid main( String args[] ) 9 { 10 StackInheritance stack = new StackInheritance(); 11 12 // create objects to store in the stack 13 Boolean bool = Boolean.TRUE; 14 Character character = new Character( '$' ); 15 Integer integer = new Integer( 34567 ); 16 String string = "hello"; 17 18 // use push method 19 stack.push( bool ); 20 stack.print(); 21 stack.push( character ); 22 stack.print(); 23 stack.push( integer ); 24 stack.print(); 25 stack.push( string ); 26 stack.print(); StackInheritanceTest.javaLine 10Lines 13-16Lines 19-26

  24. Remove object from stack 27 28 // remove items from stack 29 try { 30 Object removedObject = null; 31 32 while ( true ) { 33 removedObject = stack.pop(); // use pop method 34 System.out.println( removedObject.toString() + " popped" ); 35 stack.print(); 36 } 37 } 38 39 // catch exception if stack is empty when item popped 40 catch ( EmptyListException emptyListException ) { 41 emptyListException.printStackTrace(); 42 } 43 } 44 45 } // end class StackInheritanceTest StackInheritanceTest.javaLine 33

  25. The stack is: true The stack is: $ true The stack is: 34567 $ true The stack is: hello 34567 $ true hello popped The stack is: 34567 $ true 34567 popped The stack is: $ true $ popped The stack is: true true popped Empty stack com.deitel.jhtp5.ch5.EmptyListException: stack is empty at com.deitel.jhtp5.ch5.List.removeFromFront(List.java:82) at com.deitel.jhtp5.ch5.StackInheritance.pop( StackInheritance.java:22) at StackInheritanceTest.main(StackInheritanceTest.java:33) StackInheritanceTest.javaProgram Output

  26. Method push adds node to top of stack Method pop removes node from top of stack 1 // Fig. 5.12: StackComposition.java 2 // Class StackComposition declaration with composed List object. 3 package com.deitel.jhtp5.ch20; 4 5 publicclass StackComposition { 6 private List stackList; 7 8 // construct stack 9 public StackComposition() 10 { 11 stackList = new List( "stack" ); 12 } 13 14 // add object to stack 15 publicsynchronizedvoid push( Object object ) 16 { 17 stackList.insertAtFront( object ); 18 } 19 20 // remove object from stack 21 publicsynchronized Object pop() throws EmptyListException 22 { 23 return stackList.removeFromFront(); 24 } 25 StackComposition.javaLines 15-18Lines 21-24

  27. 26 // determine if stack is empty 27 publicsynchronizedboolean isEmpty() 28 { 29 return stackList.isEmpty(); 30 } 31 32 // output stack contents 33 publicsynchronizedvoid print() 34 { 35 stackList.print(); 36 } 37 38 } // end class StackComposition StackComposition.java

  28. 5.6 Queues • Queue • Similar to a supermarket checkout line • Nodes inserted only at tail (back) • Method enqueue • Nodes removed only from head (front) • Method dequeue

  29. Method enqueue adds node to top of stack Method dequeue removes node from top of stack 1 // Fig. 5.13: Queue.java 2 // Class Queue. 3 package com.deitel.jhtp5.ch20; 4 5 publicclass Queue { 6 private List queueList; 7 8 // construct queue 9 public Queue() 10 { 11 queueList = new List( "queue" ); 12 } 13 14 // add object to queue 15 publicsynchronizedvoid enqueue( Object object ) 16 { 17 queueList.insertAtBack( object ); 18 } 19 20 // remove object from queue 21 publicsynchronized Object dequeue() throws EmptyListException 22 { 23 return queueList.removeFromFront(); 24 } 25 Queue.javaLines 15-18Lines 21-24

  30. 26 // determine if queue is empty 27 public synchronized boolean isEmpty() 28 { 29 return queueList.isEmpty(); 30 } 31 32 // output queue contents 33 public synchronized void print() 34 { 35 queueList.print(); 36 } 37 38 } // end class Queue Queue.java

  31. Create queue Create values (Objects) to store in queue Insert values in queue 1 // Fig. 5.14: QueueTest.java 2 // Class QueueTest. 3 import com.deitel.jhtp5.ch5.Queue; 4 import com.deitel.jhtp5.ch5.EmptyListException; 5 6 publicclass QueueTest { 7 8 publicstaticvoid main( String args[] ) 9 { 10 Queue queue = new Queue(); 11 12 // create objects to store in queue 13 Boolean bool = Boolean.TRUE; 14 Character character = new Character( '$' ); 15 Integer integer = new Integer( 34567 ); 16 String string = "hello"; 17 18 // use enqueue method 19 queue.enqueue( bool ); 20 queue.print(); 21 queue.enqueue( character ); 22 queue.print(); 23 queue.enqueue( integer ); 24 queue.print(); 25 queue.enqueue( string ); 26 queue.print(); QueueTest.javaLine 10Lines 13-16Lines 19-26

  32. Remove value from queue 27 28 // remove objects from queue 29 try { 30 Object removedObject = null; 31 32 while ( true ) { 33 removedObject = queue.dequeue(); // use dequeue method 34 System.out.println( removedObject.toString() + " dequeued" ); 35 queue.print(); 36 } 37 } 38 39 // process exception if queue is empty when item removed 40 catch ( EmptyListException emptyListException ) { 41 emptyListException.printStackTrace(); 42 } 43 } 44 45 } // end class QueueCompositionTest QueueTest.javaLine 33

  33. The queue is: true The queue is: true $ The queue is: true $ 34567 The queue is: true $ 34567 hello true dequeued The queue is: $ 34567 hello $ dequeued The queue is: 34567 hello 34567 dequeued The queue is: hello hello dequeued Empty queue com.deitel.jhtp5.ch5.EmptyListException: queue is empty at com.deitel.jhtp5.ch5.List.removeFromFront(List.java:88) at com.deitel.jhtp5.ch5.Queue.dequeue(Queue.java:23) at QueueTest.main(QueueTest.java:33) QueueTest.java

  34. 5.7 Trees • Tree • Non-linear, two-dimensional data structure • (unlike linked lists, stacks and queues) • Nodes contain two or more links • Root node is the first node • Each link refers to a child • Left child is the first node in left subtree • Right child is the first node in right subtree • Children of a specific node are siblings • Nodes with no children are leaf nodes

  35. 5.7 Trees (cont.) • Binary search tree • Special ordering of nodes • Values in left subtrees are less than values in right subtrees • Inorder traversal • Traverse left subtree, obtain node value, traverse right subtree • Preorder traversal • Obtain node value, traverse left subtree, traverse right subtree • Postorder traversal • Traverse left subtree, traverse right subtree, obtain node value

  36. 5.7 Trees (cont.) B A D C Fig 5.15 Binary tree graphical representation.

  37. 11 25 7 17 43 31 47 44 65 77 68 93 5.7 Trees (cont.) Fig 5.16 Binary search tree containing 12 values.

  38. Left and right children If value of inserted node is less than value of tree node, insert node in left subtree 1 // Fig. 5.17: Tree.java 2 // Declaration of class TreeNode and class Tree. 3 package com.deitel.jhtp5.ch20; 4 5 // class TreeNode declaration 6 class TreeNode { 7 8 // package access members 9 TreeNode leftNode; 10 int data; 11 TreeNode rightNode; 12 13 // initialize data and make this a leaf node 14 public TreeNode( int nodeData ) 15 { 16 data = nodeData; 17 leftNode = rightNode = null; // node has no children 18 } 19 20 // locate insertion point and insert new node; ignore duplicate values 21 publicsynchronizedvoid insert( int insertValue ) 22 { 23 // insert in left subtree 24 if ( insertValue < data ) { 25 26 // insert new TreeNode 27 if ( leftNode == null ) 28 leftNode = new TreeNode( insertValue ); 29 Tree.javaLines 9 and 11Lines 24-32

  39. If value of inserted node is greater than value of tree node, insert node in right subtree 30 else// continue traversing left subtree 31 leftNode.insert( insertValue ); 32 } 33 34 // insert in right subtree 35 elseif ( insertValue > data ) { 36 37 // insert new TreeNode 38 if ( rightNode == null ) 39 rightNode = new TreeNode( insertValue ); 40 41 else// continue traversing right subtree 42 rightNode.insert( insertValue ); 43 } 44 45 } // end method insert 46 47 } // end class TreeNode 48 49 // class Tree declaration 50 publicclass Tree { 51 private TreeNode root; 52 53 // construct an empty Tree of integers 54 public Tree() 55 { 56 root = null; 57 } 58 59 // insert a new node in the binary search tree 60 publicsynchronizedvoid insertNode( int insertValue ) 61 { Tree.javaLines 35-43

  40. Preorder traversal – obtain data, traverse left subtree, then traverse right subtree 62 if ( root == null ) 63 root = new TreeNode( insertValue ); // create the root node here 64 65 else 66 root.insert( insertValue ); // call the insert method 67 } 68 69 // begin preorder traversal 70 publicsynchronizedvoid preorderTraversal() 71 { 72 preorderHelper( root ); 73 } 74 75 // recursive method to perform preorder traversal 76 privatevoid preorderHelper( TreeNode node ) 77 { 78 if ( node == null ) 79 return; 80 81 System.out.print( node.data + " " ); // output node data 82 preorderHelper( node.leftNode ); // traverse left subtree 83 preorderHelper( node.rightNode ); // traverse right subtree 84 } 85 86 // begin inorder traversal 87 publicsynchronizedvoid inorderTraversal() 88 { 89 inorderHelper( root ); 90 } 91 Tree.javaLines 81-93

  41. Inorder traversal – traverse left subtree, obtain data, then traverse right subtree Postorder traversal – traverse left subtree, traverse right subtree, then obtain data 92 // recursive method to perform inorder traversal 93 privatevoid inorderHelper( TreeNode node ) 94 { 95 if ( node == null ) 96 return; 97 98 inorderHelper( node.leftNode ); // traverse left subtree 99 System.out.print( node.data + " " ); // output node data 100 inorderHelper( node.rightNode ); // traverse right subtree 101 } 102 103 // begin postorder traversal 104 publicsynchronizedvoid postorderTraversal() 105 { 106 postorderHelper( root ); 107 } 108 109 // recursive method to perform postorder traversal 110 privatevoid postorderHelper( TreeNode node ) 111 { 112 if ( node == null ) 113 return; 114 115 postorderHelper( node.leftNode ); // traverse left subtree 116 postorderHelper( node.rightNode ); // traverse right subtree 117 System.out.print( node.data + " " ); // output node data 118 } 119 120 } // end class Tree Tree.javaLines 98-100Lines 115-117

  42. Insert 10 random integers in tree Traverse binary tree via preorder algorithm Traverse binary tree via inorder algorithm 1 // Fig. 5.18: TreeTest.java 2 // This program tests class Tree. 3 import com.deitel.jhtp5.ch5.Tree; 4 5 publicclass TreeTest { 6 7 publicstaticvoid main( String args[] ) 8 { 9 Tree tree = new Tree(); 10 int value; 11 12 System.out.println( "Inserting the following values: " ); 13 14 // insert 10 random integers from 0-99 in tree 15 for ( int i = 1; i <= 10; i++ ) { 16 value = ( int ) ( Math.random() * 100 ); 17 System.out.print( value + " " ); 18 tree.insertNode( value ); 19 } 20 21 System.out.println ( "\n\nPreorder traversal" ); 22 tree.preorderTraversal(); // perform preorder traversal of tree 23 24 System.out.println ( "\n\nInorder traversal" ); 25 tree.inorderTraversal(); // perform inorder traversal of tree TreeTest.javaLines 15-19Line 22Line 25

  43. Traverse binary tree via postorder algorithm 26 27 System.out.println ( "\n\nPostorder traversal" ); 28 tree.postorderTraversal(); // perform postorder traversal of tree 29 System.out.println(); 30 } 31 32 } // end class TreeTest TreeTest.javaLine 28 Inserting the following values: 39 69 94 47 50 72 55 41 97 73 Preorder traversal 39 69 47 41 50 55 94 72 73 97 Inorder traversal 39 41 47 50 55 69 72 73 94 97 Postorder traversal 41 55 50 47 73 72 97 94 69 39

  44. 6 17 33 48 13 27 42 5.7 Trees (cont.) Fig 5.19 Binary search tree with seven values.

More Related