11. Binary Trees

  • Feel free to use your laptop

  • You are strongly encourage to work with others

    • When you get stuck, ask those sitting around you for help

    • Get used to working together in the labs

    • Peer teaching and peer learning has been empirically shown to be very effective

Note

This lab focuses on manipulating binary node objects, not the development of an actual tree class. However, a tree structure is ultimately being created with the binary nodes.

11.1. Draw The Tree

  1. Start with a scrap piece of paper

    • The problems being solved are becoming more complex

    • Visualizing what the code is doing by drawing it out may be very helpful

  2. Draw the tree that the following code generates

    public static void main(String[] args){
        BinaryNode<Integer> root = new BinaryNode<>(6);
    
        root.setLeft(new BinaryNode<>(2));
        root.setRight(new BinaryNode<>(8));
    
        root.getLeft().setLeft(new BinaryNode<>(1));
        root.getLeft().setRight(new BinaryNode<>(4));
    
        root.getRight().setLeft(new BinaryNode<>(7));
        root.getRight().setRight(new BinaryNode<>(9));
    
        root.getLeft().getRight().setLeft(new BinaryNode<>(3));
        root.getLeft().getRight().setRight(new BinaryNode<>(5));
    }
    

11.2. Binary Node

  1. Create a BinaryNode class

    • This can be downloaded from here

  2. Ensure the BinaryNode class is working by running the above main method for generating the tree

11.3. In-Order Traversal

  1. Create a recursive inOrderTraversal

    public static <T> void inOrderTraversal(BinaryNode<T> current){
        // Fill me in
    }
    

11.4. Number of Nodes

  1. Write a recursive numberOfNodes method to count the number of nodes within the tree

    public static <T> int numberOfNodes(BinaryNode<T> current){
        // Fill me in
    }
    

11.5. Depth

  1. Write a recursive treeDepth method to calculate the depth of the tree

    public static <T> int treeDepth(BinaryNode<T> current){
        // Fill me in
    }
    

11.6. Balanced

Warning

This problem is more challenging than the previous. Feel free to jump to the Kattis problems if stuck.

  1. Write a recursive isBalanced method to determine if a given tree is balanced

    public static <T> boolean isBalanced(BinaryNode<T> current){
        // Fill me in
    }
    

11.7. Kattis Problems

  1. https://open.kattis.com/problems/quadrant

  2. https://open.kattis.com/problems/judgingmoose

  3. https://open.kattis.com/problems/timeloop

  4. https://open.kattis.com/problems/oddities

  5. https://open.kattis.com/problems/fizzbuzz

  6. https://open.kattis.com/problems/twostones

  7. https://open.kattis.com/problems/spavanac

  8. https://open.kattis.com/problems/cetvrta

  9. https://open.kattis.com/problems/bus

  10. https://open.kattis.com/problems/timeloop

  11. https://open.kattis.com/problems/oddities

  12. https://open.kattis.com/problems/fizzbuzz

  13. https://open.kattis.com/problems/sibice

  14. https://open.kattis.com/problems/datum

  15. https://open.kattis.com/problems/dicecup

  16. https://open.kattis.com/problems/autori

  17. https://open.kattis.com/problems/apaxiaaans

  18. https://open.kattis.com/problems/hissingmicrophone

  19. https://open.kattis.com/problems/trik

  20. https://open.kattis.com/problems/pot

  21. https://open.kattis.com/problems/filip

  22. https://open.kattis.com/problems/reversebinary

  23. https://open.kattis.com/problems/sevenwonders

  24. https://open.kattis.com/problems/zamka

  25. https://open.kattis.com/problems/bijele

  26. https://open.kattis.com/problems/cold

  27. https://open.kattis.com/problems/nastyhacks

  28. https://open.kattis.com/problems/grassseed

  29. https://open.kattis.com/problems/pet

  30. https://open.kattis.com/problems/batterup

  31. https://open.kattis.com/problems/aboveaverage

  32. https://open.kattis.com/problems/icpcawards

  33. https://open.kattis.com/problems/quickbrownfox

  34. https://open.kattis.com/problems/nodup

  35. https://open.kattis.com/problems/conundrum

  36. https://open.kattis.com/problems/bela

  37. https://open.kattis.com/problems/kornislav