CS 228 meeting -*- Outline -*- * introduction to trees (HR 13) Trees can be used to model hierarchies, and as data structures for searching and sorting Also useful as data structures in applications such as compilers where have additions and deletions, and need to search and sort General trees are useful for file structures (layout of pages on disk), etc. ** motivation ------------------------------------------ TREES (HR 13) Motivating problems: Representing hierarchical information Data structure that supports: 1. changes - insertion - deletion 2. retrieval - fast searching - traversal in sorted order ------------------------------------------ Trees are non-linear data structures ** pictures ------------------------------------------ A HIERARCHY TREE things living non-living animals plants matter ideas vertebrates trees cars boats goodness mammals birds redwoods A FAMILY TREE Iphigenia Clytemnestra Agamemnon Leda Tyndareurs Aerope Atreus Catreus ------------------------------------------ Note that trees in computing are drawn upside-down from biological trees Q: What are some other examples of trees? expression trees parse trees ------------------------------------------ EXPRESSION TREES (HR 13.5) 2 + 3: + 2 3 partition(vec, 0, 1008-1): partition vec 0 - 1008 1 ------------------------------------------ Now show some things that are not trees ------------------------------------------ COUNTEREXAMPLES A B D I A V Reason: C Y E L Reason: ------------------------------------------ ... two roots two parents ... cycle ** terminology (HR 13.1) A lot of the terminology is biological We give different defs than HR, for variety. point out on pictures too ------------------------------------------ TREE TERMINOLOGY (compare HR 13.1) partition vec + - 3 4 1008 1 def: a *tree* (or general tree) is a set of *nodes* together with a child-of relation such that: 1. if the tree is not empty, then there is a unique node, the *root* that is 2. every node, except for the root, is the child of ------------------------------------------ 1. ... not the child of any other node 2. ... exactly one other node Q: How many roots can a tree have? at most one (there can be none) ------------------------------------------ def: there is a *branch* from p to c if def: p is a *parent* of c iff def: c1 is a *sibling* of c2 iff def: g is an *ancestor* of k iff either or (and thus k is a *descendent* of g). def: a *leaf* is ------------------------------------------ ... c is a child of p ... c is a child of p ... c1 and c2 are both children of the same node p. Q: Are 3 and 4 siblings? Are 3 and 1008 siblings? ... either k is a child of g ... or an ancestor or k is a child of g ... a node that has no children (no branches from it) Q: must there be a branch from the root to each leaf in a tree? ------------------------------------------ TREE TERMINOLOGY partition vec + - 3 4 1008 1 def: a *subtree* of a tree T is either the empty tree, or a node and all its descendents def: the *level* of a node n is: 1 + the number of branches to the _____ from n def: the *height* or *depth* of a tree is ------------------------------------------ Q: What is the level of the root? Q: WHat is the level of +? of 3? ... the maximum level of its leaves