Lecture 06: Tree Structures Topics: • Trees in general • Binary Search Trees • Application: Huffman Coding • Other types of Trees Part I. Trees • Linked Lists – add [in-order]: O(n) – remove: O(n) – find: O(n) • Trees: – add [in-order]: O(log n) to O(n) – remove: O(log n) to O(n) – find: O(log n) to O(n) • Try it for these values of n: 4, 8, 1000, 1000000 Basic Idea and terminology • K-ary tree. This is a 3-ary tree • depth. The number of nodes above and including a node. depth(bottomleft)=4, depth(bottom-right)=3 • Child: a node which is immediately reachable from a node. • Descendent: a node which can be reached from a node (regardless of depth) • Sibling. Nodes at the same level. There are 3 siblings all descendent from the root. • Leaf Node: A node without children • Interior Node: A node with at least one child. More terminology • Each node is a subtree. • Full tree: all nodes have 0 or k children. • Perfect Tree: a full tree in which all leaves are at the same depth – Very desirable. – Minimizes depth (makes all operations faster) • Complete tree: the depth of all ancestors differs by at most one. • Degenerate Tree: The opposite of a perfect tree – Really a… – …linked list (that's why the worst case O-speed is O(n)) Part II: Binary Search Trees • A 2-ary tree – 2 children. Usually called left and right. • Often represents sorted data – All children and ancestors in the left branch are less than (or equal) to this node. – All children and ancestors in the right branch are greater than (or equal) to this node. Operations • [Insertions] – [8, 4, 12, 5, 1, 11, 99] – [1, 4, 5, 11, 8, 12, 99] • [Finding] – How many operations to find the 99 in both trees? • [Removing] – With no children – With 1 child – With 2 children • Finding the minimum value in the right sub-tree. • Finding the maximum value in the left sub-tree. • Traversal: Depth-First Search – Pre-order, In-order, Post-order • [Try to code at least some of it] Part III: Huffman Coding • Reference: http://en.wikipedia.org/wiki/Huffman_coding • ASCII encoding of characters • Frequency distribution • Huffman Algorithm – Developed by David Huffman for PhD thesis at MIT (1952) Part IV: Other types of Trees • B+ Trees – Each node is (small) array of values – Siblings point to the next sibling – Used for indexing in Databases (Access, MySQL, Oracle, etc) – Used in many filesystems (NTFS for one) • Red-Black Tree – Self-balancing tree (much more complex insertions and removals) => guaranteed an O(log n) access time. – Applications: • computational geometry (Maya, Blender) • some process schedulers in Linux • Used in some implementation of STL maps (like python dictionaries) • …
© Copyright 2026 Paperzz