Data Structures and Algorithm Design (Review). Java basics Object-oriented design Stacks, queues, and deques Vectors, lists and sequences Trees and binary.

Slides:



Advertisements
Similar presentations
A.A DA1: Binary Tree1 Binary Tree. A.A DA1: Binary Tree2 Implementazione usa Tree LinkedBinaryTree BinaryTree NodePositionList PositionList.
Advertisements

Queues and Linked Lists
Doubly Linked List This problem can be easily solved by using the double linked list. - Ed. 2 and 3.: Chapter 4 - Ed. 4: Chapter 3.
Tree representation and tree search - Ed. 2. and 3.: Chapter 6 - Ed. 4.: Chapter 10.
Postorder traversal - Ed. 2. and 3.: Chapter 6 – - Ed. 4.: Chapter 7 -
Lab 1: 1. Download all my programs in your computer under the same folder. 2. The tree shown in the following figure represents an expression: (((( 3 +
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
Elementary Data Structures CS 110: Data Structures and Algorithms First Semester,
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
Stacks. 2 Outline and Reading The Stack ADT (§4.2.1) Applications of Stacks (§4.2.3) Array-based implementation (§4.2.2) Growable array-based stack.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 The Stack ADT (§4.2) The Stack ADT stores arbitrary objects Insertions and deletions.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
Stacks, Queues, and Deques
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
Stacks. What is a stack? Last-in first-out data structure (LIFO) New objects are placed on top Removal restricted to top object Examples?
1 Lecture 25 Abstract Data Types –II Overview  A stack Interface in Java  StackEmptyException  Stack ADT(A Simple Array-Based Implementation  Queue.
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
Unit 11 1 Unit 11: Data Structures H We explore some simple techniques for organizing and managing information H This unit focuses on: Abstract Data Types.
Stacks, Queues & Deques CSC212.
Basic Algorithms on Trees. If the node v is the root, then the depth of the node v is 0. Otherwise, the depth of the node v is one plus the depth of.
Sequences. The Sequence Abstract Data Type Implementing a Sequence.
Stacks. 2 Outline and Reading The Stack ADT (§2.1.1) Array-based implementation (§2.1.1) Growable array-based stack (§1.5) Java.util.Stack class Java.util.Vector.
Part-B1 Stacks. Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations.
Trees - Ed. 2. and 3.: Chapter 6 - Ed. 4.: Chapter 7.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L12 (Chapter 20) Lists, Stacks,
Part-B1 Stacks. Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Singly Linked Lists - Ed. 2, 3: Chapter 4 - Ed. 4.: Chapter 3.
Stacks. 2 Outline and Reading The Stack ADT (§2.1.1) Applications of Stacks (§2.1.1) Array-based implementation (§2.1.1) Growable array-based stack (§1.5)
Data Structures and Algorithm Design (Review). Java basics Object-oriented design Stacks, queues, and deques Vectors, lists and sequences Trees and binary.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
CSC 212 Stacks & Queues. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  Cannot force you to compile & test.
Vectors, Lists, and Sequences - Ed. 2 and 3.: Chapter 5 - Ed. 4: Chapter 6.
Lists. The Position Abstract Data Type A positionp, which is associated with some elemente in a list S, does not change, even if the rank ofe changes.
Stacks, Queues, and Deques
Trees CS /02/05 L7: Trees Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Definition.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Stacks. week 2a2 Outline and Reading The Stack ADT (§4.1) Applications of Stacks Array-based implementation (§4.1.2) Growable array-based stack Think.
Abstract Data Type (ADT) & Stacks
October 18, Algorithms and Data Structures Lecture V Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.
CM0551 Exam Prep. What are an algorithm’s time and space complexity? (2 marks) Answer: The growth rate of the algorithm’s time requirement and the computer.
Information and Computer Sciences University of Hawaii, Manoa
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 20 Lists, Stacks,
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
Stacks. A stack is a data structure that holds a sequence of elements and stores and retrieves items in a last-in first- out manner (LIFO). This means.
30 May Stacks (5.1) CSE 2011 Winter Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An.
1 Chapter 17 Object-Oriented Data Structures. 2 Objectives F To describe what a data structure is (§17.1). F To explain the limitations of arrays (§17.1).
STACKS AND QUEUES 1. Outline 2  Stacks  Queues.
Analysis of Midterm-Examination Sept. 2015ACS-2947 Yangjun Chen1 1.(a)How to define an abstract data type?(2) How to implement an abstract data type? (b)Describe.
Parasol Lab, Dept. CSE, Texas A&M University
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Vectors, Lists, and Sequences. Vectors: Outline and Reading The Vector ADT (§6.1.1) Array-based implementation (§6.1.2)
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
Stack. ADS2 Lecture 1010 The Stack ADT (GoTa §5.1) The Stack ADT stores arbitrary objects Insertions and deletions follow the last-in.
1 Stacks Abstract Data Types (ADTs) Stacks Application to the analysis of a time series Java implementation of a stack Interfaces and exceptions.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture13.
Chapter 2: Basic Data Structures. Spring 2003CS 3152 Basic Data Structures Stacks Queues Vectors, Linked Lists Trees Priority Queues and Heaps Dictionaries.
DATA STRUCURES II CSC QUIZ 1. What is Data Structure ? 2. Mention the classifications of data structure giving example of each. 3. Briefly explain.
Analysis of Midterm-Examination Jan. 2009ACS-2947 Yangjun Chen1 1.(a)How to define an abstract data type?(2) How to implement an abstract data type (b)Describe.
Elementary Data Structures
Data Structure By Amee Trivedi.
Stacks.
Chapter 17 Object-Oriented Data Structures
Data Structures and Algorithm Design (Review).
Stacks.
Stacks Abstract Data Types (ADTs) Stacks
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Vectors, Lists, and Sequences
CS210- Lecture 6 Jun 13, 2005 Announcements
Presentation transcript:

Data Structures and Algorithm Design (Review)

Java basics Object-oriented design Stacks, queues, and deques Vectors, lists and sequences Trees and binary trees Data Structures and Algorithm Design: Tree traversal Merge sorting Quick sorting Set operations Graphs

Java Basics Class Class Modifiers abstract, final, public Variable Modifiers public, protected, private, static, final Methods Method Modifiers public, protected, private, abstract, final, static Arrays int[] a = new int[ 10 ]; float[][] x = new float[ 8 ][ 10 ]; a[ i ] = 138; x[ i ][ i + 1 ] = x[ i ][ i ];

Object-Oriented Design Inheritance Polymorphism method overriding method overloading Keyword: this Exception Interface, Abstract Classes Type casting

Stacks, Queues, and Deques Stacks Queues Deques Singly linked lists Doubly linked lists Sample case study application

Stacks Definition: A stack is a container of objects that are inserted and removed according to the last-in first-out (LIFO) principle. A stack S is an abstract data type (ADT) that supports following two fundamental methods: push(o): Insert object o at the top of the stack Input : Object; Output : None. pop(): Remove from the stack and return the top object on the stack; an error occurs if the stack is empty. Input : None; Output : Object

public interface Stack { public void push( Object element ); public Object pop() throws StackEmptyException; public int size(); public boolean isEmpty(); public Object top() throws StackEmptyException; }

public class ArrayStack implements Stack { public static final int CAPACITY = 1000; private in capacity; private Object [] S; private int top = -1; public ArrayStatck() { this( CAPACITY ); } public ArrayStack( int cap ) { capacity = cap; S = new Object[ capacity ]; } public int size() { return ( top + 1 ); }

public boolean isEmpty() { return( top < 0 ); } public void push( Object obj ) throws StackFullException { if( size() == capacity ) throw new StackFullException( "Stack overflow" ); S[ ++top ] = obj; } public Object top() throws StackEmptyException { if( isEmpty() ) throw new StackEmptyException( "Stack is empty." ); return S[ top ]; }

public Object pop() throws StackEmptyException { Object elem; if( isEmpty() ) throw new StackEmptyException( "Stack is Empty." ); elem = S[ top ]; S[ top-- ] = null; return elem; }

public class NodeStack implements Stack { protected Node top;// reference to the head node protected int size;// number of elements in the stack public NodeStack() {// constructs an empty stack top = null; size = 0; } public int size() { return size; } public boolean isEmpty() { if (top == null) return true; return false; } public void push(Object elem) { Node v = new Node(elem, top);// create and link-in a top = v;//new node size++; }

public Object top() throws EmptyStackException { if (isEmpty()) throw new EmptyStackException("Stack is empty."); return top.getElement(); } public Object pop() throws EmptyStackException { if (isEmpty()) throw new EmptyStackException("Stack is empty."); Object temp = top.getElement(); top = top.getNext(); // link-out the former top node size--; return temp; }

Sample Case Study Application (1) We want to write a program to calculate the span of the stock’s price on a given day. The span of the stock’s price on a given day: The maximum number of the consecutive days up to the current day such that the stock price on each of those days has been less than or equal to the price on the current day.

Java Implementation

Main idea: The span s i on a certain day i can be easily computed if we know the closest day preceding day i, such that the price on that day is higher than the price on day i. If such a preceding day exists for a day i, let us denote it with h(i), and otherwise let us define h(i) = -1. Then, s i = i – h(i).

h(0) h(1) 0 h(2) 1 h(3) 1 h(4) 3 h(5) 1 h(6) 0 s i = i – h(i). s0s0 1 s1s1 1 s2s2 1 s3s3 2 s4s4 1 s5s5 4 s6s6 6

The problem is how to compute h(i) efficiently? Step 1: p 0 = h(0) = -1, s 0 = 0 - h(0) = 0 – (-1) = 1 0 Day 0. It is possible that h(1) = 0. Step 2: p 1 = Pop days with prices less than or equal to p 1. At this point of time, we have only one element in the stack. It is 0 and p 0 > p 1. So h(1) = 0, s 1 = 1 - h(1) = 1 – 0 = 1. Day 1. It is possible that h(2) =

Step 3: p 2 = Pop days with prices less than or equal to p 2. At this point of time, we have two elements in the stack. The top one is 1 and p 1 > p 2. So h(2) = 1, s 2 = 2 - h(2) = 2 – 1 = 1. Day 2. It is possible that h(3) = Step 4: p 3 = Pop days with prices less than or equal to p 3. The top one will be taken out since p 3 > p 2. The second one is 1 and p 1 > p 3. So h(3) = 1, s 3 = 3 - h(3) = 3 – 1 = 2. Day 3. It is possible that h(4) =

Step 5: p 4 = Pop days with prices less than or equal to p 4. The top one is 3 and p 3 > p 4. So h(4) = 3, s 4 = 4 - h(3) = 4 – 3 = 1. Day 4. It is possible that h(5) = Step 6: p 5 = Pop days with prices less than or equal to p 3. The top two will be taken out since p 5 > p 4 and p 5 > p 3. The third one is 1 and p 1 > p 5. So h(5) = 1, s 5 = 5 - h(5) = 5 – 1 = 4. Day 5. It is possible that h(6) =

Step 7: p 6 = Pop days with prices less than or equal to p 3. The top two will be taken out since p 6 > p 5 and p 6 > p 1. The third one is 0 and p 0 > p 6. So h(6) = 0, s 5 = 6 - h(6) = 6 – 0 = 6. Day 6. The price on day 6. The process stops. 0 6

(2) Calculate the following expression using ArrayStack to control the computation: “ ”. public class Expression-computation{ //start class public static void main( String args[] ) //start main body {String s = " "; Stack data = new ArrayStack(); int temp; char operator; int a = 0; data.push (new Integer (1)); for (int x = 1; x < s.length(); x++) { if (s.charAt(x) == '+‘ || s.charAt(x) == ‘-’) data.push(new Character(s.charAt(x))); else { //else it is a number

operator = (Character) data.pop(); a = ((Integer)data.pop()).intValue(); if (operator == ‘+’) temp = a + charAt(x); else temp = a – charAt(x); data.push(new Integer(temp)); } System.out.println("The answer is: " + ((Integer) data.pop()).intValue()); } // end method main }// end class

Queues Definition: A queue is a container of objects that are inserted and removed according to the first-in first-out (FIFO) principle.

class ArrayQueue implements Queue { private Object[] elem; private int front, rear; private static final int DEFAULT_LENGTH = 100; private int length; public ArrayQueue() { this(DEFAULT_LENGTH); } public ArrayQueue(int length) { elem = new Object[length]; front = rear = 0; length = elem.length; }

public void enqueue(Object element) throws QueueFullException { if (size()==length-1) throw new QueueFullException(); else { elem[rear] = element; rear = (rear+1)%length; }

public Object dequeue() throws QueueEmptyException {if (isEmpty()) throw new QueueEmptyException(); else {Object temp = elem[front]; elem[front] = null; front = (front+1)%length; return temp; } private boolean isFull() { return (rear-front)==(length-1); }

public int size() { return (length-front+rear)%length; } public boolean isEmpty() { return front==rear; } public Object front() throws QueueEmptyException { if (isEmpty()) throw new QueueEmptyException(); else return elem[front]; }

public class ListQueue implements Queue { protected Node front, rear;// reference to the front and rear node protected int size;// number of elements in the queue public ListStack() {// constructs an empty queue front = null; rear = null; size = 0; } public int size() { return size; } public boolean isEmpty() { if (front == null) return true; return false; } public void enqueue(Object elem) { Node v = new Node(elem, null);//create and link-in a new node if (size == 0) {front = v; rear = v;} else {rear.setNext(v); rear = v; size++; }

public Object front() throws QueueEmptyException { if (isEmpty()) throw new QueueEmptyException("Stack is empty."); return front.getElement(); } public Object dequeue() throws QueueEmptyException { if (isEmpty()) throw new QueueEmptyException(“Queue is empty."); Object temp = front.getElement(); front = front.getNext();// link-out the former front node size--; return temp; } /** * Runtime exception thrown when one tries to perform operation *front or dequeue on an empty queue. */ public class QueueEmptyException extends RuntimeException { public QueueEmptyException(String err) { super(err); }

Application case: A breadth-first search traverses a tree as shown in the following Figure. Write an algorithm (not a Java program) to search a tree in the breadth-first manner by using the queue data structure to control the process.

Algorithm: create a Queue Q; put root of the tree into Q; while (Q is not empty) { t  Q.dequeue(); if (t’s left child is not a leaf) put t’s left child into Q; if (t’s right child is not a leaf) put t’s right child into Q; visit t; }

Singly Linked Lists

Class Node

How to generate a singly linked list? class HeadTail { Node head; Node tail; HeadTail(Node x, Node y) { head = x; tail = y; }

public class GeneratingList { public static void main (String[] args) { String [] arr1 = {"Winnipeg","Vancouver","Bejing","Athen“ "London","Berlin","Toronto","Seattle“ "Rome","Baltimore"}; HeadTail a = linkedList(arr1); Node x = a.head; while (x != null) { System.out.println(x.getElement()); x = x.getNext(); }

public static HeadTail linkedList(String[] b) { Node head = null; Node tail = null; Node x = null; for (int i = 0; i < b.length; i++) {x = new Node(); x.setElement(b[i]); if (i == 0 ) {x.setNext(null); tail = x;} else x.setNext(head); head = x; } return new HeadTail(head, tail); }

Doubly Linked List Difference from singly linked lists: -each node contains two links. -two extra nodes: header and trailer, which contain no elements.

Class DLNode

Deques Definition: A double-ended queue is a queue that supports insertion and deletion at both the front and the rear of the queue. A deque D is an abstract data type that supports the following four fundamental methods:

public interface Deque { void insertFirst(Object e); void insertLast(Object e); Object removeFirst(); Object removeLast(); Object first(); Object last(); int size(); boolean isEmpty(); }

Class MyDeque

Vectors, Lists, and Sequences Vectors Lists Sequences Iterators

Vector (interface)List (interface) Sequence (interface)ArrayVector (class)NodeList (class) ArraySequence (class)NodeSequence (class) impl. extends impl. extends impl.

A Simple Array-Based Implementation Vector ADT rank Arrayindex

public interface Vector { public int size(); public boolean isEmpty(); public Object elemAtRank(int r); public Object replaceAtRank(int r, Object e); public void insertAtRank(int r, Object e); public Object removeAtRank(int r); } Vectors

public class ArrayVector implements Vector { private Object[] A;// array storing the elements of the vector private int capacity = 16;// initial length of array A private int size = 0;// number of elements stored in the vector /** Creates the vector with initial capacity 16. */ public ArrayVector() { A = new Object[capacity]; }

public Object elemAtRank (int r) {return a[r];} public int size() {return size;} public boolean isEmpty {return size()==0;} public Object replaceAtRank (int r, Object e) { Object temp=a[r]; a[r]=e; return temp; }

/** Inserts an element at the given rank. */ public void insertAtRank(int r, Object e) throws BoundaryViolationException { checkRank(r, size() + 1); if (size == capacity) {// an overflow capacity *= 2; Object[] B = new Object[capacity]; for (int i=0; i<size; i++) B[i] = A[i]; A = B;} for (int i=size-1; i>=r; i--)// shift elements up A[i+1] = A[i]; A[r] = e; size++; }

/** Removes the element stored at the given rank. */ public Object removeAtRank(int r) throws BoundaryViolationException { checkRank(r, size()); Object temp = A[r]; for (int i=r; i<size-1; i++)// shift elements down A[i] = A[i+1]; size--; return temp; } public int size( ) {return size;}

List: A container of elements that stores each element at a position and that keeps these positions arranged in a linear order. The position abstract data type supports only one simple method: public interface Position { Object element(); } The concept of position is similar to the concept of node in a doubly linked list. Lists

Position element(); Dnode element(){…}; getNext(){…}; getPrev(){…}; setNext(){…}; setPrev(){…}; setElement(){…}; impl.

List ADT position Doubly linked list Dnode Doubly Linked List Implementation

public interface List { /** Returns the number of elements in this list. */ public int size(); /** Returns whether the list is empty. */ public boolean isEmpty(); /** Returns the first node in the list. */ public Position first(); /** Returns the last node in the list. */ public Position last(); /** Returns the node after a given node in the list. */ public Position next(Position p) throws InvalidPositionException, BoundaryViolationException; /** Returns the node before a given node in the list. */ public Position prev(Position p) throws InvalidPositionException, BoundaryViolationException;

/** Inserts an element at the front of the list. */ public Position insertFirst(Object e); /** Inserts and element at the back of the list. */ public Position insertLast(Object e); /** Inserts an element after the given node in the list. */ public Position insertAfter(Position p, Object e) throws InvalidPositionException; /** Inserts an element before the given node in the list. */ public Position insertBefore(Position p, Object e) throws InvalidPositionException; /** Removes a node from the list. */ public Object remove(Position p) throws InvalidPositionException; /** Replaces the element stored at the given node. */ public Object replace(Position p, Object e) throws InvalidPositionException; }

Class NodeList

Sequence

In Java, the interface for sequences is an example of multiple inheritance : interface Sequence extends List, Vector { public Position atRank( int rank ) throws BoundaryViolationException; public int rankOf( Position position ) throws InvalidPositionException; } Vector interface List interface Sequence interface

Sequence ADT Position rank Doubly linked list NodeatRank(r) rankOf(p) Implementation of a sequence with a doubly linked list:

/** Implementation of a sequence by means of a doubly linked list. */ public class NodeSequence extends NodeList implements Sequence { /** Checks whether the given rank is in the range [0, n - 1] */ protected void checkRank(int r, int n) throws BoundaryViolationException { if (r = n) throw new BoundaryViolationException("Illegal rank: " + r); }

/** Returns the position containing the element at the given rank; * O(n) time. */ public Position atRank (int rank) { DNode node; checkRank(rank, size()); if (rank <= size()/2) { // scan forward from the head node = header.getNext(); for (int i=0; i < rank; i++) node = node.getNext(); } else { // scan backward from the tail node = trailer.getPrev(); for (int i=1; i < size()-rank; i++) node = node.getPrev();} return node; }

/** Gets an element at the given rank.*/ public Object elemAtRank(int r) { return atRank(r).element(); } /** Returns the rank of a given position.*/ public int rankOf(Position p) { DNode node; node = header.getNext(); for for (int i=1; i < size(); i++) { if (p == node) return i; else node = node.getNext();} }

/** Inserts an element at the given rank; O(n) time. */ public void insertAtRank (int rank, Object element) throws BoundaryViolationException { checkRank(rank, size() + 1); if (rank == size()) insertLast(element); else { insertBefore(atRank(rank), element); }

/** Removes the element stored at the given rank; O(n) time. */ public Object removeAtRank (int rank) throws BoundaryViolationException { checkRank(rank, size()); return remove(atRank(rank)); } public Object replaceAtRank (int rank, object element) throws BoundadryViolationException { checkRank(rank); return replaceElement(atRank(rank), element); }

Implementing a Sequence with an Array

Iterator

An implementation of the Iterator is always related to container, i.e., a vector, a list, or a sequence. The following is an exemplary implementation of the List Iterator. public class PositionIterator implements Iterator { protected List list; // the underlying list protected Position cur; // the current (next) position public PositionIterator() { } // default constructor public PositionIterator(List L) { // preferred constructor list = L; if (list.isEmpty()) cur = null; // list is empty else cur = list.first(); // start with the first position }

public boolean hasNext() { return (cur != null); } public Object next() throws NoSuchElementException { if (!hasNext()) throw new NoSuchElementException("No next position"); Position toReturn = cur; if (cur == list.last()) cur = null; // no positions left else cur = list.next(cur); // move cursor to the next position return toReturn; } class NoSuchElementException extends Exception { public NoSuchElementException() {super();} public NoSuchElementException(String s) { super(s); } }

In a similar way, we can establish an ElementIterator as follows. public class ElementIterator implements Iterator { protected List list; // the underlying list protected Position cur; // the current (next) position protected Object elementCur;// the current (next) element public ElementIterator() { } // default constructor public ElementIterator(List L) { // preferred constructor list = L; if (list.isEmpty()) cur = null; // list is empty else cur = list.first(); // start with the first position }

public boolean hasNext() { return (cur != null); } public Object next() throws NoSuchElementException { if (!hasNext()) throw new NoSuchElementException("No next position"); elementCur = cur.element(); if (cur == list.last()) cur = null; // no positions left else cur = list.next(cur); // move cursor to the next position return elementCur; }

Trees What is a tree? Tree ADT Basic algorithms on trees Tree traversal

What is a tree?

public interface Tree { public int size(); public Boolean isEmpty(); public ElementIterator elements(); public PositionIterator positions(); public void swapElements( Position v, Position w ); public Object replaceElement( Position v, Object e ); public Position root(); public Position parent( Position v ); public PositionIterator children( Position v ); public boolean isInternal( Position v ); public boolean isExternal( Position v ); public boolean isRoot( Position v ); } Tree Interface – Tree ADT

IspectableContainer size isElement Elements IspectablePositionContainer positions PositionContainer swapElement replaceElement InspectableTree root parent children isRoot isInternal isExternal Tree

A Binary Tree Interface in Java

Data Structures for Representing Trees 1. Storing a binary tree in an array 2. Storing a tree as a linked list

Where a node v of tree T is stored can be determined as follows. Let p(v) be the index, where v is stored. The following relationships must be satisfied: If v is the root of T, then p(v) = 1. If v is the left child of the node u, then p(v) = 2p(u) If v is the right child of the node u, then p(v) = 2p(u) + 1.

abefcgd

Class BTNode

Interface Hierarchy for Positions Position element(); DNode element(){…}; getNext(){…}; getPrev(){…}; setNext(){…}; setPrev(){…}; setElement(){…}; BTNnode element(){…}; getLeft(){…}; getRight(){…}; setLeft(){…}; setRight(){…}; getParent(){…} setElement(){…};

Also see the complete program for “LinkedBinaryTree” posted on the home page of Dr. Yangjun Chen.

IspectableContainer size isElement Elements IspectablePositionContainer positions PositionContainer swapElement replaceElement InspectableTree root, parent, children, isRoot isInternal, isExternal Tree InspectableBinaryTree leftChild, rightChild, sibling BinaryTree LinkedBinaryTree … …, replaceElement, swapElement, expandExternal, removeAboveExternal imple.

Basic Algorithms on Trees

Inorder tree traversal Inorder traversal based on recursion:

inorder(T, r) if … inorder(T, u) “visit” r If … inorder (T, a) inorder(T, u) if … inorder(T, w) “visit” u If … inorder (T, v) inorder(T, w) if … “visit” w if … 1 26

inorder(T, x) if … “visit” x if … 3 inorder(T, v) if … inorder(T, x) “visit” v If … inorder (T, y) 4

if … “visit” y if … 5 inorder(T, a) if … inorder(T, b) “visit” a If … inorder (T, c) 8 7 9

Inorder traversal based on Stack data structure Algorithm Stack-control-inorder(T, v) establish stack S; S.push(v); while (S is not empty) do {u := S.pop(); if u is leaf or u is marked, visit u; else {let v1 and v2 be the left and right child node of v, respectively; S.push(v2); mark u; S.push(u*); S.push(v1); }

u r* a r w u* v r* a u* v r* a print(w) v r* a print(u) x v* y r* a v* y r* a print(x) y r* a print(v) r* a print(y) a print(r) b a* c a* c print(b) c print(a)print(c)

Preorder Traversal Algorithm preorder(T, v): perform the “visit” action for node v for each child w of v call preorder(T, w) v w postorder(T,v) postorder(T,w) Preorder traversal based on recursion:

Algorithm binaryPreorder(T,v): perform the “visit” action for node v if v is an internal node call binaryPreorder(T, T.leftChild( v )) call binaryPreorder(T, T.rightChild( v ))

Preorder traversal based on Stack data structure Algorithm PreorderOnStack(BTree myTree, BNode v) { Establish a Stack myStack; myStack.push(v); While (myStack is not empty) do { u = myStack.pop(); visit(u); if ( u has right child) myStack.push(u.rightchild()); if ( u has left child) myStack.push(u.leftchild());} }

Postorder Traversal Postorder traversal based on recursion: Algorithm Postorder(T,v): for each child w of v call postorder(T,w) perform the “visit” action for node v v w postorder(T,v) postorder(T,w)

Postorder traversal based on Stack data structure Algorithm PostorderOnStack(T,v): {establish stack D; D.push(D); while (D is not empty) do {u := D.pop(); if u is leaf or marked, then visit u; else { let u 1, …, u k be the children of u; mark u; D.push(u); for (i = k; 0 <= i; i--) D.push(u i ):} }

Load a tree from disk into main memory a c g bd efFile: a; b, c, d. b; e, f. e; f; c; g. g; d;

a c g bd ef abefcgd public class Node1 {String x; Node2 y; } public class Node2 {Node1 x; Node2 y; }

S.push(root, null); While (S is not empty) do {x := S.pop( ); generate a node n for x.node_value; if x.point_to_parent is not null then generate links between n and x.point_to_parent; let x 1, …, x k be the children of x; for i = k to 1 do S.push(x i, n); } node_value Point_to_parent a; b, c, d. b; e, f. e; f; c; g. g; d; stack S:

XML File “The Art of Programming” “D. Knuth” “1969” “The Art of Programming” “D. Knuth”“1969”

XML File node_value Point_to_node stack S: Read a file into a character array A : “ T h e A r t …

XML File Algorithm: Scan array A; If A[i] is ‘<’ and A[i+1] is a character then { generate a node x for A[i..j], where A[j] is ‘>’ directly after A[i]; let y = S.top().pointer_to_node; make x be a child of y; S.push(A[i..j], x); If A[i] is ‘ ‘‘ ’, then { genearte a node x for A[i..j], where A[j] is ‘ ’’ ’ directly after A[i]; let y = S.top().pointer_to_node; make x be a child of y; If A[i] is ‘<’ and A[i+1] is ‘/’, then S.pop();

Storing a tree onto disk a df ehbk … a d e … file: node 0 node 1 node 2 i = 0 i = 1 i = 2

Storing a tree onto disk a df ehbk … a d e … file: node 0 node 1 node 2 i = 0 i = 1 i = h 3 … 4

Storing a tree onto disk We search a tree in preorder and use a special stack data structure to control the traversal in such a way the parent address can be recorded. data flag to indicate left or right child parent address

Storing a tree onto disk Algorithm storing-tree(T, v) establish stack S; i = 0; S.push((v, 0, -1)) while (S in not empty) do { u := S.pop(); store u.Data in address i*3; if (u.Parent-address is not equal to –1) then {if (u.Flag == 0) then j := 0; else j := 1; store i in address (u.Parent-address)* j;} let u 1, u 2 be the left and right child of u, respectively; S.push((u 2, 1, i)); S.push((u 1, 0, i)); i++; } -1 indicates that the corresponding node is the root.

Merge Sort

The figure here shows how the sequence is divided in the previous example

The figure here shows how the sequences are merged in the previous example

Quick Sort

Java implementation (each time choose the middle element as the pivot.) public class Sorter { public static void sort (int[] a, int from, int to) { if ((a == null) || (a.length < 2)) return; int i = from, j = to; int center = a[(from + to)/2]; do { while ((i < to) && (a[i] < center)) i++; while ((j > from) && (a[j] > center)) j--; if (i < j) { int tmp =a[i]; a [i] = a[j]; a[j] = tmp;} i++; j--; }while (i <= j); if (from < j) sort(a, from, j); if (i < to) sort(a, i, to); } }

The Set Abstract Data Type

Graphs and Graph Traversal

Data Structure for Graphs