Docsity

Tree Height - Data Structures - Lecture Notes | CSCI 1200, Study notes of Data Structures and Algorithms

Toggle sidebar
bg1
CSCI-1200 Computer Science II — Fall 2007
Lecture 18 – Trees, Part III
Review from Lecture 17
•implementing cs2set operations: insert, destroy, printing, erase (partial)
Today’s Lecture
•Tree height, (finish) cs2set implementation of erase
•Increment and decrement operations on iterators
•Limitations of our implementation
18.1 Height and Height Calculation Algorithm
•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.
18.2 Exercise
Write a simple recursive algorithm to calculate the height of a tree. What is the running time of this algorithm?
18.3 Erase
•First we need to find the node to remove. Once it is found, the
actual removal is easy if the node has no children or only one child.
It is harder if there are two children:
–Find the node with the greatest value in the left subtree or the
node with the smallest value in the right subtree.
–The value in this node may be safely moved into the current node
because of the tree ordering.
–Then we recursively apply erase to remove that node — which is
guaranteed to have at most one child.
n−r
a−f
giraffe snake
mouse
t−za−f
giraffe snake
lion
h−l
h−k
n−r t−z
18.4 Exercise
Write a recursive version of erase. What is the worst case
running time of this algorithm?
pf3
pf4

Partial preview of the text

Download Tree Height - Data Structures - Lecture Notes | CSCI 1200 and more Study notes Data Structures and Algorithms in PDF only on Docsity!

CSCI-1200 Computer Science II — Fall 2007

Lecture 18 – Trees, Part III

Review from Lecture 17

  • implementing cs2set operations: insert, destroy, printing, erase (partial)

Today’s Lecture

  • Tree height, (finish) cs2set implementation of erase
  • Increment and decrement operations on iterators
  • Limitations of our implementation

18.1 Height and Height Calculation Algorithm

  • 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.

18.2 Exercise

Write a simple recursive algorithm to calculate the height of a tree. What is the running time of this algorithm?

18.3 Erase

  • First we need to find the node to remove. Once it is found, the

actual removal is easy if the node has no children or only one child.

It is harder if there are two children:

  • Find the node with the greatest value in the left subtree or the

node with the smallest value in the right subtree.

  • The value in this node may be safely moved into the current node

because of the tree ordering.

  • Then we recursively apply erase to remove that node — which is

guaranteed to have at most one child.

n−r

a−f

giraffe snake

mouse

a−f t−z

giraffe snake

lion

h−l

h−k

n−r t−z

18.4 Exercise

Write a recursive version of erase. What is the worst case

running time of this algorithm?

18.5 Tree Iterators

  • 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 rewrite the insert

and erase member functions to correctly adjust parent pointers. See the attached source code.

18.6 Exercise

  • Although iterator increment looks expensive in the worst case for a single application of operator++, it is fairly

easy to show that iterating through a tree storing n nodes requires O(n) operations overall.

  • Implement an algorithm for finding the in-order successor of a node. Use this to implement the iterator

increment function.

18.7 Limitations of Our BST Implementation

  • The efficiency of the main insert, find and erase algorithms depends on the height of the tree.
  • The best-case and average-case heights of a binary search tree storing n nodes are both O(log n). The worst-

case, which often can happen in practice, is O(n).

  • Developing more sophisticated algorithms to avoid the worst-case behavior will be covered in Data Structures

and Algorithms.

18.8 Red-Black Trees

In addition to the binary search tree properties, the

following red-black tree properties are maintained

throughout all modifications to the data structure:

1. Each node is either red or black.

2. The NULL child pointers are black.

3. Both children of every red node are black.

Thus, the parent of a red node must also be black.

4. All paths from a particular node to a NULL child pointer

contain the same number of black nodes.

NULL

NULL

NULL

NULL

NULL

NULL

NULL

NULL

NULL

NULL

NULL NULL

NULL

NULL

NULL

14

1 3

8

4

6

5 7 9

10

12

11 13

2

What tree does our cs2set implementation produce if we insert the numbers 1-14 in order?

The tree at the right is the result using a red-black tree. Notice how the tree is still quite balanced.

Visit this link for an animation of the sequential insertion and rebalancing:

http://babbage.clarku.edu/∼achou/cs160fall03/examples/bst animation/RedBlackTree-Example.html

// MAKE SURE THE DATA STRUCTURE’S CHILD & PARENT POINTERS ARE CONSISTENT

bool sanity_check() const { if (root_ == NULL) return true; if (root_->parent != NULL) return false; return sanity_check(root_); } iterator begin() const { if (!root_) return iterator(NULL); TreeNode* p = root_; while (p->left) p = p->left; return iterator(p); } iterator end() const { return iterator(NULL); }

private: // REPRESENTATION TreeNode* root_; int size_; // now with parent pointers, need to pass the current node as the parent pointer for the child TreeNode* copy_tree(TreeNode* old_root, TreeNode* the_parent) { if (old_root == NULL) return NULL; TreeNode answer = new TreeNode(); answer->value = old_root->value; answer->left = copy_tree(old_root->left,answer); answer->right = copy_tree(old_root->right,answer); answer->parent = the_parent; // link up the parent return answer; } void destroy_tree(TreeNode p) { /* implementation omitted / } iterator find(const T& key_value, TreeNode p) { /* implementation omitted / } // now with parent pointers, need to pass the current node as the parent pointer for the child std::pair<iterator,bool> insert(const T& key_value, TreeNode& p, TreeNode* the_parent) { if (!p) { p = new TreeNode(key_value); p->parent = the_parent; // link up the parent this->size_++; return std::pair<iterator,bool>(iterator(p), true); } else if (key_value < p->value) return insert(key_value, p->left, p); else if (key_value > p->value) return insert(key_value, p->right, p); else return std::pair<iterator,bool>(iterator(p), false); }

int erase(T const& key_value, TreeNode* &p) { /* Implemented in Lecture 18 */

bool sanity_check(TreeNode* p) const { if (p == NULL) return true; if (p->left != NULL && p->left->parent != p) { return false; } if (p->right != NULL && p->right->parent != p) { return false; } return sanity_check(p->left) && sanity_check(p->right); } };