SEARCH TREES

Decode GTIN - 13 In JavaUsing Barcode Control SDK for Java Control to generate, create, read, scan barcode image in Java applications.

Printing EAN / UCC - 13 In JavaUsing Barcode creation for Java Control to generate, create GTIN - 13 image in Java applications.

Figure 12.15 Binary trees

EAN-13 Supplement 5 Reader In JavaUsing Barcode reader for Java Control to read, scan read, scan image in Java applications.

Drawing Barcode In JavaUsing Barcode maker for Java Control to generate, create bar code image in Java applications.

Answers to Review Questions

Recognize Barcode In JavaUsing Barcode scanner for Java Control to read, scan read, scan image in Java applications.

Make EAN 13 In Visual C#Using Barcode generator for .NET framework Control to generate, create EAN13 image in Visual Studio .NET applications.

12.1 The disadvantage of a binary search tree is that it may become very unbalanced, in which case searching degenerates into an O(n) algorithm. The advantage is the efficiency that a binary tree provides for insertions and deletions. The advantage of an AVL tree is that it is always balanced, guaranteeing the O(lgn) speed of the binary search algorithm. The disadvantages the complex rotations used by the insertion and removal algorithms needed to maintain the tree s balance.

Encoding EAN13 In VS .NETUsing Barcode maker for ASP.NET Control to generate, create GTIN - 13 image in ASP.NET applications.

European Article Number 13 Creation In Visual Studio .NETUsing Barcode encoder for Visual Studio .NET Control to generate, create EAN13 image in VS .NET applications.

Solutions to Problems

Generate GS1 - 13 In VB.NETUsing Barcode generation for .NET framework Control to generate, create EAN 13 image in Visual Studio .NET applications.

Linear Generator In JavaUsing Barcode drawer for Java Control to generate, create 1D image in Java applications.

12.1 To insert a new record with key 16 into the tree shown in Figure 12.16, the initial search would lead to the first leaf node. Since that is a five-way search tree, that first leaf node has overflowed, causing it to be split into two leaf nodes and moving its middle key 19 up to its parent node, as shown in Figure 12.17. But now that parent node has overflowed. So it also gets split, moving its middle key up to its parent node, as shown in Figure 12.18. Two other ordering of the seven keys in Example 12.5 on page 235 that will produce the same BST: a. 44, 22, 33, 77, 55, 99, 88 b. 44, 22, 77, 33, 55, 99, 88 An array of objects could be sorted by inserting their objects into a binary search tree and then using an inorder traversal to copy them back into the array. The BST property guarantees that the inorder traversal will visit the elements in order. If an AVL tree is used, then each insertion runs in O(lgn) time, so building the tree with n elements will require O(n lgn) time. The subsequent inorder traversal also has O(n lgn) complexity, so the entire algorithm sorts the array in O(n lgn) time. All except a are binary search trees.

Generating GTIN - 12 In JavaUsing Barcode maker for Java Control to generate, create UPC-A Supplement 5 image in Java applications.

Barcode Generator In JavaUsing Barcode generation for Java Control to generate, create barcode image in Java applications.

SEARCH TREES

Bookland EAN Drawer In JavaUsing Barcode maker for Java Control to generate, create ISBN - 10 image in Java applications.

Create Universal Product Code Version A In Visual C#Using Barcode creator for .NET Control to generate, create UPCA image in .NET framework applications.

[CHAP. 12

Barcode Creation In JavaUsing Barcode drawer for BIRT reports Control to generate, create barcode image in BIRT reports applications.

UPCA Generation In Objective-CUsing Barcode encoder for iPhone Control to generate, create UPC Symbol image in iPhone applications.

Figure 12.16 Inserting the key 16 in a five-way search tree

Decoding EAN / UCC - 13 In Visual Basic .NETUsing Barcode recognizer for VS .NET Control to read, scan read, scan image in .NET framework applications.

UPC-A Supplement 2 Creator In NoneUsing Barcode generator for Word Control to generate, create UPC A image in Word applications.

Figure 12.17 Inserting the key 16 in a five-way search tree

EAN13 Encoder In JavaUsing Barcode drawer for BIRT reports Control to generate, create EAN 13 image in BIRT applications.

Draw Bar Code In JavaUsing Barcode generation for Android Control to generate, create bar code image in Android applications.

Figure 12.18 Inserting the key 16 in a five-way search tree

private AVLTree rotateRight() { AVLTree x = this, y = left, z = y.left; x.left = z; y.left = x; int xb = x.balance; int yb = y.balance;

CHAP. 12]

SEARCH TREES

if (yb > 1) { ++x.balance; y.balance = ( xb<0 yb+1 : xb+yb+2 ); } else if (yb > xb) { x.balance += yb-1; ++y.balance; } else { y.balance = xb+2; } return y; }

Theorem. Every subtree of a binary search tree is a binary search tree. Proof: Let T be a binary search tree, and let S be a subtree of T. Let x be any element in S, and let L and R be the left and right subtrees of x in S. Then, since S is a subtree of T, x is also an element of T, and L and R are the left and right subtrees of x in T. Therefore, y x z for every y L and every z R because T has the BST property. Thus, S also has the BST property.

Theorem. Every subtree of an AVL tree is an AVL tree. Proof: The proof that every subtree of a binary search tree is a binary search tree is given in Problem 12.6. If a S is a subtree of an AVL tree T, then every node is S is also in T. Therefore, the balance number at every node in S is 1, 0, or 1.

The solution is shown in Figure 12.19.

Figure 12.19 AVL tree insertions

SEARCH TREES

[CHAP. 12

Figure 12.19 (continued) AVL tree insertions

Heaps and Priority Queues

HEAPS A heap is a complete binary tree whose elements have keys that satisfy the following heap property: the keys along any path from root to leaf are descending (i.e., nonincreasing). EXAMPLE 13.1 A Heap

Figure 13.1 shows a heap. Note that the keys along each of its root-to-leaf paths are descending: 77 66 44 22; 77 66 44 41; 77 66 60 58; 77 66 60 25; 77 55 33 29; 77 55 55.