chapter 11- binary search trees

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

A new BST is built by inserting nodes in this order:6 2 8What is the tree height? (Remember, the root is at height 0)

1 The tree is 6 (2, 8). Longest path to a leaf is 1.

A new BST is built by inserting nodes in this order:20 12 23 18 30What is the tree height?

2 The tree is 20 (12 (-, 18), 23 (-, 30)). Longest path to a leaf is 2.

A new BST is built by inserting 255 nodes in sorted order. What is the tree height?

254 The tree basically becomes a list, with every node having just one child.

Complete the tree traversal after node 300's left subtree has been printed. 11 25 100 201

300 750 775 925 After printing a node's a left subtree, the algorithm prints the current node, followed by the right subtree.

A new BST is built by inserting nodes in this order:30 11 23 21 20What is the tree height?

4 30's left child is 11. 11's right child is 23. 23's left child is 21. Etc. The nodes need not be perfectly sorted to obtain a large height.

A tree traversal algorithm visits all nodes in the tree once and performs an operation on each node.

An inorder traversal visits all nodes in a BST from smallest to largest, which is useful for example to print the tree's nodes in sorted order. Starting from the root, the algorithm recursively prints the left subtree, the current node, and the right subtree.

In a BST without parent pointers, a search for a node's parent can be implemented recursively. The algorithm recursively searches for a parent in a way similar to the normal BSTSearch algorithm.

But instead of comparing a search key against a candidate node's key, the node is compared against a candidate parent's child pointers.

BSTReplaceChild will not work if the parent pointer is null

At the beginning of BSTReplaceChild, the parent's left child is accessed. The parent parameter must be non-null.

What is the worst case (largest) number of comparisons given a BST with N nodes? Perfect BST with N = 7 ⌊log2N⌋ ⌊log2N⌋+1 N

B-- In the worst case, searching a perfect BST requires (height of BST + 1) comparisons.⌊log2(7)⌋+1=3

An especially useful form of binary tree is a binary search tree (BST), which has an ordering property that any node's left subtree keys ≤ the node's key, and the right subtree's keys ≥ the node's key. That property enables fast searching for an item

BST ordering property: For three nodes, left child is less-than-or-equal-to parent, parent is less-than-or-equal-to right child. For more nodes, all keys in subtrees must satisfy the property, for every node

BSTRemoveKey uses BSTRemoveNode

BSTRemoveKey finds the node with the key, then calls BSTRemoveNode to remove the node

A binary tree's height can be minimized by keeping all levels full, except possibly the last level.

Such an "all-but-last-level-full" binary tree's height is h = log2N

A new BST is built by inserting nodes in this order:30 23 21 20 18What is the tree height?

4 The tree is just a list of left children. Longest path is to leaf 18, with length 4. Note that sorted ascending or descending is equally bad

What is the worst case (largest) number of comparisons given a BST with N nodes? Perfect BST with N = 31 31 4 5

5-- In the worst case, searching a perfect BST requires height + 1 comparisons.

A new BST is built by inserting 255 nodes in random order. What is the minimum possible tree height?

7 A tree with all levels full (except perhaps the last level) has heighth=⌊log2255⌋=⌊7.994⌋=7. The minimum height occurs if all levels are full. 255 nodes would have levels filled as: 1, 2, 4, 8, 16, 32, 64, and 128, summing to 255. Although one sees 8 levels, the height is 7 because the root is at height 0 (hence the floor operation).

To search nodes means to find a node with a desired key, if such a node exists

A BST may yield faster searches than a list. Searching a BST starts by visiting the root node

A BST defines an ordering among nodes, from smallest to largest. A BST node's successor is the node that comes after in the BST ordering, so in A B C, A's successor is B, and B's successor is C.

A BST node's predecessor is the node that comes before in the BST ordering.

The BST remove algorithm traverses the tree from the root to find the node to remove. When the node being removed has 2 children, the node's successor is found and a recursive call is made. One node is visited per level, and in the worst case scenario the tree is traversed twice from the root to a leaf.

A BST with N nodes has at least log2N levels and at most N levels. Therefore, the runtime complexity of removal is best case O(logN) and worst case O(N). Two pointers are used to traverse the tree during removal. When the node being removed has 2 children, a third pointer and a copy of one node's data are also used, and one recursive call is made. Thus, the space complexity of removal is always O(1).

A perfect binary tree has height ⌊log2N⌋.

A perfect binary tree search is O(H), so O(logN). Searching a BST may be faster than searching a list.

