diff --git a/Lab110-VenbergGE/src/AbstractBinaryTree.java b/Lab110-VenbergGE/src/AbstractBinaryTree.java deleted file mode 100644 index 9f19b6c..0000000 --- a/Lab110-VenbergGE/src/AbstractBinaryTree.java +++ /dev/null @@ -1,76 +0,0 @@ -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; -/* - * * Data Structures & Algorithms 6th Edition - * Goodrich, Tamassia, Goldwasser - * Code Fragments 8.7, 8.26, 8.22 - *\ - -/** - * an abstract base class providing some functionality of the binarytree interface - * @author Gabriel Venberg - */ -public abstract class AbstractBinaryTree extends AbstractTree implements BinaryTree { - public Position sibling(Position p){ - Position parent = parent(p); - //p is root. - if (parent == null){return null;} - //p is left child, right child might be null. - if (p==left(parent)){return right(parent);} - //p is right child, left child might be null. - else {return left(parent);} - } - - /**returns the number of children of Position p*/ - public int numChildren(Position p){ - int count=0; - if (left(p)!=null){count++;} - if(right(p)!=null){count++;} - return count; - } - - /**returns an iterable collection of Positions representing p's children.*/ - public Iterable> children(Position p){ - //max capacity of 2 - List > snapshot=new ArrayList<>(2); - //needed to modify this, as the arraylist we made in class needed an index - if(left(p)!=null){snapshot.add(left(p));} - if(right(p)!=null){snapshot.add(right(p));} - // and our arraylist - return snapshot; - } - - /**adds positions of the subtree rooted at Position p to the given snapshot*/ - private void inorderSubtree(Position p, List> snapshot){ - if(left(p)!=null){inorderSubtree(left(p), snapshot);} - snapshot.add(p); - if(right(p)!=null){inorderSubtree(right(p), snapshot);} - } - - /**returns an iterable collection of the positions of the tree, reported in inorder.*/ - public Iterable> inorder(){ - List> snapshot=new ArrayList<>(); - //fill snapshot recursively - if(!isEmpty()){inorderSubtree(root(), snapshot);} - return snapshot; - } - - /**Overrides positions to make inorder the default order for binary trees*/ - public Iterable> positions(){ - return inorder(); - } - - //nested ElementIterator class - /**this class adapts the iteration produced by positions() to returns elements*/ - private class ElementIterator implements Iterator{ - Iterator> posIterator=positions().iterator(); - public boolean hasNext(){return posIterator.hasNext();} - //return element - public E next(){return posIterator.next().getElement();} - public void remove(){posIterator.remove();} - }//end of nested ElementIterator class - - /**returns an iterator if the elements stored in the tree*/ - public Iterator iterator(){return new ElementIterator();} -} diff --git a/Lab110-VenbergGE/src/AbstractTree.java b/Lab110-VenbergGE/src/AbstractTree.java deleted file mode 100644 index 463090e..0000000 --- a/Lab110-VenbergGE/src/AbstractTree.java +++ /dev/null @@ -1,104 +0,0 @@ - -import java.util.ArrayList; -import java.util.List; - -/* - * * Data Structures & Algorithms 6th Edition - * Goodrich, Tamassia, Goldwasser - * Code Fragments 8.2-8.5, 8.19-21 - *\ -/* - * an abstract base class providing some functionality of the tree interface. - * @author Gabriel Venberg - */ -public abstract class AbstractTree implements Tree { - public boolean isInternal(Position p) {return numChildren(p)>0;} - public boolean isExternal(Position p){return numChildren(p)==0;} - public boolean isRoot(Position p){return p == root();} - public boolean isEmpty(){return size()==0;} - - /**returns the number of levels sperating position p from the root.*/ - public int depth(Position p){ - if (isRoot(p)){return 0;} - else{return 1+depth(parent(p));} - } - - /**returns the hight of the tree.*/ - private int hightBad(){ //works, but quadratic worst case time. - int h=0; - for(Position p : positions()){ - //only consider leaf positions. - if(isExternal(p)){h=Math.max(h, depth(p));} - } - return h; - } - - /**returns the hight of the subtree rooted at position p. should be O(n) time.*/ - public int hight(Position p){ - //base case if p is external - int h=0; - for (Position c : children(p)){ - h=Math.max(h,1+hight(c)); - } - return h; - } - - //iterators - /**adds positions of the subtree rooted at position p to the given snapshot (for use in traversal)*/ - private void preorderSubtree(Position p, List> snapshot){ - //for preorder, add position p before exploring subtrees. - snapshot.add(p); - for(Position c:children(p)){ - preorderSubtree(c, snapshot); - } - } - - /**returns an iterable collection of positions in the tree, reported in preorder*/ - public Iterable> preorder(){ - List> snapshot=new ArrayList<>(); - //fill the snapshot recursively - if(!isEmpty()){ - preorderSubtree(root(), snapshot); - } - return snapshot; - } - - /**adds positions of the subtree rooted at position p to the given snapshot (for use in traversal)*/ - private void postorderSubtree(Position p, List> snapshot){ - //for postorder, add position p before exploring subtrees. - for(Position c:children(p)){ - postorderSubtree(c, snapshot); - } - snapshot.add(p); - } - - /**returns an iterable collection of positions in the tree, reported in postorder*/ - public Iterable> postorder(){ - List> snapshot=new ArrayList<>(); - //fill the snapshot recursively - if(!isEmpty()){ - postorderSubtree(root(), snapshot); - } - return snapshot; - } - - /**returns an iterable collection of positions in the tree in breadth first traversal*/ - public Iterable> breadthFirst(){ - List> snapshot=new ArrayList<>(); - if(!isEmpty()){ - Queue> fringe=new LinkedQueue<>(); - fringe.enqueue(root()); - while(!fringe.isEmpty()){ - Position p=fringe.dequeue(); - snapshot.add(p); - for(Position c:children(p)){ - fringe.enqueue(c); - } - } - } - return snapshot; - } - - /**default iterator*/ - public Iterable> positions(){return preorder();} -} diff --git a/Lab110-VenbergGE/src/ArrayStack.java b/Lab110-VenbergGE/src/ArrayStack.java new file mode 100644 index 0000000..c84a847 --- /dev/null +++ b/Lab110-VenbergGE/src/ArrayStack.java @@ -0,0 +1,55 @@ +/* + * Data Structures & Algorithms 6th Edition + * Goodrich, Tamassia, Goldwasser + * Code Fragment 6.2 + * + * An implementation of an ArrayStack class + * */ + +/** + * + * @author Gabriel Venberg + */ +public class ArrayStack implements Stack { + public static final int CAPACITY = 1000;//default capacity + private E[] data; //generic array used for storage. + private int t=-1; //index of the top element in the stack + + + //im swiching the authors comments to javadoc style, for ease of use. + + /** + * constructs stack with default capacity + */ + public ArrayStack(){this(CAPACITY);} + + /** + * constructs stack with given capacity + * @param capacity capacity to construct the stack with. + */ + public ArrayStack(int capacity){ + data = (E[]) new Object[capacity]; + } + + public int size(){return (t+1);} + + public boolean isEmpty(){return(t==-1);} + + public void push(E e) throws IllegalStateException{ + if(size()==data.length){throw new IllegalStateException("Stack is full");} + data[++t]=e; //increment t before storing a new item. + } + + public E top(){ + if(isEmpty()){return null;} + return data[t]; + } + + public E pop(){ + if(isEmpty()){return null;} + E answer = data[t]; + data[t] = null; //dereference to help with garbage collection. + t--; + return answer; + } +} diff --git a/Lab110-VenbergGE/src/BinarySearchTree.java b/Lab110-VenbergGE/src/BinarySearchTree.java index 0f4dbbb..cd19460 100644 --- a/Lab110-VenbergGE/src/BinarySearchTree.java +++ b/Lab110-VenbergGE/src/BinarySearchTree.java @@ -1,193 +1,344 @@ + +import java.util.ArrayList; + /** * * @author Gabriel Venberg */ -public class BinarySearchTree extends AbstractBinaryTree { +public class BinarySearchTree { //Represent a node of binary tree - private static class Node implements Position{ + private static class Node implements Position { + private int data; private Node left; private Node right; private Node parent; - public Node(int data){ + public Node(int data) { //Assign data to the new node, set left and right children to null this.data = data; this.left = null; this.right = null; this.parent = null; } - - public Integer getElement(){return data;} - - public Node getLeft(){return left;} - public Node getRight(){return right;} - public Node getParent(){return parent;} - public void setData(int newData){data=newData;} - public void setLeft(Node newLeft){left=newLeft;} - public void setRight(Node newRight){right=newRight;} - public void setParent(Node newParent){parent=newParent;} - } - //Represent the root of binary tree - private Node root; - private int size = 0; + public Integer getElement() { + return data; + } - public BinarySearchTree(){ - root = null; - } - - public Position root(){return root;} - public int size(){return size;} - - //nonpublic utility - /**validates the position and returns it as a node*/ - protected Node validate(Position p) throws IllegalArgumentException{ - if(!(p instanceof Node)){ - throw new IllegalArgumentException("not a valid position type"); - } - //safe cast - Node node=(Node)p; - //our convention for a defunct node. Wont this make the GC not clean it up? why not just set the parent to null and let the GC clean it up? - if(node.getParent()==node){ - throw new IllegalArgumentException("p is no longer in the tree"); - } - return node; - } - - //methods for getting info about specific nodes. - public Position parent(Position n){ - Node node=validate(n); + public Node getLeft() { + return left; + } + + public Node getRight() { + return right; + } + + public Node getParent() { + return parent; + } + + public void setData(int newData) { + data = newData; + } + + public void setLeft(Node newLeft) { + left = newLeft; + } + + public void setRight(Node newRight) { + right = newRight; + } + + public void setParent(Node newParent) { + parent = newParent; + } + } + + //Represent the root of binary tree + private Node root; + private int size = 0; + + public BinarySearchTree() { + root = null; + } + + public Position root() { + return root; + } + + public int size() { + return size; + } + + //nonpublic utility + /** + * validates the position and returns it as a node + */ + protected Node validate(Position p) throws IllegalArgumentException { + if (!(p instanceof Node)) { + throw new IllegalArgumentException("not a valid position type"); + } + //safe cast + Node node = (Node) p; + //our convention for a defunct node. Wont this make the GC not clean it up? why not just set the parent to null and let the GC clean it up? + if (node.getParent() == node) { + throw new IllegalArgumentException("p is no longer in the tree"); + } + return node; + } + + //methods for getting info about specific nodes. + public Position parent(Position n) { + Node node = validate(n); return node.getParent(); - } - - public Position left(Position n){ - Node node = validate(n); - return node.getLeft(); - } - - public Position right(Position n){ - Node node = validate(n); - return node.getLeft(); - } + } - //insert() will add new node to the binary search tree - public void insert(int data) { - //Create a new node - Node newNode = new Node(data); - size++; + public Position left(Position n) { + Node node = validate(n); + return node.getLeft(); + } - //Check whether tree is empty - if(root == null){ - root = newNode; - return; - } - else { + public Position right(Position n) { + Node node = validate(n); + return node.getLeft(); + } - //current node point to root of the tree - Node current = root, parent = null; + //copy and modify to find method, use find to calculate depth of each node + //insert() will add new node to the binary search tree + public void insert(int data) { + //Create a new node + Node newNode = new Node(data); + size++; - while(true) { - //parent keep track of the parent node of current node. - parent = current; + //Check whether tree is empty + if (root == null) { + root = newNode; + return; + } else { - //If data is less than current's data, node will be inserted to the left of tree - if(data < current.data) { - current = current.getLeft(); - if(current == null) { - parent.setLeft(newNode); - newNode.setParent(parent); - return; - } - } - //If data is greater than current's data, node will be inserted to the right of tree - else { - current = current.getRight(); - if(current == null) { - parent.setRight(newNode); - newNode.setParent(parent); - return; - } - } - } - } - } + //current node point to root of the tree + Node current = root, parent = null; - //minNode() will find out the minimum node - public Position minNode(Node root) { - if (root.left != null) - return minNode(root.left); - else - return root; - } + while (true) { + //parent keep track of the parent node of current node. + parent = current; - //deleteNode() will delete the given node from the binary search tree - public Position deleteNode(Position position, int value) { - size--; - Node node = validate(position); - if(node == null){ - return null; - } - else { - //value is less than node's data then, search the value in left subtree - if(value < node.getElement()) - //should be a safe cast... - node.setLeft((Node)deleteNode(node.getLeft(), value)); + //If data is less than current's data, node will be inserted to the left of tree + if (data < current.data) { + current = current.getLeft(); + if (current == null) { + parent.setLeft(newNode); + newNode.setParent(parent); + return; + } + } //If data is greater than current's data, node will be inserted to the right of tree + else { + current = current.getRight(); + if (current == null) { + parent.setRight(newNode); + newNode.setParent(parent); + return; + } + } + } + } + } - //value is greater than node's data then, search the value in right subtree - else if(value > node.getElement()) - //should be a safe cast... - node.setRight((Node)deleteNode(node.getRight() , value)); + //find() will take a key and return the depth of that key. + public int find(int data) throws IllegalStateException, IllegalArgumentException { + int depth = 0; + //chekc if tree is empty + if (root == null) { + throw new IllegalStateException("tree is empty"); + } else { + Node current = root; + Node parent = null; + while (true) { + //advance our way along the tree + parent = current; + if(data < current.data) { + current = current.getLeft(); + //if there is no left child, element does not exist. + if (current == null) { + throw new IllegalArgumentException("data does not exitst"); + } + } else if(data > current.data) { + current = current.getRight(); + if (current == null) { + throw new IllegalArgumentException("data does not exitst"); + } + } //must otherwise be equal + else { + return depth; + } + depth++; + } + } + } + + //gets the hight of the tree. should run in O(nlog(n)) time. + public int hight() throws IllegalStateException { + ArrayList nodeList = nodeList(root); + int hight = 0; + for(int i=0; i minNode(Node root) { + if (root.left != null) { + return minNode(root.left); + } else { + return root; + } + } - //If node to be deleted has only one right child - else if(node.getLeft() == null) { - node = node.getRight() ; - } + //deleteNode() will delete the given node from the binary search tree + public Position deleteNode(Position position, int value) { + size--; + Node node = validate(position); + if (node == null) { + return null; + } else { + //value is less than node's data then, search the value in left subtree + if (value < node.getElement()) //should be a safe cast... + { + node.setLeft((Node) deleteNode(node.getLeft(), value)); + } //value is greater than node's data then, search the value in right subtree + else if (value > node.getElement()) //should be a safe cast... + { + node.setRight((Node) deleteNode(node.getRight(), value)); + } //If value is equal to node's data that is, we have found the node to be deleted + else { + //If node to be deleted has no child then, set the node to null + if (node.getLeft() == null && node.getRight() == null) { + node = null; + } //If node to be deleted has only one right child + else if (node.getLeft() == null) { + node = node.getRight(); + } //If node to be deleted has only one left child + else if (node.getRight() == null) { + node = node.getLeft(); + } //If node to be deleted has two children node + else { + //then find the minimum node from right subtree + //should be a safe cast... + Node temp = (Node) minNode(node.getRight()); + //Exchange the data between node and temp + node.setData(temp.getElement()); + //Delete the node duplicate node from right subtree + //should be a safe cast... + node.setRight((Node) deleteNode(node.getRight(), temp.getElement())); + } + } + return node; + } + } - //If node to be deleted has only one left child - else if(node.getRight() == null) { - node = node.getLeft(); - } + //inorder() will perform inorder traversal on binary search tree + //made iterative + public void inorderTraversal(Position position) { + Node node = validate(position); + //Check whether tree is empty + if (root == null) { + System.out.println("Tree is empty"); + return; + } else { + ArrayStack nodeStack = new ArrayStack<>(size); + Node current = root; + while(current!=null||!nodeStack.isEmpty()){ + //go down the left side as far as we can. + while(current!=null){ + nodeStack.push(current); + current=current.getLeft(); + } + //then go up one and to the right + current = nodeStack.pop(); + System.out.print(current.getElement()); + current = current.getRight(); + } + } + } + //turns out that if we visit the right node before the left one in a preorder, we get a reversed post order traversal. + public void postorderTraversal(Position position) { + Node root = validate(position); + if (root == null) { + System.out.println("Tree is empty"); + return; + } else { + ArrayStack nodeStack = new ArrayStack<>(size); + ArrayStack returnStack = new ArrayStack<>(size); + nodeStack.push(root); + //do a modified preorder + while(!nodeStack.isEmpty()){ + Node node = nodeStack.pop(); + returnStack.push(node.getElement()); + if (node.getRight() != null) { + nodeStack.push(node.getRight()); + } + if (node.getLeft() != null) { + nodeStack.push(node.getLeft()); + } + } + //print in reverse order + while(!returnStack.isEmpty()){ + System.out.print(returnStack.pop()); + } + } + } + + //made iterative + public void preorderTraversal(Position position) { + Node root = validate(position); + //Check whether tree is empty + if (root == null) { + System.out.println("Tree is empty"); + return; + } else { + ArrayStack nodeStack = new ArrayStack<>(size); + nodeStack.push(root); + while (!nodeStack.isEmpty()) { + Node node = nodeStack.pop(); + System.out.print(node.getElement()); + if (node.getLeft() != null) { + nodeStack.push(node.getLeft()); + } + if (node.getRight() != null) { + nodeStack.push(node.getRight()); + } + } + } + } - //If node to be deleted has two children node - else { - //then find the minimum node from right subtree - //should be a safe cast... - Node temp = (Node)minNode(node.getRight() ); - //Exchange the data between node and temp - node.setData(temp.getElement()); - //Delete the node duplicate node from right subtree - //should be a safe cast... - node.setRight((Node)deleteNode(node.getRight() , temp.getElement())); - } - } - return node; - } - } - - //inorder() will perform inorder traversal on binary search tree - public void inorderTraversal(Position position) { - Node node = validate(position); - //Check whether tree is empty - if(root == null){ - System.out.println("Tree is empty"); - return; - } - else { - - if(node.getLeft()!= null) - inorderTraversal((Position)node.getLeft()); - System.out.print(node.getElement() + " "); - if(node.getRight() != null) - inorderTraversal((Position)node.getRight()); - - } - } -} \ No newline at end of file + //returns a list of entries from the external children of the node. + //made iterative + public ArrayList nodeList(Position position) throws IllegalStateException { + Node root = validate(position); + ArrayList nodeList = new ArrayList<>(); + //Check whether tree is empty + if (root == null) { + throw new IllegalStateException("tree is empty"); + } else { + ArrayStack nodeStack = new ArrayStack<>(size); + nodeStack.push(root); + while (!nodeStack.isEmpty()) { + Node node = nodeStack.pop(); + //only add if it is external. + if (node.getLeft() == null && node.getRight() == null) { + nodeList.add(node.getElement()); + } + if (node.getLeft() != null) { + nodeStack.push(node.getLeft()); + } + if (node.getRight() != null) { + nodeStack.push(node.getRight()); + } + } + } + return nodeList; + } +} diff --git a/Lab110-VenbergGE/src/BinaryTree.java b/Lab110-VenbergGE/src/BinaryTree.java deleted file mode 100644 index 8f44182..0000000 --- a/Lab110-VenbergGE/src/BinaryTree.java +++ /dev/null @@ -1,18 +0,0 @@ -/* - * * Data Structures & Algorithms 6th Edition - * Goodrich, Tamassia, Goldwasser - * Code Fragments 8.6 - *\ - -\** - *an interface for a binary tree, in which each node has at most two children. - * @author Gabriel Venberg - */ -public interface BinaryTree extends Tree { - /**returns the position of p's left child (or null if no child exists).*/ - Position left(Position p) throws IllegalArgumentException; - /**returns the position of p's right child (or null if no child exists)*/ - Position right(Position p) throws IllegalArgumentException; - /**returns the position of p's sibling (or null of no sibling exists).*/ - Position sibling(Position p) throws IllegalArgumentException; -} diff --git a/Lab110-VenbergGE/src/Client.java b/Lab110-VenbergGE/src/Client.java index 9e362b1..6e7bc80 100644 --- a/Lab110-VenbergGE/src/Client.java +++ b/Lab110-VenbergGE/src/Client.java @@ -27,7 +27,7 @@ public class Client { //set up stuff needed for test. - final int BSTSize = 10; + final int BSTSize = 1000000; long startTime; long endTime; String[][] data = new String[7][2]; @@ -36,27 +36,34 @@ public class Client { BinarySearchTree testTree = new BinarySearchTree(); startTime=System.nanoTime(); for(int i=0; i=0; i--){ - System.out.println("test3,"+i); testTree.insert(i); } - data[1][1] = String.format("%,d", testTree.hight(testTree.root())); - System.out.println("test4,"); + data[1][1] = String.format("%,d", testTree.hight()); endTime=System.nanoTime(); data[1][0]=String.format("%,d", endTime-startTime); + + testTree.preorderTraversal(testTree.root()); + System.out.println(); + testTree.postorderTraversal(testTree.root()); + System.out.println(); testTree.inorderTraversal(testTree.root()); System.out.println(); @@ -73,15 +80,11 @@ public class Client { startTime=System.nanoTime(); for(int j=0; j implements Queue{ - private SinglyLinkedList list = new SinglyLinkedList(); //an empty list - public LinkedQueue(){} //new queue relies on initaly empty list - public int size(){return list.size();} - public boolean isEmpty(){return list.isEmpty();} - public void enqueue(E element){list.addLast(element);} - public E first(){return list.first();} - public E dequeue(){return list.removeFirst();} -} diff --git a/Lab110-VenbergGE/src/Queue.java b/Lab110-VenbergGE/src/Queue.java deleted file mode 100644 index 0126b36..0000000 --- a/Lab110-VenbergGE/src/Queue.java +++ /dev/null @@ -1,28 +0,0 @@ -/** - * Data Structures & Algorithms 6th Edition - * Goodrich, Tamassia, Goldwasser - * Code Fragment 6.9 - * - * An implementation of the Queue interface - * */ - -/** - * - * @author Gabriel Venberg - */ -public interface Queue { - /** returns the number of elements in the queue*/ - int size(); - - /** tests whether the queue is empty*/ - boolean isEmpty(); - - /**inserts an element at the rear of the queue*/ - void enqueue(E e); - - /**returns, but does not remove, the first element of the queue (null if empty). */ - E first(); - - /** removes and returns the first element of the queue (null if empty)*/ - E dequeue(); -} diff --git a/Lab110-VenbergGE/src/SinglyLinkedList.java b/Lab110-VenbergGE/src/SinglyLinkedList.java deleted file mode 100644 index 88f25b8..0000000 --- a/Lab110-VenbergGE/src/SinglyLinkedList.java +++ /dev/null @@ -1,78 +0,0 @@ -/** - *SinglyLinkedListClass - * Code Fragments 3.14, 3.15 - * from - * Data Structures & Algorithms, 6th edition - * by Michael T. Goodrich, Roberto Tamassia & Michael H. Goldwasser - * Wiley 2014 - * Transcribed by - * @author Gabe Venberg - */ -public class SinglyLinkedList { - - private static class Node { - private E element; //refrence to element stored at this node - private Node next; //refrence to subsequent node of list - - public Node(E e, Node n){ - element = e; - next = n; - } - - public E getElement() {return element;} - - public Node getNext() {return next;} - - public void setNext(Node n) {next = n;} - } - - //instance variables of SinglyLinkedList - private Node head = null;//head node of list - private Node tail = null;//last node of list - private int size = 0;//number of nodes in list - - public SinglyLinkedList(){}//constructs an initaly empty list - - //access methods - public int size() {return size;} - - public boolean isEmpty() {return size == 0;} - - public E first(){//returns but does not remove the first element - if (size == 0) {return null;} //special case - return head.getElement(); - } - - public E last(){//returns but does not remove last elemnt - if (size ==0) {return null;}//special case - return tail.getElement(); - } - - //update methods - public void addFirst(E e){//adds element e to the front of the list - head = new Node<>(e, head);//create and link a new node - if (size == 0) {tail = head;}//special case, head becomes tail also - size++; - } - - public void addLast(E e){//adds element to end of list - Node newest = new Node<>(e, null);//create and link a new node - if(size == 0){//special case, previously empty list - head = newest; - } - else{ - tail.setNext(newest);//new node after existing tail - } - tail = newest;//new node becomes tail - size++; - } - - public E removeFirst(){//removes and returns the first element - if(size == 0){return null;}//nothing to remove - E answer = head.getElement(); - head = head.getNext();//will become null if list had only one node. - size--; - if(size==0){tail = null;}// special case as list is now empty - return answer; - } -} diff --git a/Lab110-VenbergGE/src/Stack.java b/Lab110-VenbergGE/src/Stack.java new file mode 100644 index 0000000..7930e6e --- /dev/null +++ b/Lab110-VenbergGE/src/Stack.java @@ -0,0 +1,46 @@ +/** + * Data Structures & Algorithms 6th Edition + * Goodrich, Tamassia, Goldwasser + * Code Fragment 6.1 + * + * An implementation of the stack interface + * */ + +/** + * A collection of objects that are inserted and removed according to a last-in + * first-out principle. Although similar in purpose, this interface differs from + * java.util.stack. + * @author Gabriel Venberg + */ +public interface Stack { + + /** + * returns the number of elements in the stack + * @return number of elements in the stack. + */ + int size(); + + /** + * tests whether the stack is empty. + * @return true if stack is empty, false otherwise. + */ + boolean isEmpty(); + + /** + * inserts an element at the top of the stack. + * @param e the element to be inserted. + */ + void push(E e); + + /** + * returns, but does not remove, the top element of the stack. + * @return top element of the stack or null if empty. + */ + E top(); + + /** + * removes and returns the top element from the stack. + * @return element removed or null if empty. + */ + E pop(); +} diff --git a/Lab110-VenbergGE/src/Tree.java b/Lab110-VenbergGE/src/Tree.java deleted file mode 100644 index 79c5802..0000000 --- a/Lab110-VenbergGE/src/Tree.java +++ /dev/null @@ -1,29 +0,0 @@ - -import java.util.Iterator; - -/* - * Data Structures & Algorithms 6th Edition - * Goodrich, Tamassia, Goldwasser - * Code Fragment 8.1 - * - * An implementation of the tree interface - */ - - -/** - * An interface for a tree where nodes can have an arbitrary number of children. - * @author Gabriel Venberg - */ -public interface Tree extends Iterable{ - Position root(); - Position parent(Position p) throws IllegalArgumentException; - Iterable> children(Position p) throws IllegalArgumentException; - int numChildren(Position p) throws IllegalArgumentException; - boolean isInternal(Position p) throws IllegalArgumentException; - boolean isExternal(Position p) throws IllegalArgumentException; - boolean isRoot(Position p) throws IllegalArgumentException; - int size(); - boolean isEmpty(); - Iterator iterator(); - Iterable> positions(); -}