Presentation is loading. Please wait.

Presentation is loading. Please wait.

Problem of the Day  Solve this equation by moving the numbers: 76 = 24.

Similar presentations


Presentation on theme: "Problem of the Day  Solve this equation by moving the numbers: 76 = 24."— Presentation transcript:

1 Problem of the Day  Solve this equation by moving the numbers: 76 = 24

2 Problem of the Day  Solve this equation by moving the numbers: 76 = 24 7 2 = 49

3 CSC 212 – Data Structures

4 Trees  Represent hierarchical relationships  Parent-child basis of this abstract data type  Real-world example: directories & types csc212/ homeworks/ ideas.todo 1M programs/ LLQueue.java 10K DLNode.java 25K hard.txt 3K impossible.txt 2K Tree.java 20K

5 Trees  Represent hierarchical relationships  Parent-child basis of this abstract data type  Real-world example: organism classification Mammal Cat Ape LionHumanChimpanzee ProfessorsYankees Fan

6 Tree Basics  Yet another Collection  Nodes are Position s  # of elements is its size  Some things are familiar  Using Position to hold elements  Others are very different  Node class has little overlap  Cannot discuss next & prev A B D C G H E F I J K

7 Tree Terms  Defined by relationships  Talk about parents, siblings, descendant  Nodes have no gender  Will often mix in terms like mother, father, niece, aunt, uncle, cousin, … A B D C G H E F I J K

8 Tree Terms  Tree rooted at node 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 A B D C G H E F I J K

9 Tree Relationships  Nodes either leaf or interior  Both or neither not options parents  A, B, C, F are parents  At least 1 child for each  Also called “interior nodes”  "Leaves"  "Leaves" name of others  Leaf node has no children A B D C G H E F I J K

10 Trees Recursion  Trees are recursive structure  Subtree defined by a node A B D C G H E F I J K

11 Trees Recursion  Trees are recursive structure  Subtree defined by a node  C is root of this subtree A B D C G H E F I J K

12 Trees Recursion  Trees are recursive structure  Subtree defined by a node  C is root of this subtree  B root of this subtree A B D C G H E F I J K

13 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 A B D C G H E F I J K

14 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

15 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

16 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

17 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 must  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

18 Binary Tree  Extends Tree interface & adds following property  2 or fewer children per node  Typically drawing of tree sets childrens' names Left child  “Left child” is name of child to left of the parent Right child  Child towards the right is called “Right child” R B D H E F I

19 Height and Size of a Tree Level 0 – max. 1 node  Maximum number of nodes doubles each level

20 Height and Size of a Tree Level 1 – max. 2 nodes Level 0 – max. 1 node  Maximum number of nodes doubles each level

21 Height and Size of a Tree Level 2 – max. 4 nodes Level 1 – max. 2 nodes Level 0 – max. 1 node  Maximum number of nodes doubles each level

22 Height and Size of a Tree Level 3 – max. 8 nodes Level 2 – max. 4 nodes Level 1 – max. 2 nodes Level 0 – max. 1 node  Maximum number of nodes doubles each level  For a level i in a binary tree  That level can have 2 i nodes  At most 2 (i+1) -1 nodes for tree to that level

23 Independent of Implementation A B Z F I Q M

24 Binary Tree, Tree, or Other?

25

26

27

28

29

30 ABCD 

31

32 Your Turn  Get into your groups and complete activity

33 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  Programming Assignment #2 due today  To get help, do not delay; end of week gets crazy


Download ppt "Problem of the Day  Solve this equation by moving the numbers: 76 = 24."

Similar presentations


Ads by Google