Download tutorial3

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Data structures and algorithms tutorial
Sit in groups of 3 to 5 students, so I can move around and answer questions.
1/ Size of a list
With some implementations of data structures we have looked at, we have explicitly stored an integer size
variable. This was incremented by 1 each time a new node was added to the data structure. Imagine we
have a list data structure, but no size variable was included in the implementation. Write a method
that calculates the size of the list. What is the method header, and what is the method body. Hint: use
recursion. What is the base case and what is the recursive of case? What are the pros and cons of storing
a size variable explicitly. Hint what are the time and space complexity of these operations?
2/ Stacks. What do the following lines of code print?
Stack<Integer> stack = new Stack<Integer>();
stack.push(new Integer(10));
System.out.println(stack);
stack.push(new Integer(20));
System.out.println(stack);
stack.push(new Integer(30));
System.out.println(stack);
stack.push(new Integer(40));
System.out.println(stack);
System.out.println(stack.size());
stack.pop();
System.out.println(stack);
stack.pop();
System.out.println(stack);
System.out.println(stack.peek());
System.out.println(stack);
3/ Queues. What do the following lines of code print?
Queue<String> queue = new LinkedList<String>();
queue.add("1");
System.out.println("Items in the queue " + queue);
queue.add("2");
System.out.println("Items in the queue " + queue);
queue.add("3");
System.out.println("Items in the queue " + queue);
System.out.println("remove element: " + queue.remove());
System.out.println("Items in the queue " + queue);
System.out.println("size of queue is " + queue.size());
System.out.println("is queue empty " + queue.isEmpty());
System.out.println("peek at the queue" + queue.peek());
System.out.println("Items in the queue " + queue);
4/ TreeSets. What do the following lines of code print?
TreeSet<String> myTreeSet = new TreeSet<String>();
myTreeSet.add("C");//adds an object
myTreeSet.add("A");//adds an object
myTreeSet.add("B");//adds an object
System.out.println(myTreeSet);
System.out.println(myTreeSet.contains("A"));//does myTreeSet contain A
System.out.println(myTreeSet.first());//first object
System.out.println(myTreeSet.last());//last object
System.out.println(myTreeSet.headSet("B"));//up to the element
System.out.println(myTreeSet.headSet("B", true));//up to and including the
element
System.out.println(myTreeSet.remove("B"));
System.out.println(myTreeSet);
Iterator<String> iterator = myTreeSet.iterator();
// Displaying the Tree set data
while (iterator.hasNext()) {
System.out.print(iterator.next() + " ");
}
5/ Binary Trees. What do the following lines of code print?
// set up some numbers - as STRINGS
Node one = new Node("1");
Node two = new Node("2");
Node three = new Node("3");
Node four = new Node("4");
// set up some
Node add = new
Node mul = new
Node sub = new
arithmetic operators - as STRINGS
Node("+");
Node("*");
Node("-");
// lets set up the connections
// draw the tree to help visualise it.
add.setRight(mul);
add.setLeft(sub);
mul.setRight(one);
mul.setLeft(two);
sub.setRight(three);
sub.setLeft(four);
printPreInPostOrder(mul);
printPreInPostOrder(add);
printPreInPostOrder(sub);
}
// prints the node in different order
public static void printPreInPostOrder(Node node) {
System.out.println("------------------------");
TreeBinary.preorder(node);
System.out.println();
TreeBinary.inorder(node);
System.out.println();
TreeBinary.postorder(node);
System.out.println();
TreeBinary.levelorder(node);
System.out.println();
}
Related documents