This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: CSCI-1200 Data Structures — Fall 2010 Lecture 18 – Trees, Part III Review from Lecture 17 • Implementing ds set operations: insert, destroy, printing, erase Today’s Lecture • Adding parent pointers, insert & erase w/ parent pointers • Increment and decrement operations on iterators • Limitations of our implementation, brief intro to red-black trees 18.1 Tree Height, Longest & Shortest Paths • (From last lecture... ) The height of a node in a tree is the length of the longest path down the tree from that node to a leaf node. The height of a leaf is therefore 0. We will think of the height of a null pointer as -1. • The height of the tree is the height of the root node, and therefore if the tree is empty the height will be -1. • An exercise from last lecture was to write a simple recursive algorithm to calculate the height of a tree. Let’s write it now. What is the running time of this algorithm? • Now let’s write a function to calculate the shortest path to a NULL child pointer. What is the running time of this algorithm? Can we do better? 18.2 Tree Iterators (review and with more detail) • The increment operator should change the iterator’s pointer to point to the next TreeNode in an in-order traversal — the “in-order successor” — while the decrement operator should change the iterator’s pointer to point to the “in-order predecessor”. • Unlike the situation with lists and vectors, these predecessors and successors are not necessarily “nearby” (either in physical memory or by following a link) in the tree, as examples we draw in class will illustrate. • There are two common solution approaches: – Each iterator maintains a stack of pointers representing the path down the tree to the current node. – Each node stores a parent pointer. Only the root node has a null parent pointer. • Let’s go with the parent pointer method. Most of the changes are simple, but we need to revise the implemen- tation of several member functions, including insert and erase , to correctly adjust parent pointers. See the attached source code. 18.3 Erase (now with parent pointers) • (Review from last lecture... ) First we need to find the node to remove.(Review from last lecture....
View Full Document
- Fall '08
- Data Structures, Pointer, Red-black tree, NULL NULL NULL