Trees 5 May 2016 OSU CSE 1
A New Math Type: tree • A ubiquitous concept in computing is that of a tree – Often we are interested in a binary tree , a special case of a tree in which each node has at most two children • An informal introduction (“node”?, “children”?) follows, using pictures rather than any new mathematical notation 5 May 2016 OSU CSE 2
Recursive Structure • A tree is made up of: – A root node – A string of zero or more child nodes of the root, each of which is the root of its own tree • Since a tree may contain other trees, its structure is recursive • Note: the following explanation of trees is adequate for present purposes but is not technically complete; details later… 5 May 2016 OSU CSE 3
B K R S A C L H E P T G 5 May 2016 OSU CSE 4
Nodes B ? K R S A C L H E P T G 5 May 2016 OSU CSE 5
Edges or B Branches K R S A C L H E P T G 5 May 2016 OSU CSE 6
B root node (node B) K R S A C L H E P T G 5 May 2016 OSU CSE 7
children of B root node (or, of node B) K R S A C L H E P T G 5 May 2016 OSU CSE 8
parent of B node S (for example) K R S A C L H E P T G 5 May 2016 OSU CSE 9
B siblings of node S K R S A C L H E P T G 5 May 2016 OSU CSE 10
B K R S A C L H E P T G leaf nodes 5 May 2016 OSU CSE 11
Yes, it’s B drawn root upside down! K R S A C L H E P leaf T G 5 May 2016 OSU CSE 12
size of a tree B is its total number of nodes; here, it is 12 K R S A C L H E P T G 5 May 2016 OSU CSE 13
Your Turn! What’s the size of each of these 4 trees? C Q R A Y W G C L K P H D T B 5 May 2016 OSU CSE 14
path from B node B to node L; its length is 3 K R S A C L H E P T G 5 May 2016 OSU CSE 15
height of a tree is the B length of the longest path from its root; here, it is 4 K R S A C L H E P T G 5 May 2016 OSU CSE 16
Your Turn! What’s the height of each of these 4 trees? C Q R A Y W G C L K P H D T B 5 May 2016 OSU CSE 17
A Tricky One? What’s the height of this D tree? Y L K A N P Q B 5 May 2016 OSU CSE 18
B tree rooted at node B K R S A C L H E P T G 5 May 2016 OSU CSE 19
subtree 0 of tree B rooted at node B (or, of node B) K R S A C L H E P T G 5 May 2016 OSU CSE 20
subtree 1 of tree B rooted at node B (or, of node B) K R S A C L H E P T G 5 May 2016 OSU CSE 21
subtree 2 of tree B rooted at node B (or, of node B) K R S A C L H E P T G 5 May 2016 OSU CSE 22
Notice the recursive B structure: a root plus its subtrees. K R S A C L H E P T G 5 May 2016 OSU CSE 23
Resources • Wikipedia: Tree structure – http://en.wikipedia.org/wiki/Tree_structure 5 May 2016 OSU CSE 24
Recommend
More recommend