Java Software Structures:: Chapter 11: Binary Search Trees

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

(SR 11.1) What is the difference between a binary tree and a binary search tree?

(SRA 11.1) A binary search tree has the added ordering property that the left child of any node is less than the node, and the node is less than or equal to its right child.

(Term) balance factor

A property of a node that is computed by subtracting the height of the left subtree from the height of the right subtree. If the result is either greater than 1 or less than -1, then the tree is unbalanced.

(Term) right rotation

A single rotation strategy for rebalancing a tree when the long path is in the left subtree of the left child of the root.

(Term) left rotation

A single rotation strategy for rebalancing a tree when the long path is in the right subtree of the right child of the root.

(Term) red/black trees

A strategy for keeping a binary search tree balanced that makes use of a color (either red or black) associated with each node.

(Term) AVL trees

A strategy for keeping a binary search tree balanced that makes use of the balance factor of each node.

(Term) promoted

A term used to describe the concept of a node in a tree being moved up to replace a parent node or other ancestor node that is being removed from the tree.

(Term) degenerate tree

A tree that does not branch.

(Key Concept) What references does a BinaryTreeNode object maintain?

Each BinaryTreeNode object maintains a reference to the element stored at that node, as well as references to each of the node's children.

(Key Concept) What could happen if a binary search tree is not balanced?

If a binary search tree is not balanced, it may be less efficient than a linear structure.

(Key Concept) What must happen when an element in a binary search tree is removed?

In removing an element from a binary search tree, another node must be promoted to replace the node being removed.

(Key Concept) What is one use of a tree?

One of the uses of trees is to provide efficient implementations of other collections.

(Key Concept) Is the balance restriction on red/black trees more or less strict than the balance restriction on AVL trees?

The balance restriction on a red/black tree is somewhat less strict than that for AVL trees. However, in both cases, the find operation is order log n.

(Key Concept) Where do we get our definition of a binary search tree?

The definition of a binary search tree is an extension of the definition of a binary tree.

(Key Concept) What is the height of the right subtree minus the height of the left subtree called?

The height of the right subtree minus the height of the left subtree is called the balance factor of a node.

(Key Concept) Where are minimum and maximum element in a binary search tree?

The leftmost node in a binary search tree will contain the minimum element, whereas the rightmost node will contain the maximum element.

(SR 11.10) What is the time complexity of the addElement operation after modifying to implement an AVL tree?

(SRA 11.10) Keep in mind that an addElement method affects only one path of the tree, which in a balanced AVL tree has a maximum length of log n. As we have discussed previously, finding the position to insert and setting the reference is O(log n). We then have to progress back up the same path, updating the balance factors of each node (if necessary) and rotating if necessary. Updating the balance factors is an O(1) step, and rotation is also an O(1) step. Each of these will have to be done at most log n times. Therefore, addElement has time complexity 2*log n or O(log n).

(SR 11.11) What imbalance is fixed by a single right rotation?

(SRA 11.11) A single right rotation will fix the imbalance if the long path is in the left subtree of the left child of the root.

(SR 11.12) What imbalance is fixed by a leftright rotation?

(SRA 11.12) A leftright rotation will fix the imbalance if the long path is in the right subtree of the left child of the root.

(SR 11.13) What is the balance factor of an AVL tree node?

(SRA 11.13) The balance factor of an AVL tree node is the height of the right subtree minus the height of the left subtree.

(SR 11.14) In our discussion of the process for rebalancing an AVL tree, we never discussed the possibility of the balance factor of a node being either +2 or -2 and the balance factor of one of its children being either +2 or -2. Why not?

(SRA 11.14) Rebalancing an AVL tree is done after either an insertion or a deletion, and it is done starting at the affected node and work- ing up along a single path to the root. As we progress upward, we update the balance factors and rotate if necessary. We will never encounter a situation where both a child and a parent have balance factors of +/-2 because we would have already fixed the child before we ever reached the parent.

(SR 11.15) We noted that the balance restriction for a red/black tree is less strict than that of an AVL tree, and yet we still claim that traversing the longest path in a red/black tree is still O(log n). Why?

