Algorithms Chapter 21 Algorithms Sequential Parallel And Distributed Balanced Search Trees Glance Table Contents

subject Type Homework Help
subject Pages 9
subject Words 1914
subject Authors Jerome L. Paul, Kenneth A. Berman

Unlock document.

This document is partially blurred.
Unlock all pages and 1 million more documents.
Get Access
Algorithms: Sequential, Parallel and Distributed 1-1
Chapter 21
Balanced Search Trees
At a Glance
Table of Contents
Overview
Objectives
Instructor Notes
Solutions to Selected Exercises
page-pf2
Algorithms: Sequential, Parallel and Distributed 1-2
Lecture Notes
Overview
In Chapter 21 we introduce the notion of a balanced binary search tree, and show how rotations
in such trees can be used to help restore balance after nodes have been inserted or deleted. We
define the notion of an AVL tree and a red-black tree, and prove that the depth of a red-black
tree is logarithmic in the number of nodes in the tree. We then discuss algorithms for inserting
and deleting nodes in a red-black tree, including giving transition diagrams that show how
these algorithms proceed through the various cases until balance is restored. We finish the
chapter with a discussion of the B-tree multiway search tree, which is a useful ADT for
maintaining externally stored databases.
Chapter Objectives
After completing the chapter the student will know:
The definitions of AVL and red-black trees
The notion of a rotation in a binary search tree, and its role in restoring balance in AVL and
red-black trees
Instructor Notes
The algorithms for inserting and deleting elements in a red-black tree are straightforward, but
cumbersome in detail. Certainly one would not expect the student (or the instructor!) to
memorize the various cases and what procedures are followed depending on these cases.
page-pf3
Algorithms: Sequential, Parallel and Distributed 1-3
Solutions to Selected Exercises
Section 21.2 Rotations of Binary Search Trees
21.1
function CreateBinSrchTree(i, j) recursive
Input: i, j (integers between 0 and n 1, inclusive)
Output: creates a binary search tree for the keys Ki, Ki+1, …, Kj and returns a pointer to the root
21.2 Except for whatever is needed to assign information to the field Info, only three
statements need to be added to the pseudocode for InsertBinSrchTree given on p. 138 in the
21.3 That an inorder traversal of a binary search tree T visits the nodes in increasing order
of the keys is shown in the solution to Exercise 4.36 of Chapter 4. Thus, we only need show
that if the inorder traversal visits the nodes of the binary tree T in increasing order of the keys,
page-pf4
21.4
procedure RotateLeft(Root,Node)
Input: Root (BinaryTreeNode) //points at root of a binary tree T
Node (BinaryTreeNode) //points at node where rotation takes
//place
21.5 We prove that the depth d(T) of any AVL-tree on n nodes belongs to O(logn) by proving
the following proposition.
Proposition Let d = d(T) be the depth of an AVL tree T. Then each level of T is full for all
levels ≤ d/4.
page-pf5
21.6 There are a number of cases that occur when an insertion of an element into an AVL tree
T destroys the AVL property. Suppose that T* is the smallest subtree (that, is, rooted at the
deepest level) where the insertion violates the AVL property of T*. A straightforward case-by-
case analysis shows that a single rotation of T* will restore the AVL property of T*, and at the
same time restore the AVL property of the entire tree, except for two mirror-image cases, as
page-pf6
page-pf7
Algorithms: Sequential, Parallel and Distributed 1-7
Section 21.3 Red-Black Trees
21.7 Consider any two paths P1 and P2 from the root to leaf nodes in the red-black tree. Then
P1 and P2 have the same number b of black nodes. Let r1 and r2 be the number of red nodes in
21.9 In the pseudocode for RBCorrect(iii)OrMoveUpL, we assume that the parameter Node in
the call to DeleteBinSrch2 is an input-output parameter, so that after the call it is the node
actually deleted, which will be the inorder successor of the node containing the key to be
deleted in case the latter node has two non-null children. Also, DeleteBinSrch2 has added the
output parameter CurNode to the parameter list of DeleteBinSrch, where CurNode is the
page-pf8
.and. SRCColor = black then //Case 1
ParentColor red
SiblingColor black
RotateLeft(Root, Parent)
//Case 3
SiblingColor red
if SiblingColor = black .and. SRCColor = red then //Case 4
SRCColor black
21.10 a. We only consider the unprimed cases, as the primed cases are similar. Our arguments
are based on Figure 21.9, which while it uses specific keys for ease of discussion, nevertheless
page-pf9
Case 1. Note that the number of black nodes of on paths passing through node 23 and going on
to either node 17 or node 55 not changed after the transformation. Note also that a path going
child 5. After the transformation, all paths going through 12 are deficient, so that the violation
Case 2b. After the transformation, all deficient paths add one black node (that is, they are no
Case 3. A simple check shows that all non-deficient paths have the same number of black
Case 4. (Deficient) paths going through 5 pick up one black node after the transformation, so
they are no longer deficient. A simple check shows that all non-deficient paths have the same
number of black nodes (that is, remain non-deficient) after the transformation. Since no other
violations of the red-black properties are introduced, the red-black property is now restored.
b.
if NodeLeftChild Sentinel .and. (NodeLeftChild)Color = red then
(NodeLeftChild)Color = black
return
endif
if NodeRightChild Sentinel .and. (NodeRightChild)Color = red then
page-pfa
21.11
page-pfb
page-pfc
page-pfd
21.12
page-pfe
page-pff

Trusted by Thousands of
Students

Here are what students say about us.

Copyright ©2022 All rights reserved. | CoursePaper is not sponsored or endorsed by any college or university.