forked from aaronbloomfield/pdr
-
Notifications
You must be signed in to change notification settings - Fork 228
/
AVLTree.h.html
86 lines (72 loc) · 8.52 KB
/
AVLTree.h.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="GENERATOR" content="GNU source-highlight
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite">
<title>AVLTree.h</title>
</head>
<body style="background-color:white">
<pre><b><span style="color:#000080">#ifndef</span></b> AVL_H
<b><span style="color:#000080">#define</span></b> AVL_H
<b><span style="color:#000080">#include</span></b> <span style="color:#FF0000"><string></span>
<b><span style="color:#000080">#include</span></b> <span style="color:#FF0000">"AVLNode.h"</span>
<b><span style="color:#0000FF">using</span></b> <b><span style="color:#0000FF">namespace</span></b> std<span style="color:#990000">;</span>
<i><span style="color:#9A1900">// You do not need to know how Trunk works.</span></i>
<b><span style="color:#0000FF">struct</span></b> <span style="color:#008080">Trunk</span> <span style="color:#FF0000">{</span>
Trunk<span style="color:#990000">*</span> prev<span style="color:#990000">;</span>
<span style="color:#008080">string</span> str<span style="color:#990000">;</span>
<b><span style="color:#000000">Trunk</span></b><span style="color:#990000">(</span>Trunk<span style="color:#990000">*</span> prev<span style="color:#990000">,</span> <span style="color:#008080">string</span> str<span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
<b><span style="color:#0000FF">this</span></b><span style="color:#990000">-></span>prev <span style="color:#990000">=</span> prev<span style="color:#990000">;</span>
<b><span style="color:#0000FF">this</span></b><span style="color:#990000">-></span>str <span style="color:#990000">=</span> str<span style="color:#990000">;</span>
<span style="color:#FF0000">}</span>
<span style="color:#FF0000">}</span><span style="color:#990000">;</span>
<b><span style="color:#0000FF">class</span></b> <span style="color:#008080">AVLTree</span> <span style="color:#FF0000">{</span>
<b><span style="color:#0000FF">public</span></b><span style="color:#990000">:</span>
<b><span style="color:#000000">AVLTree</span></b><span style="color:#990000">();</span>
<span style="color:#990000">~</span><b><span style="color:#000000">AVLTree</span></b><span style="color:#990000">();</span>
<i><span style="color:#9A1900">// insert finds a position for x in the tree and places it there, rebalancing</span></i>
<i><span style="color:#9A1900">// as necessary.</span></i>
<span style="color:#009900">void</span> <b><span style="color:#000000">insert</span></b><span style="color:#990000">(</span><b><span style="color:#0000FF">const</span></b> string<span style="color:#990000">&</span> x<span style="color:#990000">);</span>
<i><span style="color:#9A1900">// remove finds x's position in the tree and removes it, rebalancing as</span></i>
<i><span style="color:#9A1900">// necessary.</span></i>
<span style="color:#009900">void</span> <b><span style="color:#000000">remove</span></b><span style="color:#990000">(</span><b><span style="color:#0000FF">const</span></b> string<span style="color:#990000">&</span> x<span style="color:#990000">);</span>
<i><span style="color:#9A1900">// printTree pretty-prints the tree to aid debugging.</span></i>
<span style="color:#009900">void</span> <b><span style="color:#000000">printTree</span></b><span style="color:#990000">();</span>
<i><span style="color:#9A1900">// pathTo finds x in the tree and returns a string representing the path it</span></i>
<i><span style="color:#9A1900">// took to get there.</span></i>
<span style="color:#008080">string</span> <b><span style="color:#000000">pathTo</span></b><span style="color:#990000">(</span><b><span style="color:#0000FF">const</span></b> string<span style="color:#990000">&</span> x<span style="color:#990000">)</span> <b><span style="color:#0000FF">const</span></b><span style="color:#990000">;</span>
<i><span style="color:#9A1900">// find determines whether or not x exists in the tree.</span></i>
<span style="color:#009900">bool</span> <b><span style="color:#000000">find</span></b><span style="color:#990000">(</span><b><span style="color:#0000FF">const</span></b> string<span style="color:#990000">&</span> x<span style="color:#990000">)</span> <b><span style="color:#0000FF">const</span></b><span style="color:#990000">;</span>
<i><span style="color:#9A1900">// numNodes returns the total number of nodes in the tree.</span></i>
<span style="color:#009900">int</span> <b><span style="color:#000000">numNodes</span></b><span style="color:#990000">()</span> <b><span style="color:#0000FF">const</span></b><span style="color:#990000">;</span>
<b><span style="color:#0000FF">private</span></b><span style="color:#990000">:</span>
<i><span style="color:#9A1900">// Declare a root node</span></i>
AVLNode<span style="color:#990000">*</span> root<span style="color:#990000">;</span>
<i><span style="color:#9A1900">// balance makes sure that the subtree with root n maintains the AVL tree</span></i>
<i><span style="color:#9A1900">// property, namely that the balance factor of n is either -1, 0, or 1.</span></i>
<span style="color:#009900">void</span> <b><span style="color:#000000">balance</span></b><span style="color:#990000">(</span>AVLNode<span style="color:#990000">*&</span> n<span style="color:#990000">);</span>
<i><span style="color:#9A1900">// rotateLeft performs a single rotation on node n with its right child.</span></i>
AVLNode<span style="color:#990000">*</span> <b><span style="color:#000000">rotateLeft</span></b><span style="color:#990000">(</span>AVLNode<span style="color:#990000">*&</span> n<span style="color:#990000">);</span>
<i><span style="color:#9A1900">// rotateRight performs a single rotation on node n with its left child.</span></i>
AVLNode<span style="color:#990000">*</span> <b><span style="color:#000000">rotateRight</span></b><span style="color:#990000">(</span>AVLNode<span style="color:#990000">*&</span> n<span style="color:#990000">);</span>
<i><span style="color:#9A1900">// private helper for remove to allow recursion over different nodes. returns</span></i>
<i><span style="color:#9A1900">// an AVLNode* that is assigned to the original node.</span></i>
AVLNode<span style="color:#990000">*</span> <b><span style="color:#000000">remove</span></b><span style="color:#990000">(</span>AVLNode<span style="color:#990000">*&</span> n<span style="color:#990000">,</span> <b><span style="color:#0000FF">const</span></b> string<span style="color:#990000">&</span> x<span style="color:#990000">);</span>
<i><span style="color:#9A1900">// min finds the string with the smallest value in a subtree.</span></i>
<span style="color:#008080">string</span> <b><span style="color:#000000">min</span></b><span style="color:#990000">(</span>AVLNode<span style="color:#990000">*</span> node<span style="color:#990000">)</span> <b><span style="color:#0000FF">const</span></b><span style="color:#990000">;</span>
<i><span style="color:#9A1900">// height returns the value of the height field in a node. If the node is</span></i>
<i><span style="color:#9A1900">// null, it returns -1.</span></i>
<span style="color:#009900">int</span> <b><span style="color:#000000">height</span></b><span style="color:#990000">(</span>AVLNode<span style="color:#990000">*</span> node<span style="color:#990000">)</span> <b><span style="color:#0000FF">const</span></b><span style="color:#990000">;</span>
<i><span style="color:#9A1900">// private helper for printTree to allow recursion over different nodes.</span></i>
<span style="color:#009900">void</span> <b><span style="color:#000000">printTree</span></b><span style="color:#990000">(</span>AVLNode<span style="color:#990000">*</span> root<span style="color:#990000">,</span> Trunk<span style="color:#990000">*</span> prev<span style="color:#990000">,</span> <span style="color:#009900">bool</span> isRight<span style="color:#990000">);</span>
<i><span style="color:#9A1900">// Any other methods you need...</span></i>
<span style="color:#FF0000">}</span><span style="color:#990000">;</span>
<i><span style="color:#9A1900">// max returns the greater of two integers.</span></i>
<span style="color:#009900">int</span> <b><span style="color:#000000">max</span></b><span style="color:#990000">(</span><span style="color:#009900">int</span> a<span style="color:#990000">,</span> <span style="color:#009900">int</span> b<span style="color:#990000">);</span>
<b><span style="color:#000080">#endif</span></b>
</pre>
</body>
</html>