123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892 |
- goog.provide('goog.structs.AvlTree');
- goog.provide('goog.structs.AvlTree.Node');
- goog.require('goog.structs.Collection');
- goog.structs.AvlTree = function(opt_comparator) {
- this.comparator_ = opt_comparator || goog.structs.AvlTree.DEFAULT_COMPARATOR_;
- };
- goog.structs.AvlTree.DEFAULT_COMPARATOR_ = function(a, b) {
- if (String(a) < String(b)) {
- return -1;
- } else if (String(a) > String(b)) {
- return 1;
- }
- return 0;
- };
- goog.structs.AvlTree.prototype.root_ = null;
- goog.structs.AvlTree.prototype.comparator_ = null;
- goog.structs.AvlTree.prototype.minNode_ = null;
- goog.structs.AvlTree.prototype.maxNode_ = null;
- goog.structs.AvlTree.prototype.add = function(value) {
-
- if (this.root_ == null) {
- this.root_ = new goog.structs.AvlTree.Node(value);
- this.minNode_ = this.root_;
- this.maxNode_ = this.root_;
- return true;
- }
-
- var newNode = null;
-
- this.traverse_(function(node) {
- var retNode = null;
- var comparison = this.comparator_(node.value, value);
- if (comparison > 0) {
- retNode = node.left;
- if (node.left == null) {
- newNode = new goog.structs.AvlTree.Node(value, node);
- node.left = newNode;
- if (node == this.minNode_) {
- this.minNode_ = newNode;
- }
- }
- } else if (comparison < 0) {
- retNode = node.right;
- if (node.right == null) {
- newNode = new goog.structs.AvlTree.Node(value, node);
- node.right = newNode;
- if (node == this.maxNode_) {
- this.maxNode_ = newNode;
- }
- }
- }
- return retNode;
- });
-
- if (newNode) {
- this.traverse_(function(node) {
- node.count++;
- return node.parent;
- }, newNode.parent);
- this.balance_(newNode.parent);
- }
-
- return !!newNode;
- };
- goog.structs.AvlTree.prototype.remove = function(value) {
-
- var retValue = null;
-
- this.traverse_(function(node) {
- var retNode = null;
- var comparison = this.comparator_(node.value, value);
- if (comparison > 0) {
- retNode = node.left;
- } else if (comparison < 0) {
- retNode = node.right;
- } else {
- retValue = node.value;
- this.removeNode_(node);
- }
- return retNode;
- });
-
- return retValue;
- };
- goog.structs.AvlTree.prototype.clear = function() {
- this.root_ = null;
- this.minNode_ = null;
- this.maxNode_ = null;
- };
- goog.structs.AvlTree.prototype.contains = function(value) {
-
- var isContained = false;
-
- this.traverse_(function(node) {
- var retNode = null;
- var comparison = this.comparator_(node.value, value);
- if (comparison > 0) {
- retNode = node.left;
- } else if (comparison < 0) {
- retNode = node.right;
- } else {
- isContained = true;
- }
- return retNode;
- });
-
- return isContained;
- };
- goog.structs.AvlTree.prototype.indexOf = function(value) {
-
- var retIndex = -1;
- var currIndex = 0;
-
- this.traverse_(function(node) {
- var comparison = this.comparator_(node.value, value);
- if (comparison > 0) {
-
- return node.left;
- }
- if (node.left) {
-
- currIndex += node.left.count;
- }
- if (comparison < 0) {
-
- currIndex++;
-
- return node.right;
- }
-
- retIndex = currIndex;
- return null;
- });
-
- return retIndex;
- };
- goog.structs.AvlTree.prototype.getCount = function() {
- return this.root_ ? this.root_.count : 0;
- };
- goog.structs.AvlTree.prototype.getKthValue = function(k) {
- if (k < 0 || k >= this.getCount()) {
- return null;
- }
- return this.getKthNode_(k).value;
- };
- goog.structs.AvlTree.prototype.getMinimum = function() {
- return this.getMinNode_().value;
- };
- goog.structs.AvlTree.prototype.getMaximum = function() {
- return this.getMaxNode_().value;
- };
- goog.structs.AvlTree.prototype.getHeight = function() {
- return this.root_ ? this.root_.height : 0;
- };
- goog.structs.AvlTree.prototype.getValues = function() {
- var ret = [];
- this.inOrderTraverse(function(value) { ret.push(value); });
- return ret;
- };
- goog.structs.AvlTree.prototype.inOrderTraverse = function(
- func, opt_startValue) {
-
- if (!this.root_) {
- return;
- }
-
- var startNode;
- if (opt_startValue !== undefined) {
- this.traverse_(function(node) {
- var retNode = null;
- var comparison = this.comparator_(node.value, opt_startValue);
- if (comparison > 0) {
- retNode = node.left;
- startNode = node;
- } else if (comparison < 0) {
- retNode = node.right;
- } else {
- startNode = node;
- }
- return retNode;
- });
- if (!startNode) {
- return;
- }
- } else {
- startNode = this.getMinNode_();
- }
-
- var node = startNode, prev = startNode.left ? startNode.left : startNode;
- while (node != null) {
- if (node.left != null && node.left != prev && node.right != prev) {
- node = node.left;
- } else {
- if (node.right != prev) {
- if (func(node.value)) {
- return;
- }
- }
- var temp = node;
- node =
- node.right != null && node.right != prev ? node.right : node.parent;
- prev = temp;
- }
- }
- };
- goog.structs.AvlTree.prototype.reverseOrderTraverse = function(
- func, opt_startValue) {
-
- if (!this.root_) {
- return;
- }
-
- var startNode;
- if (opt_startValue !== undefined) {
- this.traverse_(goog.bind(function(node) {
- var retNode = null;
- var comparison = this.comparator_(node.value, opt_startValue);
- if (comparison > 0) {
- retNode = node.left;
- } else if (comparison < 0) {
- retNode = node.right;
- startNode = node;
- } else {
- startNode = node;
- }
- return retNode;
- }, this));
- if (!startNode) {
- return;
- }
- } else {
- startNode = this.getMaxNode_();
- }
-
- var node = startNode, prev = startNode.right ? startNode.right : startNode;
- while (node != null) {
- if (node.right != null && node.right != prev && node.left != prev) {
- node = node.right;
- } else {
- if (node.left != prev) {
- if (func(node.value)) {
- return;
- }
- }
- var temp = node;
- node = node.left != null && node.left != prev ? node.left : node.parent;
- prev = temp;
- }
- }
- };
- goog.structs.AvlTree.prototype.traverse_ = function(
- traversalFunc, opt_startNode, opt_endNode) {
- var node = opt_startNode ? opt_startNode : this.root_;
- var endNode = opt_endNode ? opt_endNode : null;
- while (node && node != endNode) {
- node = traversalFunc.call(this, node);
- }
- };
- goog.structs.AvlTree.prototype.balance_ = function(node) {
- this.traverse_(function(node) {
-
- var lh = node.left ? node.left.height : 0;
- var rh = node.right ? node.right.height : 0;
-
- if (lh - rh > 1) {
- if (node.left.right &&
- (!node.left.left || node.left.left.height < node.left.right.height)) {
- this.leftRotate_(node.left);
- }
- this.rightRotate_(node);
- } else if (rh - lh > 1) {
- if (node.right.left &&
- (!node.right.right ||
- node.right.right.height < node.right.left.height)) {
- this.rightRotate_(node.right);
- }
- this.leftRotate_(node);
- }
-
- lh = node.left ? node.left.height : 0;
- rh = node.right ? node.right.height : 0;
-
- node.height = Math.max(lh, rh) + 1;
-
- return node.parent;
- }, node);
- };
- goog.structs.AvlTree.prototype.leftRotate_ = function(node) {
-
- if (node.isLeftChild()) {
- node.parent.left = node.right;
- node.right.parent = node.parent;
- } else if (node.isRightChild()) {
- node.parent.right = node.right;
- node.right.parent = node.parent;
- } else {
- this.root_ = node.right;
- this.root_.parent = null;
- }
-
- var temp = node.right;
- node.right = node.right.left;
- if (node.right != null) node.right.parent = node;
- temp.left = node;
- node.parent = temp;
-
- temp.count = node.count;
- node.count -= (temp.right ? temp.right.count : 0) + 1;
- };
- goog.structs.AvlTree.prototype.rightRotate_ = function(node) {
-
- if (node.isLeftChild()) {
- node.parent.left = node.left;
- node.left.parent = node.parent;
- } else if (node.isRightChild()) {
- node.parent.right = node.left;
- node.left.parent = node.parent;
- } else {
- this.root_ = node.left;
- this.root_.parent = null;
- }
-
- var temp = node.left;
- node.left = node.left.right;
- if (node.left != null) node.left.parent = node;
- temp.right = node;
- node.parent = temp;
-
- temp.count = node.count;
- node.count -= (temp.left ? temp.left.count : 0) + 1;
- };
- goog.structs.AvlTree.prototype.removeNode_ = function(node) {
-
-
- if (node.left != null || node.right != null) {
- var b = null;
- var r;
- if (node.left != null) {
- r = this.getMaxNode_(node.left);
-
- this.traverse_(function(node) {
- node.count--;
- return node.parent;
- }, r);
- if (r != node.left) {
- r.parent.right = r.left;
- if (r.left) r.left.parent = r.parent;
- r.left = node.left;
- r.left.parent = r;
- b = r.parent;
- }
- r.parent = node.parent;
- r.right = node.right;
- if (r.right) r.right.parent = r;
- if (node == this.maxNode_) this.maxNode_ = r;
- r.count = node.count;
- } else {
- r = this.getMinNode_(node.right);
-
- this.traverse_(function(node) {
- node.count--;
- return node.parent;
- }, r);
- if (r != node.right) {
- r.parent.left = r.right;
- if (r.right) r.right.parent = r.parent;
- r.right = node.right;
- r.right.parent = r;
- b = r.parent;
- }
- r.parent = node.parent;
- r.left = node.left;
- if (r.left) r.left.parent = r;
- if (node == this.minNode_) this.minNode_ = r;
- r.count = node.count;
- }
-
- if (node.isLeftChild()) {
- node.parent.left = r;
- } else if (node.isRightChild()) {
- node.parent.right = r;
- } else {
- this.root_ = r;
- }
-
- this.balance_(b ? b : r);
- } else {
-
- this.traverse_(function(node) {
- node.count--;
- return node.parent;
- }, node.parent);
-
- if (node.isLeftChild()) {
- this.special = 1;
- node.parent.left = null;
- if (node == this.minNode_) this.minNode_ = node.parent;
- this.balance_(node.parent);
- } else if (node.isRightChild()) {
- node.parent.right = null;
- if (node == this.maxNode_) this.maxNode_ = node.parent;
- this.balance_(node.parent);
- } else {
- this.clear();
- }
- }
- };
- goog.structs.AvlTree.prototype.getKthNode_ = function(k, opt_rootNode) {
- var root = opt_rootNode || this.root_;
- var numNodesInLeftSubtree = root.left ? root.left.count : 0;
- if (k < numNodesInLeftSubtree) {
- return this.getKthNode_(k, root.left);
- } else if (k == numNodesInLeftSubtree) {
- return root;
- } else {
- return this.getKthNode_(k - numNodesInLeftSubtree - 1, root.right);
- }
- };
- goog.structs.AvlTree.prototype.getMinNode_ = function(opt_rootNode) {
- if (!opt_rootNode) {
- return this.minNode_;
- }
- var minNode = opt_rootNode;
- this.traverse_(function(node) {
- var retNode = null;
- if (node.left) {
- minNode = node.left;
- retNode = node.left;
- }
- return retNode;
- }, opt_rootNode);
- return minNode;
- };
- goog.structs.AvlTree.prototype.getMaxNode_ = function(opt_rootNode) {
- if (!opt_rootNode) {
- return this.maxNode_;
- }
- var maxNode = opt_rootNode;
- this.traverse_(function(node) {
- var retNode = null;
- if (node.right) {
- maxNode = node.right;
- retNode = node.right;
- }
- return retNode;
- }, opt_rootNode);
- return maxNode;
- };
- goog.structs.AvlTree.Node = function(value, opt_parent) {
-
- this.value = value;
-
- this.parent = opt_parent ? opt_parent : null;
-
- this.count = 1;
- };
- goog.structs.AvlTree.Node.prototype.left = null;
- goog.structs.AvlTree.Node.prototype.right = null;
- goog.structs.AvlTree.Node.prototype.height = 1;
- goog.structs.AvlTree.Node.prototype.isRightChild = function() {
- return !!this.parent && this.parent.right == this;
- };
- goog.structs.AvlTree.Node.prototype.isLeftChild = function() {
- return !!this.parent && this.parent.left == this;
- };
|