Lecture 16-17-18
Binary Trees
Binary Search Trees�Search in BST
Binary Tree
Binary Tree Traversal
Possible?
Give a tree with at least 3 nodes (all nodes must have different keys) such that both its in-order read and its pre-order read are the same, or prove that there is no such tree.
Binary search trees
Binary SEARCH tree
BST
Reminders
CompareTo
compareTo, simplified
Example
public class Test {
public static void main(String args[]) {
Integer x = 5;
System.out.println(x.compareTo(3)); # 1
System.out.println(x.compareTo(5)); # 0
System.out.println(x.compareTo(8)); # -1
}
}
Example
public class Student {
int age;
int compareTo(Student st) {
if this.age == st.age return 0;
if this.age > st.age return 1;
If this.age < st.age return -1;
...
}
}
public static void main(String args[]) {
Student st1 = new Student(19);
Student st2 = new Student(22);
SOS(st1.compareTo(st2)); # -1
BST implementation
Insert
Do we need recursion for the third one?
A: Yes
B: No
Duplicate keys
• Allow them and be consistent with the rule
• Node is a linked (Array) list. All duplicates are linked.
How to delete an element from a BST
Lazy deletion
Complexity
Complexity
What is the output?
A: 10, 5, 3, 7, 15, 18
B: 3, 5, 7, 10, 15, 18
�C: 3, 7, 5, 18, 15, 10
�D: 3, 5, 7, 15, 10, 18
�E: Something else
Iterators
What is the time complexity?
for (int i = 0; i< linkedlist.size(); i++) {
elem = linkedlist.getElem( i ) ;
print(elem);
}
A: O(1)
B: O(n)
C: O(n log n)
D: O(n^2)
E: Something else
Iterators in Java, performance benefits
Iterator interface
In Java, the Iterator interface contains three (well, four now) method signatures:
Iterator for a Linked List
import java.util.*;
class IteratorExample {
public static void main(String [] args) {
// Create an linked list
LinkedList <Integer> intList = new LinkedList <>();
for (int i=10; i<20; i++)
intList.add(i);
import java.util.*;
class IteratorExample {
public static void main(String [] args) {
// Create an linked list
LinkedList <Integer> intList = new LinkedList <>();
for (int i=10; i<20; i++)
intList.add(i);
//To start, we need to obtain an Iterator from a Collection; this is done by calling the iterator() method.
// we’ll obtain Iterator instance from a list:
Iterator itr = intList.iterator(); // created the iterator object
import java.util.*;
class IteratorExample {
public static void main(String [] args) {
// Create an linked list
LinkedList <Integer> intList = new LinkedList <>();
for (int i=10; i<20; i++)
intList.add(i);
Iterator itr = intList.iterator(); // created the iterator object
while(itr.hasNext()) {
Object element = itr.next();
System.out.print(element + " ");
}
System.out.println();
import java.util.*;
class IteratorExample {
public static void main(String [] args) {
// Create an linked list
LinkedList <Integer> intList = new LinkedList <>();
for (int i=10; i<20; i++)
intList.add(i);
Iterator itr = intList.iterator(); //created the iterator object
while(itr.hasNext()) {
Object element = itr.next();
System.out.print(element + " ");
}
System.out.println();
10 11 12 13 14 15 16 17 18 19
import java.util.*;
class IteratorExample {
public static void main(String [] args) {
// Create an linked list
LinkedList <Integer> intList = new LinkedList <>();
for (int i=10; i<20; i++)
intList.add(i);
Iterator itr = intList.iterator(); // created the iterator object
while(itr.hasNext()) {
Object element = itr.next();
System.out.print(element + " ");
}
System.out.println();
10 11 12 13 14 15 16 17 18 19
Iterators for other data structures
Iterable
interface
Iterable interface
Interface Iterator
boolean hasNext();
E next();
void remove();
• The ListIterator interface adds a few more methods.
boolean hasPrevious();
E previous();
•…
In HW6
public class BSTree<T extends Comparable<? super T>> implements Iterable
Example: how to create an iterator
To implement an iterable data structure, we need to:
Example: how to create an iterator
To implement an iterable data structure, we need to:
import java.util.*;
public class A implements Iterable{
ArrayList arr = new ArrayList();
public Iterator iterator() {
return new ArrayList_It(); # another object!
}
Iterator: every second item in ArrayList
import java.util.*;
public class A implements Iterable{
ArrayList arr = new ArrayList();
public Iterator iterator() {
return new ArrayList_It(); # another object!}
# inner class
class ArrayList_It implements Iterator{
int cursor;
public ArrayList_It() {
cursor = 0;
}
import java.util.*;
public class A implements Iterable{
ArrayList arr = new ArrayList();
public Iterator iterator() {
return new ArrayList_It(); # another object!}
# inner class
class ArrayList_It implements Iterator{
int cursor;
public ArrayList_It() {
cursor = 0;
}
import java.util.*;
public class A implements Iterable{
ArrayList arr = new ArrayList();
public Iterator iterator() {
return new ArrayList_It(); # another object!}
# inner class
class ArrayList_It implements Iterator{
int cursor;
public ArrayList_It() {
cursor = 0;
}
public boolean hasNext() {
}
import java.util.*;
public class A implements Iterable{
ArrayList arr = new ArrayList();
public Iterator iterator() {
return new ArrayList_It(); # another object!}
# inner class
class ArrayList_It implements Iterator{
int cursor;
public ArrayList_It() {
cursor = 0;
}
public boolean hasNext() {
if (arr.size() == 0)
return false;
}
import java.util.*;
public class A implements Iterable{
ArrayList arr = new ArrayList();
public Iterator iterator() {
return new ArrayList_It(); # another object!}
# inner class
class ArrayList_It implements Iterator{
int cursor;
public ArrayList_It() {
cursor = 0;
}
public boolean hasNext() {
if (arr.size() == 0)
return false;
if (cursor==arr.size()-1)
return true;
if (cursor <= arr.size()-2) // needs to be tested
return true;
else
return false;
}
import java.util.*;
public class A implements Iterable{
ArrayList arr = new ArrayList();
public Iterator iterator() {
return new ArrayList_It(); # another object!}
# inner class
class ArrayList_It implements Iterator{
int cursor;
public ArrayList_It() {
cursor = 0;
}
public Object next() {
int to_return = (Integer) arr.get(cursor);
cursor = cursor + 2;
return to_return;
}