course-web-page-fall-2022

Course web page for Data Structures H343 Fall 2022

View the Project on GitHub IUDataStructuresCourse/course-web-page-fall-2022

Review of NextPrevBinaryTree

class Node {
    T data;
    Node left, right, parent;
	
    public Node first() {
        Node n = this;
        while (n.left != null) {
        	n = n.left;
        }
        return n;
    }
	
    public Node first() {
        if(left != null) {
            return left.first();
        } else if(right != null) {
            return right.first();
        }

        return this;
    }		

    public Node last() {
        if (right == null) {
            return this;
        } else {
            return right.last();
        }
    }
	
    public Node nextAncestor() {
        if (parent == null) {
            return null;
		} else if (this == parent.left) {
		    return parent;
		} else { // this == parent.right
        	return parent.nextAncestor();
        }
    }
	
    public Node prevAncestor() {
        Node N = this;
        while (N.parent != null) {
            if (N.parent.right == N)
			    return N.parent;
            N = N.parent;
        }
        return null;
    }		
	
    public Node next() {
        if (this.right == null) {
            return this.nextAncestor();
        } else {
            return right.first();
        }
    }
	
    public Node previous() {
        if (left == null) {
            return prevAncestor();
        } else {
            return left.last();
        }
    }
	
}

class Iter implements Iterator<T>, ReverseIterator<T> {
    private Node curr;

    @Override
    public T get() {
        return curr.data;
    }

    @Override
    public void retreat() {
        curr = curr.previous();
    }

    @Override
    public void advance() {
        curr = curr.next();
    }

    @Override
    public boolean equals(Object other) {
        return curr == other; // comparing a node to an iterator, bad
    }
	
    @Override
    public boolean equals(Object other) {
        return curr.equals(other); // always false, bad
    }
	
    @Override
    public boolean equals(Object other) {
        return ((Iter) other).curr == curr; // good
    }

    @Override
    public Iter clone(){
        return new Iter(curr);
    }		
}

Student Test Code

public class StudentTest{
    @Test
    public void test() throws Exception{
        /*
         * Creates a Binary Tree of arbitrary length and arbitrary elements
         */
        ArrayList<Integer> array1 = new ArrayList<>();
        Random random = new Random();
        for (int i = 0; i < random.nextInt(10000)+1; i++) {
            array1.add(random.nextInt(10000));
        }
        BinaryTree<Integer> tree1 = new BinaryTree<>(array1);

        //Tests begin()
        Assertions.assertEquals(array1.get(0), tree1.begin().get());
        //Tests rbegin()
        Assertions.assertEquals(array1.get(array1.size()-1), 
                                tree1.rbegin().get());
        //Tests advance()
        BinaryTree.Iter iter1 = tree1.begin();
        if (!iter1.equals(tree1.end()))
            iter1.advance();
        Assertions.assertEquals(array1.get(1), iter1.get());

        ...
    }
}

Finish AVL Trees

Review:

Definition The AVL Invariant: the height of two child subtrees may only differ by 1.

Algorithm for fixing AVL invariant

From the changed node on up (there can be several AVL violations)

Example: Remove Node and fix AVL

Recall that we can restore the AVL property using tree rotations:

			y                         x
		   / \    right_rotate(y)    / \
		  x   C  --------------->   A   y
		 / \     <-------------        / \
		A   B     left_rotate(x)      B   C

Given the following AVL Tree, delete the node with key 8. (This example has two nodes that end up violating the AVL property.)

		 8
	   /   \
	  5     10
	 / \   / \
	2   6 9   11
   / \   \      \
  1   3   7      12
	   \
		4 Solution:  * Step 1: replace node 8 with node 9

		   9
		 /   \
		5     10
	   / \     \
	  2   6     11
	 / \   \      \
	1   3   7      12
		 \
		  4

Time Complexity of Insert and Remove for AVL Tree

O(h) = O(log n)

Using AVL Trees for sorting

ADT’s that can be implemented by AVL Trees