Using left, current, and right, what ordering will print the BST from largest to smallest? Ex: An inorder traversal uses left current right

A reverse inorder traversal visits nodes from largest to smallest. The resulting order would be: 925 775 750 300 201 100 25 11.

Given a new node, a BST insert operation inserts the new node in a proper location obeying the BST ordering property. A

A simple BST insert algorithm compares the new node with the current node (initially the root).

Given a key, a search algorithm returns the first node found matching that key, or returns null if a matching node is not found.

A simple BST search algorithm checks the current node (initially the tree's root), returning that node as a match, else assigning the current node with the left (if key is less) or right (if key is greater) child and repeating. If such a child is null, the algorithm returns null (matching node not found)

How many nodes are visited?

A tree traversal visits all nodes in the tree.

A major BST benefit is that an N-node binary tree's height may be as small as O(logN), yielding extremely fast searches

Ex: A 10,000 node list may require 10,000 comparisons, but a 10,000 node BST may require only 14 comparisons.

Searching a BST in the worst case requires H + 1 comparisons, meaning O(H) comparisons, where H is the tree height.

Ex: A tree with a root node and one child has height 1; the worst case visits the root and the child: 1 + 1 = 2.

Remove an internal node with single child

If X has a parent (so X is not the root), the parent's left or right child (whichever points to X) is assigned with X's single child. Else, if X was the root, the root pointer is assigned with X's single child.

Remove a leaf node:

If X has a parent (so X is not the root), the parent's left or right child (whichever points to X) is assigned with null. Else, if X was the root, the root pointer is assigned with null, and the BST is now empty

If a node has a right subtree, the node's successor is that right subtree's leftmost child: Starting from the right subtree's root, follow left children until reaching a node with no left child (may be that subtree's root itself).

If a node doesn't have a right subtree, the node's successor is the first ancestor having this node in a left subtree. Another section provides an algorithm for printing a BST's nodes in order

Given a key, a BST remove operation removes the first-found matching node, restructuring the tree to preserve the BST ordering property. The algorithm first searches for a matching node just like the search algorithm.

If found (call this node X), the algorithm performs one of the following sub-algorithms:

Inserting in random order naturally keeps tree height near the minimum, in this case 3 (minimum: 2) Inserting in sorted order yields the maximum height, in this case 6.

If nodes are given beforehand, randomizing the ordering before inserting keeps tree height near minimum

BST search algorithm checks current node, returning a match if found. Otherwise, assigns current node with left (if key is less) or right (if key is greater) child and continues search

If the child to be visited does not exist, the algorithm returns null indicating no match found.

Search for insert location:

If the left (or right) child is not null, the algorithm assigns the current node with that child and continues searching for a proper insert location.

Insert as right child:

If the new node's key is greater than the current node, and the current node's right child is null, the algorithm assigns the node's right child with the new node.

Insert as left child

If the new node's key is less than the current node, and the current node's left child is null, the algorithm assigns that node's left child with the new node.

Given a node representing a BST subtree, the height can be computed as follows:

If the node is null, return -1. Otherwise recursively compute the left and right child subtree heights, and return 1 plus the greater of the 2 child subtrees' heights.

Searching a BST is fast if the tree's height is near the minimum. Inserting items in random order naturally keeps a BST's height near the minimum.

In contrast, inserting items in nearly-sorted order leads to a nearly-maximum tree height.

What node is printed first?

Inorder traversal prints the node with smallest key first

Searching a 7-node list may require up to 7 comparisons. In a BST, if desired key equals current node's key, return found. If less, descend to left child. If greater, descend to right child.

Searching a BST may require fewer comparisons, in this case 3 vs. 7

An inorder traversals starts at the root. Recursive call descends into left subtree. When left done, current is printed, then recursively descend into right subtree.

Return from recursive call causes ascending back up the tree; left is done, so do current and right. Continues similarly

A BST implementation often includes a parent pointer inside each node. A balanced BST, such as an AVL tree or red-black tree, may utilize the parent pointer to traverse up the tree from a particular node to find a node's parent, grandparent, or siblings.

The BST insertion and removal algorithms below insert or remove nodes in a BST with nodes containing parent pointers.

A node inserted into an empty tree will become the tree's root.

The BST is searched to find a suitable location to insert the new node as a leaf node.

Recall that a tree's height is the maximum edges from the root to any leaf. (Thus, a one-node tree has height 0.)

The minimum N-node binary tree height is h=⌊log2N⌋, achieved when each level is full except possibly the last. The maximum N-node binary tree height is N - 1 (the - 1 is because the root is at height 0).

