Problem of the Day Solve this equation by moving the numbers: 76 = 24 Problem of the Day Solve this equation by moving the numbers: 76 = 24 2 7 = 49 CSC 212 – Data Structures LECTURE 34: TREE AND BINARY TREE ADT Trees Represent hierarchical relationships Parent-child basis of this abstract data type Real-world example: directories & types csc212/ homeworks/ hard.txt 3K impossible.txt 2K programs/ LLQueue.java 10K DLNode.java 25K ideas.todo 1M Tree.java 20K Trees Represent hierarchical relationships Parent-child basis of this abstract data type Real-world example: organism classification Mammal Cat Lion Ape Human Chimpanzee Professors Yankees Fan Tree Basics Yet another Collection A Nodes are Positions B # of elements is its size Some things are familiar Using Position E C G F to hold elements Others are very different Node class has little overlap Cannot discuss next & prev I J K D H Tree Terms Defined by relationships A Talk about parents, siblings, descendant Nodes have no gender B E C G F Will often mix in terms like mother, father, niece, aunt, uncle, cousin, … I J K D H Tree Terms Tree rooted at node A A “Top” or “Start” of tree B, C, D are children of A A is parent of B, C, D B, C, D are siblings E, F, G, H are grandchildren of A B E C G F I J K D H Tree Relationships Nodes either leaf or interior A Both or neither not options A, B, C, F are parents B At least 1 child for each Also called “interior nodes” E "Leaves" name of others Leaf node has no children C G F I J K D H Trees Recursion Trees are recursive structure A Subtree defined by a node B E C G F I J K D H Trees Recursion Trees are recursive structure A Subtree defined by a node C is root of this subtree B E C G F I J K D H Trees Recursion Trees are recursive structure A Subtree defined by a node C is root of this subtree B C D B root of this subtree E G F I J K H Trees Recursion Trees are recursive structure A Subtree defined by a node C is root of this subtree B C D B root of this subtree D root of this subtree E G F I J K H Trees Recursion Trees are recursive structure Subtree defined by a node C is root of this subtree B root of this subtree D root of this subtree Trees are like professors Some are tall & thin Trees Recursion Trees are recursive structure Subtree defined by a node C is root of this subtree B root of this subtree D root of this subtree Trees are like professors Some are tall & thin Short & boring a possibility Trees Recursion Trees are recursive structure Subtree defined by a node C is root of this subtree B root of this subtree D root of this subtree Trees are like professors Some are tall & thin Short & boring a possibility Others can be empty & useless Trees vs. Binary Trees Both represent parent-child relationships Both consist of single "root" node & its descendants Nodes can have at most one parent Root nodes are orphans -- do not have a parent All others, the non-root nodes must have parent Children not required for any node in the tree No limit to number of children for non-binary nodes 2 children for node in binary tree is the maximum Binary Tree Extends Tree interface & adds following property 2 or fewer children per node Typically drawing of tree sets childrens' names “Left child” is name of child to left of the parent Child towards the right is called “Right child” R B D E F I H Height and Size of a Tree Maximum number of nodes doubles each level Level 0 – max. 1 node Height and Size of a Tree Maximum number of nodes doubles each level Level 0 – max. 1 node Level 1 – max. 2 nodes Height and Size of a Tree Maximum number of nodes doubles each level Level 0 – max. 1 node Level 1 – max. 2 nodes Level 2 – max. 4 nodes Height and Size of a Tree Maximum number of nodes doubles each level For a level i in a binary tree That level can have 2i nodes At most 2(i+1)-1 nodes for tree to that level Level 0 – max. 1 node Level 1 – max. 2 nodes Level 2 – max. 4 nodes Level 3 – max. 8 nodes Independent of Implementation A B Z F I Q M Binary Tree, Tree, or Other? Binary Tree, Tree, or Other? Binary Tree, Tree, or Other? Binary Tree, Tree, or Other? Binary Tree, Tree, or Other? Binary Tree, Tree, or Other? Binary Tree, Tree, or Other? A B C D Binary Tree, Tree, or Other? Your Turn Get into your groups and complete activity For Next Lecture Read parts of GT 7.2 & 7.3.6 for Monday Trees are Iterable, but how would it be done? What are the types of traversals & how do they differ? When do we do parent first & when do we do our kid? Programming Assignment #2 due today To get help, do not delay; end of week gets crazy