(SRA 11.15) Because no red node can have a red child, at most half of the nodes in a path could be red nodes, and at least half of the nodes in a path are black. From this we can argue that the maximum height of a red/black tree is roughly 2*log n, and thus the traversal of the longest path is O(log n).

(SR 11.2) Why are we able to specify addElement and removeElement operations for a binary search tree but unable to do so for a binary tree?

(SRA 11.2) With the added ordering property of a binary search tree, we are now able to define what the state of the tree should be after an add or remove. We were unable to define that state for a binary tree.

(SR 11.3) Assuming that the tree is balanced, what is the time complexity (order) of the addElement operation?

(SRA 11.3) If the tree is balanced, finding the insertion point for the new element will take at worst log n steps, and since inserting the element is simply a matter of setting the value of one reference, the operation is O(log n).

(SR 11.4) Without the balance assumption, what is the time complexity (order) of the addElement operation?

(SRA 11.4) Without the balance assumption, the worst case would be a degenerate tree, which is effectively a linked list. Therefore, the addElement operation would be O(n).

(SR 11.5) As stated in this chapter, a degenerate tree might actually be less efficient than a linked list. Why?

(SRA 11.5) A degenerate tree will waste space with unused references, and many of the algorithms will check for null references before following the degenerate path, thus adding steps that the linked list implementation does not have.

(SR 11.6) Our removeElement operation uses the inorder successor as the replacement for a node with two children. What would be an- other reasonable choice for the replacement?

(SRA 11.6) The best choice is the inorder successor because we are placing equal values to the right.

(SR 11.7) The removeAllOccurrences operation uses both the contains operation and the removeElement operation. What is the result- ing time complexity (order) for this operation?

(SRA 11.7) With our balance assumption, the contains operation uses the find operation, which will be rewritten in the BinarySearchTree class to take advantage of the ordering property and will be O(log n). The removeElement operation is O(log n). The while loop will iterate some constant (k) number of times, depending on how many times the given element occurs within the tree. The worst case would be that all n elements of the tree were the element to be removed, which would make the tree degenerate, and in which case the complexity would be n*2*n or O(n2). However, the expected case would be some small constant (0 <= k < n) occurrences of the element in a balanced tree, which would result in a complexity of k*2*log n or O(log n).

(SR 11.8) RemoveFirst and first were O(1) operations for our earlier implementation of an ordered list. Why are they less efficient for our BinarySearchTreeOrderedList?

(SRA 11.8) In our earlier linked implementation of an ordered list, we had a reference that kept track of the first element in the list, which made it quite simple to remove it or return it. With a binary search tree, we have to traverse to get to the leftmost element before knowing that we have the first element in the ordered list.

(SR 11.9) Why does the BinarySearchTreeOrderedList class have to define the iterator method? Why can't it just rely on the iterator method of its parent class, as it does for size and isEmpty?

(SRA 11.9) Remember that the iterators for a binary tree are all followed by which traversal order to use. That is why the iterator method for the BinarySearchTreeOrderedList class calls the iteratorInOrder method of the BinaryTree class.

(Key Concept) What is a binary search tree?

A binary search tree is a binary tree with the added property that the left child is less than the parent, which is less than or equal to the right child.

(Term) binary search tree

A binary tree with the added property that, for each node, the left child is less than the parent, which is less than or equal to the right child.

(Term) rightleft rotation

A double rotation strategy for rebalancing a tree when the long path is in the left subtree of the right child of the root.

(Term) leftright rotation

A double rotation strategy for rebalancing a tree when the long path is in the right subtree of the left child of the root.

(Key Concept) What are the ways a tree or subtree can become unbalanced?

There are only two ways in which a tree, or any subtree of a tree, can become unbalanced: through the insertion of a node and through the deletion of a node.


Set pelajaran terkait

Microbial Motility and Chemotaxis

View Set

CE Edge Prep - Property Valuation and Financial Analysis

View Set

Chapter 17: The Enlightenment & the American Revolution

View Set

L'examen partiel: La Littérature Française:

View Set

FUNDAMENTALS OF SURVEYING (LECTURE)

View Set

Fundamentals Chapter 30: Perioperative Nursing 1-4

View Set