-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRbTreeNode.as
More file actions
166 lines (165 loc) · 3.84 KB
/
Copy pathRbTreeNode.as
File metadata and controls
166 lines (165 loc) · 3.84 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package nl.hku.kmt.ikc.as3.modular.tools.data.struct.tree
{
public class RbTreeNode implements IRbTreeNode, ITreeNode
{
/**
* Constructor for the RedBlackTreeNode.
* @param data Data for the node to refer to. It will also be the input for the comparator.
*
*/
public function RbTreeNode(data:Object)
{
this._data = data;
this._red = true;
}
public function get rank():uint{ return _rank; }
public function set rank(val:uint):void{ this._rank = val; }
private var _rank:uint;
/**
*
* @inheritDoc
*
*/
public function get data():Object{ return _data; }
/**
*
* @inheritDoc
*
*/
public function set data(val:Object):void{ _data = val; }
/**
*
* @inheritDoc
*
*/
public function get left():ITreeNode{ return _left; }
/**
*
* @inheritDoc
*
*/
public function set left(val:ITreeNode):void{ _left = val; }
/**
*
* @inheritDoc
*
*/
public function get right():ITreeNode{ return _right; }
/**
*
* @inheritDoc
*
*/
public function set right(val:ITreeNode):void{ _right = val; }
/**
*
* @inheritDoc
*
*/
public function get_child(direction:Boolean):ITreeNode{
return direction ? this.right : this.left;
}
/**
*
* @inheritDoc
*
*/
public function set_child(direction:Boolean, value:ITreeNode):void{
if(direction){
this._right = value;
}else{
this._left = value;
}
}
private var _red:Boolean;
private var _size:int;
/**
*
* @inheritDoc
*
*/
public function get size():int{ return _size; }
/**
*
* @inheritDoc
*
*/
public function set size(val:int):void{ _size = val; }
/**
*
* @inheritDoc
*
*/
public function get red():Boolean{ return _red; }
/**
*
* @inheritDoc
*
*/
public function set red(val:Boolean):void{ this._red = val; }
/**
*
* @inheritDoc
*
*/
public function get dot():String{
var me:String = this.data.toString();
var dot:String = "";
if(this.left){
var leftStr:String = this.left.data.toString();
var leftSize:String = RbTreeNode(this.left).size.toString();
dot += leftStr + '[label="' +leftStr+'-s'+leftSize+ '"';
dot += RbTreeNode(this.left).red ? ',fillcolor="red",style="filled"]' : ']';
dot += ";";
dot += me + "->" + leftStr + ";";
dot += RbTreeNode(this.left).dot;
}
if(this.right){
var rightStr:String = this.right.data.toString();
var rightSize:String = RbTreeNode(this.right).size.toString();
dot += rightStr + '[label="' +rightStr+'-s'+rightSize+ '"';
dot += RbTreeNode(this.right).red ? ',fillcolor="red",style="filled"]' : ']';
dot += ";";
dot += me + "->" + rightStr + ";";
dot += RbTreeNode(this.right).dot;
}
return dot;
}
/**
* error check for debugging purposes, returns recalculated size
* throws an error containing the toString output of the data property
* if the old size does not match the recalculated size.
*
* this should normally not happen, this function should not be used.
* instead the delete and insert algorithms must maintain the size property properly.
* @return
*
*/
public function checkSize():int{
var newSize:int = 1;
var lc:RbTreeNode = this.left as RbTreeNode;
var rc:RbTreeNode = this.right as RbTreeNode;
if(lc) newSize += lc.checkSize();
if(rc) newSize += rc.checkSize();
if(newSize != this.size){
throw new Error("INCORRECT SIZE on " + this.data);
}
this.size = newSize;
return newSize;
}
/**
*
* @return
*
*/
public function get export():*{
return this.data;
}
public function toString():String {
return this.data.toString() + (this.red ? "[R]" : "");
}
protected var _data:Object;
protected var _left:ITreeNode;
protected var _right:ITreeNode;
}
}