BST insertion and removal can also be implemented using recursion. The insertion algorithm uses recursion to traverse down the tree until the insertion location is found.

The removal algorithm uses the recursive search functions to find the node and the node's parent, then removes the node from the tree. If the node to remove is an internal node with 2 children, the node's successor is recursively removed.

BST search can be implemented using recursion. A single node and search key are passed as arguments to the recursive search function. Two base cases exist. The first base case is when the node is null, in which case null is returned. If the node is non-null, then the search key is compared to the node's key.

The second base case is when the search key equals the node's key, in which case the node is returned. If the search key is less than the node's key, a recursive call is made on the node's left child. If the search key is greater than the node's key, a recursive call is made on the node's right child.

The BST insert algorithm traverses the tree from the root to a leaf node to find the insertion location. One node is visited per level. A BST with N nodes has at least log2N levels and at most N levels. Therefore, the runtime complexity of insertion is best case O(logN) and worst case O(N).

The space complexity of insertion is O(1) because only a single pointer is used to traverse the tree to find the insertion location.

What is the worst case (largest) number of comparisons given a BST with N nodes?

The worst case number of comparisons is the height + 1, which is 4 + 1 = 5. The height can be large for a non-perfect tree.

Remove an internal node with two children:

This case is the hardest. First, the algorithm locates X's successor (the leftmost child of X's right subtree), and copies the successor to X. Then, the algorithm recursively removes the successor from the right subtree

If a child to be visited doesn't exist, the desired node does not exist

With this approach, only a small fraction of nodes need be compared

950's successor is 150

false-- 950 is the largest BST item, so has no successor. The BST's largest item is the root's rightmost child: Follow right children until reaching a node with no right child (may be the root itself).

BSTInsert will not work if the tree's root is null.

false-- At the beginning of BSTInsert, a null root node is handled as a special case. The node will be inserted as the tree's root.

The worst-case time complexity for BSTGetHeight is O(log N), where N is the number of nodes in the tree.

false-- BSTGetHeight is called for every node in the tree, so the time complexity is O(N), not O(log N).

BSTGetParent always returns a non-null node when searching for a null node.

false-- BSTGetParentRecursive cannot be used to search for a null node. The node's key is accessed in the function, so the function may fail if the node is null.

BSTRemoveKey will not work if the key is not in the tree.

false-- For a key not in the tree, BSTSearch returns null. The null node is then passed to BSTRemoveNode, which returns after the first if statement. The tree is not altered.

BSTRemoveNode will not work to remove the last node in a tree.

false-- The last node in a tree is the root, and the case (node == tree⇢root) is encountered. The tree's root pointer is set to the root's right child, which is null, thus clearing the tree.

500's successor is 850

false-- The next larger item is 750. For a node with a right subtree, the successor is the leftmost child of the right subtree.

The first node in the BST ordering is 150.

true-- 150 is the smallest item in the BST. The BST's first node is the leftmost child of the root: Follow left children until reaching a node with no left child (may be the root itself)

150's successor is 250.

true-- 250 is the next-larger item. If a node has a right subtree, the successor is the leftmost child of that subtree

950's predecessor is 900.

true-- 900's successor is 950, so 950's predecessor is 900.

BSTGetHeight returns 0 for a tree with a single node

true-- A tree's height is the maximum edges from the root to any leaf. A tree with 1 node has 0 edges and therefore a height of 0.

250's successor is 300.

true-- If a node has no right child, the successor will be the first ancestor having this node in a left subtree. A node's successor need not involve a direct link.

BSTGetParent returns null when the node argument is the tree's root.

true-- The first call to BSTGetParentRecursive compares the root against the root's children, not finding a match. Recursive calls are made to search the remaining tree levels, ultimately not finding a parent and returning null

BSTGetHeight would also work if the recursive call on the right child was made before the recursive call on the left child.

true-- The order of the two recursive calls does not matter. As long as the 2 recursive calls are made and the greater of the 2 returned heights is used, BSTGetHeight will properly determine the tree height.

The base case for BSTGetHeight is when the node argument is null.

true-- When the node argument is null, BSTGetHeight returns -1 and does not make any recursive calls.

The base case for BSTGetParentRecursive is when subtreeRoot is null or is node's parent.

true-- f subtreeRoot is null or node's parent, the function returns without making a recursive call.


Ensembles d'études connexes

Chapter 4, Documentation for Statistical Reporting and Public Health

View Set

Chapter 17: Insurance and Billing

View Set

LS 4: Adjustments, Financial Statements, and Financial Results

View Set