6.9: Treaps
- Page ID
- 46733
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)
( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\id}{\mathrm{id}}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\kernel}{\mathrm{null}\,}\)
\( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\)
\( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\)
\( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)
\( \newcommand{\vectorA}[1]{\vec{#1}} % arrow\)
\( \newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow\)
\( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vectorC}[1]{\textbf{#1}} \)
\( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)
\( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)
\( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)The invariant in a binary tree is that left is less than right with respect to insertion keys. e.g. for a key with order, ord(L) < ord(R). This doesn't dictate the relationship of nodes however, and left and right rotation does not affect the above. Therefore another order can be imposed. If the order is randomised, it is likely to counteract any skewness of a plain binary tree e.g. when inserting an already sorted input in order.
Below is a java example implementation, including a plain binary tree delete code example.
import java.util.Iterator; import java.util.LinkedList; import java.util.Random; public class Treap1<K extends Comparable<K>, V> { public Treap1(boolean test) { this.test = test; } public Treap1() {} boolean test = false; static Random random = new Random(System.currentTimeMillis()); class TreapNode { int priority = 0; K k; V val; TreapNode left, right; public TreapNode() { if (!test) { priority = random.nextInt(); } } } TreapNode root = null; void insert(K k, V val) { root = insert(k, val, root); } TreapNode insert(K k, V val, TreapNode node) { TreapNode node2 = new TreapNode(); node2.k = k; node2.val = val; if (node == null) { node = node2; } else if (k.compareTo(node.k) < 0) { node.left = insert(k, val, node.left); } else { node.right = insert(k, val, node.right); } if (node.left != null && node.left.priority > node.priority) { // left rotate TreapNode tmp = node.left; node.left = node.left.right; tmp.right = node; node = tmp; } else if (node.right != null && node.right.priority > node.priority) { // right rotate TreapNode tmp = node.right; node.right = node.right.left; tmp.left = node; node = tmp; } return node; } V find(K k) { return findNode(k, root); } private V findNode(K k, Treap1<K, V>.TreapNode node) { if (node == null) return null; if (k.compareTo(node.k) < 0) { return findNode(k, node.left); } else if (k.compareTo(node.k) > 0) { return findNode(k, node.right); } else { return node.val; } } static class Deleted { boolean success = false; } boolean delete(K k) { Deleted del = new Deleted(); root = deleteNode(k, root, del); return del.success; } private Treap1<K, V>.TreapNode deleteNode(K k, Treap1<K, V>.TreapNode node, Deleted del) { if (node == null) { return null; } else if (k.compareTo(node.k) < 0) { node.left = deleteNode(k, node.left, del) ; } else if (k.compareTo(node.k) > 0) { node.right = deleteNode(k, node.right, del); // k.compareTo(node.k) == 0 } else if ( node.left == null ) { del.success = true; return node.right; } else if ( node.right == null) { del.success = true; return node.left; } else if (node.left !=null && node.right != null){ /* // left rotate and all delete on left subtree TreapNode tmp = node.right; node.right = node.right.left; tmp.left = node; node = tmp; node.left = deleteNode(k , node.left, del); */ // more standard method ? doesn't disturb tree structure as much // find leftmost descendant of the right child node and replace contents TreapNode n2 = node.right; TreapNode previous2 = null; while (n2.left != null) { previous2 = n2; n2 = n2.left; } if (previous2 != null) { previous2.left = n2.right; //n2 has no parent link, orphaned } else { node.right = n2.right; //n2 has no parent link, orphaned } node.k = n2.k; node.val = n2.val; del.success = true; // once n2 out of scope, the orphaned node at n2 will be garbage collected, } return node; } public static void main(String[] args) { LinkedList<Integer> dat = new LinkedList<Integer>(); for (int i = 0; i < 15000; ++i) { dat.add(i); } testNumbers(dat, true); // no random priority balancing testNumbers(dat, false); } private static void testNumbers(LinkedList<Integer> dat, boolean test) { Treap1<Integer, Integer> treap = new Treap1<>(test); for (Integer integer : dat) { treap.insert(integer, integer); } long t1 = System.currentTimeMillis(); Iterator<Integer> desc = dat.iterator(); int found = 0; while (desc.hasNext()) { Integer j = desc.next(); Integer i = treap.find(j); if (j.equals(i)) { ++found; } } long t2 = System.currentTimeMillis(); System.out.println("found = " + found + " in " + (t2 - t1)); System.out.println("test delete"); int deleted = 0; for (Integer integer : dat) { if (treap.delete(integer)) ++deleted; } System.out.println("Deleted = " + deleted); } }