{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# 26 - CMPSCI 187:Programming With Data Structures Lecture#26...

This preview shows pages 1–5. Sign up to view the full content.

CMPSCI 187:Programming With Data Structures Lecture #26: Trees: Definition and Implementation 16 November 2011

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Trees: Definition and Implementation • Tree Vocabulary • Basic Strategies to Implement Trees • Ways to Traverse a Tree • Implementing Trees with Links • Implementing Trees with Arrays
Tree Vocabulary • Trees are made up of nodes . The key property of a tree is that every node except for one, the root , has exactly one parent . We use genealogical language throughout, so that children of the same node are siblings , a node that can be reached by following parent pointers is an ancestor , and a node whose ancestor you are is your descendent . • Nodes with no children are called leaves , and nodes with children are internal nodes . The root is at level 0 , and the level of any other node is the number of parent pointers you must follow to reach the root. The height of a tree is the largest level number of any node in it. • A tree is binary if every internal node has either one or two children, and it is k-ary if every node has at most k children. L&C often implicitly assume that every internal node has exactly two children, but this isn’t a math course...

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Kinds of Binary Trees • A binary tree is called balanced in L&C if its leaves are all on one level or on two adjacent levels. A balanced tree of height h has at most 2^h leaves, and the fewest it can have (with no unary nodes) is 2^{h-1} + 1, still O(2^h). The height of a balanced tree with n leaves, or with n nodes, is O(log n).
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}