Trees Lower bound on sorting Tree introduction terminology
Trees Lower bound on sorting Tree introduction & terminology February 07, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 1
Detour – back to sorting Lower bounds on worst case • The big question: Is it possible for a comparison-based sorting algorithm to have better asymptotic worst-case performance than Merge sort? February 07, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 2
Lower bounds on sorting • a b c d The leaves represent the sorted output for some particular input permutation Y b<c N a<b Y a<c Y … N N Y Each path from root to a leaf is the sequence of decisions made to sort some input permutation a<c Y b<c N N Y … … sorted … b<c N … sorted February 07, 2020 Longest path: maximum decisions in worst case Cinda Heeren / Andy Roth / Geoffrey Tien 3
Lower bounds on sorting Decision tree model • February 07, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 4
Lower bounds on sorting Height of a perfect decision tree • The longest decision path can be no shorter than this But – there are non-comparison-based sorting algorithms that can perform better (with assumptions). Stay tuned in CPSC 320 (maybe)! February 07, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 5
Trees February 07, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 6
Review: linked lists • Linked lists are constructed out of nodes, consisting of – a data element – a pointer to another node • Lists are constructed as chains of such nodes List February 07, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 7
Trees • Trees are also constructed from nodes – Nodes may now have pointers to one or more other nodes • A set of nodes with a single starting point – called the root of the tree (root node) • Each node is connected by an edge to another node • A tree is a connected graph – There is a path to every node in the tree – A tree has one less edge than the number of nodes Root February 07, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 8
Tree relationships • Node v is said to be a child of u, and u the parent of v if – There is an edge between the nodes u and v, and – u is above v in the tree, root A edge B C Parent of B, C, D D • This relationship can be generalized – – E and F are descendants of A D and A are ancestors of G B, C and D are siblings F and G are? February 07, 2020 E Cinda Heeren / Andy Roth / Geoffrey Tien F G 9
More tree terminology • A leaf is a node with no children • A path is a sequence of nodes v 1 … vn – where vi is a parent of vi+1 (1 i n) • A subtree is any node in the tree along with all of its descendants • A binary tree is a tree with at most two children per node – The children are referred to as left and right – We can also refer to left and right subtrees February 07, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 10
Tree terminology example A B A path from A to D to G C D A subtree rooted at B E F G C, E, F, G are leaf nodes February 07, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 11
Binary tree terminology A B Left subtree of A D C E F H February 07, 2020 G I Cinda Heeren / Andy Roth / Geoffrey Tien Right child of A Right subtree of C J 12
Measuring trees • The height of a node v is the length of the longest path from v to a leaf – The height of the tree is the height of the root • The depth of a node v is the length of the path from v to the root – This is also referred to as the level of a node • Note that there is a slightly different formulation of the height of a tree – Where the height of a tree is said to be the number of different levels of nodes in the tree (including the root) February 07, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 13
Tree measurements explained A Height of tree is 3 B Height of node B is 2 D Level 1 C E F Level 2 G Depth of node E is 2 H February 07, 2020 I Cinda Heeren / Andy Roth / Geoffrey Tien J Level 3 14
Perfect binary trees • A binary tree is perfect, if – No node has only one child – And all the leaves have the same depth • A perfect binary tree of height h has – 2 h+1 – 1 nodes, of which 2 h are leaves • Perfect trees are also complete February 07, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 15
Height of a perfect tree • 01 12 11 22 21 31 February 07, 2020 32 33 24 23 34 35 36 Cinda Heeren / Andy Roth / Geoffrey Tien 37 38 16
Complete binary trees It's not quite perfect, but almost • A binary tree is complete if A – The leaves are on at most two different levels, – The second to bottom level is completely filled in and – The leaves on the bottom level are as far to the left as possible B D February 07, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien C E F 17
Readings for this lesson • Carrano & Henry – Chapter 2 (Recursion) – Carrano & Henry: Chapter 15. 1 (Tree terminology) • Next class: – Carrano & Henry: Chapter 15. 1 – 15. 2 (binary trees & traversals) February 07, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 18
- Slides: 18