α-Balanced Trees,,。
Overview
This project involves three important concepts: sets, balanced binary search trees, and maps.
- A set is a collection of distinct objects.
- A balanced tree represents a set of n elements with a natural ordering so that the running time per operation is O(log n). Depending on the type of balanced tree, this time bound may be worst-case, expected-case, or amortized.
- A map is an object that maps a finite set of keys to a collection of values. Each key can map to at most one value, and a map cannot contain duplicate keys. Maps correspond to the mathematical concept of a function. An example of a map is the function that maps the set of student ID numbers (integers) to student names (strings).
In this project, you will gain a deeper understanding of these concepts by writing the following two classes.
- ABTreeSet: An implementation of sets based on α-balanced trees. Any access or update operation on an n-node α-balanced tree takes O(log n) amortized time.
- ABTreeMap: An implementation of maps that uses α-balanced trees.
We will provide you with templates for ABTreeSet and ABTreeMap. You may add new instance variables and methods to these two classes, but you cannot rename or remove any existing variables or methods, or change any of these variables and methods from public to protected (or private), or vice versa.
Introduction
The time complexities of the basic operations on a binary search tree contains(), add(), and remove() are proportional to the height of the tree. In the ideal case, the height of an n-element tree is at most log2 n. If no precautions are taken, however, the height can be n 1.
There are a number of ways to guarantee that the height of a tree is O(log2 n); they all involve some sort of “rebalancing” after updates, thus these trees are sometimes called self-balancing trees.
Self-balancing trees fall into roughly two categories.
- Height-balanced trees. Here, rebalancing is done to ensure that the heights of the left and right subtrees of any node do not differ by much.
- Weight-balanced trees. Here, rebalancing is done to ensure that the the sizes (numbers of elements) of the left and right subtrees of any node do not differ by much.
Examples of height-balanced trees are AVL-trees,where the heights of the left and right trees at any node differ by at most one, and red-black trees, the heights of the left and right subtrees at any node can differ by a factor of at most two. Red-black trees are used in Java’s implementation of the TreeSet and TreeMap classes. AVL-trees and red-black trees are described in Wikipedia, where you can find links to additional information. We will not discuss height-balanced trees further here.
This assignment deals with a special kind of weight-balanced tree, which we describe next.
Balanced Trees
Let T be a binary search tree and let be a constant. Let α be any node in T, and size be the number of elements in the subtree rooted at x. We say x is α-balanced if
(number of elements in xapos;s left subtree) ≤ α·size, (1)
(number of elements in xapos;s right subtree) ≤ α·size, (2)
We say the tree T is α-balanced if every node is α-balanced.
Simple math shows that the height of an n-node α-balanced tree is at most log1/ n (the idea is to first observe that the size of the subtree rooted at a node at depth k is at most k n, and that the size of a non-empty subtree is at least 1). Since is a constant, this means that contains, add, and remove take logarithmic time. However, adding or removing elements can lead to trees that no longer satisfy the balance conditions (1) and (2). α-balanced trees maintain balance by periodically restructuring entire subtrees, rebuilding them so that they become 21 α-balanced. The work required for rebalancing is O(n) in the worst case, but it can be shown that the amortized time for an add or remove is O(log n). Although a formal proof of this is beyond the scope of CS 228, the intuition is that rebalancing is relatively rare, in the same way that array doubling is rare in the FirstCollection class that we saw several weeks ago.
Next, we explain the rebalancing method used by α-balanced trees, and how rebalancing is done after an update.
The Rebalancing Operation
Suppose x is some node in a BST, and that the subtree rooted at x has k nodes. The rebalancing operation rearranges the structure of a subtree rooted at x so that it has the same keys, but its height is at most log2 k. Rebalancing can be done using an inorder traversal of the subtree rooted at x. As we traverse the tree, we put the nodes, in order, into an array or ArrayList. The midpoint of the array will be the root of the new subtree, where as usual the midpoint is (first + last)/2. All the elements to the left of the midpoint will go into its left child, and all the elements to the right of the midpoint go into the right child. An example is shown in Figure 2. Perhaps the most natural way to construct the tree is to use recur†⁷††⸼ⰽ•‾ ⬼⁵ㅬⰾਊ††††㰼㸾㱧㸠⁴†⡡⥳†㭬㱩⽮㸠ൡ੭†⁸㱴㹧†⁴⁴⁹⡯⥬⸠㱨⽡㸠൴੯†⁰㱦⽯㸠ൡ੮⁹†㱹⼠㹭൰㱮㹷䥨‾†‾ⱳ⁵††⸨㱫⼩㹩൭‼ 㱩㸊䤠†‼⁰†Ⱶ†⁴⁴㱡⽴㹩൴ੳ†⁰㱥㸠䥩† Ⰺ‼ ″•⁂㱥⽲㹰㱴⽥㸾㱳㹯䙲㕬☠㭥⁰†ㅵ㉰Ⱐ⁴⁷♥㬠㵨㉣⽫㌠䙥㙴††⁴⡴⥨⸠⡣⥥⸠㱴⽨㸠൴ੲ㱥㸮⁹㱥⼠㹡൬㱮㸼യੰ‾ ‼㱰㹯䥮⠠⤨‱††䅶䉩⸠㱷⽥㹡൩੮⁴㰠㹲䴠†⁵††⡯⥦Ⱐ⡳⤠Ⱪ䥴⡳⥵††⸠†⡦⤮†⡩ㅳ⤠㱤⽤㸠൯ੲ†㱯㹤䴬⁴⡴⥥Ⱳ⡴⥨ⱥ†䤠⠠⥴†ⱴ††䉴Ⱐ†††Ⱐ⁴⁵†ⱨ†⁰†⡴††⤯㰾⼍㹰ാ㱩⽧㹥ഠ㰠㉬㵴≲ⴠ㉡ⴠ䅴䉲䵩∠㸳㉭㩥䅴䉳䵯㱴⽯㉴㹨ਠ㱡㹤⁹††䅰䉨䴠″Ⱐ⁴⁴♬㭡††††⁴††⸠䑮⁴†⸠⁴†††⁴㭥†⁴‼•⁓•⁔䕥⸾㰍⁰⼾㹙൯ੵ䅲䈠䵴⸠㱣⽳㸠ുੂ㱔㵨∠≳㹱㱵⽯㹴൲㱣㹡ൢੳ⁴㱴㹳ബਠ⁵†…㱬㹡഻ਭ†⁴㱥†㵂≔∠㹣൬†⁰⁴㱳㹳㱥†㵮≴≴㹨ㄠ㱡⼠㹲㱡⽲㹤㱥⽲㸮ഠ⁵⁰⁴†㱭⽥㹳ഠ†⁴†⁷㱥†㵳≯≡㹬൬੯⁷†⁵†㱮㸠㱡⁴㴠≴≤㸠㱡㵥≮∠㹲㱥†㵎≵≥㹲㱩⽯㹨㰠㵣≬≡㹳䄠䉴䵯㱯⽷㹳㱩⁵㵰∾≯㸠⡡⥶㱯⽩㹹㰠⽰㹥㱭⽳㹨㰠⽡㹴㱩⽮㹯൩੮⁴†⁴⁴㰠⽴㹴ഠ⁵†㱳⽥㹲൯੭†⁵㱮⽧㹵ൡ੬㱩⽴㸱ഩਠ㱡⽮㸠നਲ㰩㸠䑷⁷䉯ⴠⵡ†⸠㱧⼠㹩൴ੳ㰠⁴㵤∠∮㹥㰮⼬㹡഻ਠ㰽㹢൯ੴ⁴㰠㸠੮ⁱ⁵⁴㱥㸨റ††
′
†㰠⁰㵤∠∍㸊഼ੰ‾‼‾
⁵㱭㸠㱯㴠≩≰㹯ㅳ㰻⽳㹴㰠⽴㹲㱥⽥㹯൴ੴ†⁺†⁴⁰㰠⼠㸳ഩ
⁵††㱥㵩≮≯㹳഻ੳ†⁴†⁵⁴㱥㹢㱯†㵴≯∨㸴㰩㴯≰≔㹨㱥㵡≬≬㹤㱰⼠㹡㱤⁴㴳∠∬㸠䄦䉡䴻㰠⼲㸼㰯‼㴠≬∠㹲⡩㱧㵮≴∠㹤㱥⽴㸠䉦⥴㱯⽲㹨㱥⼠㹴㰨⼩㹨㱯⽨㹯㱵⽬㹥൴ੵ†⁴†⁴⁴㰠⽮㹢ੲ††㱥⽮㸠൩੮†⁴㰠⽳㹥ഠੲ㱯⽯㹴ഠੴ㱨⽡㸠൮੯㱤㸮䤠䉨Ɽ⁷†††☠㭤㵴㈬⼠㍡䤠䉴ⱦ††ⵡⵥ㰠†⽡㹰൰੬㱩㵩≯≴㹨㱯⽤㹥ഠ੩㱭㹮൴†⁹㰠㹡൩੮†⁴†㱳㹥ഠ੯†⁴†⁴㰠㴠≯≥㹰ൡੲ⁴⁹†⁴㱩㹧㰠㵯≦∠㹬ㅥ㱦⽴㸠㱲⽴㸠㱳⽵㹥ੳ†⁉†⁹†㱥⼬㹥ഠੲⁱ⁵†⁴⁴†㱴†㴨∩≨㹯ਠ⁵††⁴㱡㹴㱩 㵰∾≥㹥㱓㵩≮≳㸠㱎⁴㵭≰∠㹴㱎⽯㹮⁴㱥㴠≣≫㹥䄠䉡䵯㱦⽩㹯㱮⁵㴠≴≩㹮⡮㱥㵤≥≤㹥㱴⽨㹡䉩Ᵽ㱮†㵥∠∠㹩㱲⽦㸼 Ⰺ‼㱰㴠≭≲㹲㰠⽡㸬†⥮㱳⼨㹥㱭⽯㸬㰠⽳㸩㰬⽤㸠㱩⽴㹴൯ੲ
†⁙⁵†⁵⁴㰠⽡㹯ഠ੯⁶†㱴⽯㹲൩੮
㰠⽴㹰൬㱹⼠㹵൲ੲ㱥⽮㸠ൣ੯㱮㹩䥧䉯Ⱐ…ⵔ♣㭮㴠⁴⽨⁷䥩䈺 ⱬ†⵰⠠†⤯†䤾䅴䕤⽥⁰⁴ㄠ⼨㉩††ㅫ㰩⼮㸯൬੩㰾㸊䄠䈠䵩⁴†⁴⸠㰰⽮㹤ഠ㱩⁴㵤≥≲㸠㱴⽲㹬മਠ㱍㹰㱡㹳൵੯•‾⁰⁵㱩㸯൳ੰ‾†⁁⁹⁌㱴※㴼≳≡㹳൳•⁴⁴•‾⁶⁵㱥㹳㱰㵬≡≰㹡ㅲ㱡⽭㸩㰼⁰⽡㹮㰾⼼㹡൮ਾ‼ ⁰‾‼† 㰾⼼㹲ਾ ††††††††††㰠 㴠∠∠㸠഼ਯ⁴‾ ††††‼ ⁴㱢㸾㰍㴊∼∊㸼㱰⁵㵮∠≴㸠㱳㴠≶≯㹮㰠⽩㹩㱡㵡≴≥㸠㰠⽢㹰㱬㵥≳≁㹲⡲䭡⁹ⱴ†⥣㱯⽮㸠㱴⽨㹬㱵⽥㸠㱡⽮㹤㱩⽮㹲††⁴†㱯⽲㹳൰੯†㱹⽳㸯൰ਾ ‼⁰㰾⽅㹥മਠ㱓⽵㸠൴੨㱩⽳㹭ൡੰ㰠㹯䅮⁴⁵‰‱†Ⱜ††‵⁔⁌⸠㱲⁵⽲㹮㰠†㵳≴⁴≳㸠㱃⽡㹩൬੬㰬㹬ബਠ⁁㱥㹬ബਠ†⁴⁴†㱯㹥൲ਮ‼ ⁰‾ ‼⁰‾⁎⁴㱥⁂㵥≹≡㹮ਠ⁶⁵
†㱵㹢㱥㵥≤≩㹴ㅥ㱲⽡㸠㱴⽵㹧㱨⼠㸠ുੂ⁔⁓⁴†⁴⁴㰠⽲㹲ੳ⁴†⁴†⁰㰮⁰㴲∠≓㹵ൢ੭•‾⁓⁵㱩㹯㱮‾㵗≲≹㹯㱵㵩≮∮㹩㱡㴸∮≫㹡㱲⽮㹴†㱺㵯≴∠㹣㱳⼠㹳㰮㵬≬∠㹧⡵䭩⥮㱥⽳㹴㱥⽤㹥㱲⼠㹭㱥⼠㹡㱭⽰㹩൮੫††⁂㱡⽲㹌†⁁㰬⼠㹬൬੯⁷†⁰㱯⽪㹬ൡੲ㱩⽦㹯൮ੳ㰠⽯㸠ൂ੬㱡㹫⁉⁵††⁵Ɱ†††⁰⸠㱮⽤㸠െ੩㱲 㵡≭∀㸀㰀⼀㸀ഀ㰀㸀ഀ 㰀㸀ഀ 㰀㸀ഀ 㰀 㴀∀∀㸀ഀ 㰀㸀㰀 㴀∀∀㸀㰀⼀㸀㰀 ⼀㸀㰀⼀㸀ഀ 㰀⼀㸀ഀ 㰀 㴀∀∀㸀ഀ 㰀㸀㰀 㴀∀∀㸀㰀 㴀∀∀㸀㰀 㴀∀∀㸀㰀⼀㸀 㰀 㴀∀∀㸀㰀⼀㸀㰀 㴀∀∀㸀⠀䬀 ⤀㰀⼀㸀㰀⼀㸀㰀⼀㸀㰀 ⼀㸀㰀⼀㸀ഀ 㰀⼀㸀ഀ 㰀⼀㸀ഀ 㰀⼀㸀ഀ㰀⼀㸀ഀ㰀⼀㸀ഀ㰀㸀 ⸀ Ⰰ ⸀㰀 ⼀㸀ഀ㰀 㴀∀ ∀㸀㰀⼀㸀ഀ㰀㸀ഀ 㰀㸀ഀ 㰀㸀ഀ 㰀 㴀∀∀㸀ഀ 㰀㸀㰀 㴀∀∀㸀㰀⼀㸀㰀 ⼀㸀㰀⼀㸀ഀ 㰀⼀㸀ഀ 㰀 㴀∀∀㸀ഀ 㰀㸀㰀 㴀∀∀㸀㰀 㴀∀∀㸀㰀 㴀∀∀㸀㰀⼀㸀 㰀 㴀∀∀㸀㰀⼀㸀 㰀 㴀∀∀㸀䬀㰀⼀㸀㰀 㴀∀∀㸀⠀䬀 ⤀㰀⼀㸀㰀⼀㸀㰀⼀㸀㰀 ⼀㸀㰀⼀㸀ഀ 㰀⼀㸀ഀ 㰀⼀㸀ഀ 㰀⼀㸀ഀ㰀⼀㸀ഀ㰀⼀㸀ഀ㰀㸀 㬀 Ⰰ ⸀㰀 ⼀㸀ഀ㰀 㴀∀ ∀㸀㰀⼀㸀ഀ㰀㸀ഀ 㰀㸀ഀ 㰀㸀ഀ 㰀 㴀∀∀㸀ഀ 㰀㸀㰀 㴀∀∀㸀㰀⼀㸀㰀 ⼀㸀㰀⼀㸀ഀ 㰀⼀㸀ഀ 㰀 㴀∀∀㸀ഀ 㰀㸀㰀 㴀∀∀㸀㰀 㴀∀∀㸀㰀 㴀∀∀㸀㰀⼀㸀 㰀 㴀∀∀㸀㰀⼀㸀 㰀 㴀∀∀㸀㰀⼀㸀㰀 㴀∀∀㸀⠀⤀㰀⼀㸀㰀⼀㸀㰀⼀㸀㰀 ⼀㸀㰀⼀㸀ഀ 㰀⼀㸀ഀ 㰀⼀㸀ഀ 㰀⼀㸀ഀ㰀⼀㸀ഀ㰀⼀㸀ഀ㰀㸀 ⴀ ⸀㰀 ⼀㸀ഀ㰀 㴀∀ ∀㸀㰀⼀㸀ഀ㰀㸀ഀ 㰀㸀ഀ 㰀㸀ഀ 㰀 㴀∀∀㸀ഀ 㰀㸀㰀 㴀∀∀㸀㰀⼀㸀㰀 ⼀㸀㰀⼀㸀ഀ 㰀⼀㸀ഀ 㰀 㴀∀∀㸀ഀ 㰀㸀㰀 㴀∀∀㸀㰀 㴀∀∀㸀㰀 㴀∀∀㸀㰀⼀㸀 䄀䈀㬀䬀㬀 㰀 㴀∀∀㸀㰀⼀㸀㰀 㴀∀∀㸀⠀⤀㰀⼀㸀㰀⼀㸀㰀⼀㸀㰀 ⼀㸀㰀⼀㸀ഀ 㰀⼀㸀ഀ 㰀⼀㸀ഀ 㰀⼀㸀ഀ㰀⼀㸀ഀ㰀⼀㸀ഀ㰀㸀 䄀䈀 ⠀ ⤀ ⸀ 䄀䈀 䄀䈀䴀⸀㰀 ⼀㸀ഀ䔀⸀ ⠀Ⰰ ⤀ 㨀 ⠀ Ⰰ 䌀⤀Ⰰ ⠀㈀Ⰰ 䈀⤀Ⰰ ⠀㐀㔀Ⰰ 䌀⤀Ⰰ ⠀㠀Ⰰ 䄀⤀Ⰰ ⠀㤀㔀Ⰰ 䈀⤀⸀ Ⰰ 䄀䈀 Ⰰ ㈀Ⰰ 㐀㔀Ⰰ 㠀Ⰰ 㤀⸀㰀 ⼀㸀ഀ㰀 㴀∀ ∀㸀㰀⼀㸀ഀ㰀㸀ഀ 㰀㸀ഀ 㰀㸀ഀ 㰀 㴀∀∀㸀ഀ 㰀㸀㰀 㴀∀∀㸀㰀⼀㸀㰀 ⼀㸀㰀⼀㸀ഀ 㰀⼀㸀ഀ 㰀 㴀∀∀㸀ഀ 㰀㸀㰀 㴀∀∀㸀㰀 㴀∀∀㸀㰀 㴀∀