Created
July 2, 2019 13:42
-
-
Save L9m/5d173200d404d072600e777b26b372b4 to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/siyegaq
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
class Node { | |
contructor(key) { | |
this.key = key | |
this.right = this.left = null | |
} | |
} | |
class Tree { | |
contructor() { | |
this.root = null | |
} | |
insert(node, element) { | |
if (node === null) { | |
node = new Node(element) | |
} else if (element < node.key) { | |
node.left = this.insert(node.left, element) | |
if (node.left !== null) { | |
if ((this.getHeight(node.left) - this.getHeight(node.right)) > 1){ | |
if (element < node.left.key){ | |
node = this.rotationLL(node) | |
} else { | |
node = this.rotationLR(node) | |
} | |
} | |
} | |
} else if (element > node.key) { | |
node.right = insert(node.right, element) | |
if (node.right !== null) { | |
if ((this.getHeight(node.right) - this.getHeight(node.left)) > 1){ | |
if(element > node.right.key) { | |
node = this.rotationRR(node) | |
} else { | |
node = this.rotationRL(node) | |
} | |
} | |
} | |
} | |
rotationRR(node) { | |
var tmp = node.right | |
node.right = tmp.left | |
tmp.left = node | |
return tmp | |
} | |
rotationLL(node) { | |
var tmp = node.left | |
node.left = tmp.right | |
tmp.right = node | |
return tmp | |
} | |
rotationLR(node) { | |
node.left = this.rotationRR(node.left) | |
return this.rotationLL(NODE) | |
} | |
rotationRL(node) { | |
node.right = this.rotationLL(node.right) | |
return this.rotationRR(node) | |
} | |
getHeight(node) { | |
if (node === null) { | |
return -1 | |
} else { | |
return Math.max(this.getHeight(node.left), this.getHeight(node.right)) + 1 | |
} | |
} | |
} | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">class Node { | |
contructor(key) { | |
this.key = key | |
this.right = this.left = null | |
} | |
} | |
class Tree { | |
contructor() { | |
this.root = null | |
} | |
insert(node, element) { | |
if (node === null) { | |
node = new Node(element) | |
} else if (element < node.key) { | |
node.left = this.insert(node.left, element) | |
if (node.left !== null) { | |
if ((this.getHeight(node.left) - this.getHeight(node.right)) > 1){ | |
if (element < node.left.key){ | |
node = this.rotationLL(node) | |
} else { | |
node = this.rotationLR(node) | |
} | |
} | |
} | |
} else if (element > node.key) { | |
node.right = insert(node.right, element) | |
if (node.right !== null) { | |
if ((this.getHeight(node.right) - this.getHeight(node.left)) > 1){ | |
if(element > node.right.key) { | |
node = this.rotationRR(node) | |
} else { | |
node = this.rotationRL(node) | |
} | |
} | |
} | |
} | |
rotationRR(node) { | |
var tmp = node.right | |
node.right = tmp.left | |
tmp.left = node | |
return tmp | |
} | |
rotationLL(node) { | |
var tmp = node.left | |
node.left = tmp.right | |
tmp.right = node | |
return tmp | |
} | |
rotationLR(node) { | |
node.left = this.rotationRR(node.left) | |
return this.rotationLL(NODE) | |
} | |
rotationRL(node) { | |
node.right = this.rotationLL(node.right) | |
return this.rotationRR(node) | |
} | |
getHeight(node) { | |
if (node === null) { | |
return -1 | |
} else { | |
return Math.max(this.getHeight(node.left), this.getHeight(node.right)) + 1 | |
} | |
} | |
}</script></body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Node { | |
contructor(key) { | |
this.key = key | |
this.right = this.left = null | |
} | |
} | |
class Tree { | |
contructor() { | |
this.root = null | |
} | |
insert(node, element) { | |
if (node === null) { | |
node = new Node(element) | |
} else if (element < node.key) { | |
node.left = this.insert(node.left, element) | |
if (node.left !== null) { | |
if ((this.getHeight(node.left) - this.getHeight(node.right)) > 1){ | |
if (element < node.left.key){ | |
node = this.rotationLL(node) | |
} else { | |
node = this.rotationLR(node) | |
} | |
} | |
} | |
} else if (element > node.key) { | |
node.right = insert(node.right, element) | |
if (node.right !== null) { | |
if ((this.getHeight(node.right) - this.getHeight(node.left)) > 1){ | |
if(element > node.right.key) { | |
node = this.rotationRR(node) | |
} else { | |
node = this.rotationRL(node) | |
} | |
} | |
} | |
} | |
rotationRR(node) { | |
var tmp = node.right | |
node.right = tmp.left | |
tmp.left = node | |
return tmp | |
} | |
rotationLL(node) { | |
var tmp = node.left | |
node.left = tmp.right | |
tmp.right = node | |
return tmp | |
} | |
rotationLR(node) { | |
node.left = this.rotationRR(node.left) | |
return this.rotationLL(NODE) | |
} | |
rotationRL(node) { | |
node.right = this.rotationLL(node.right) | |
return this.rotationRR(node) | |
} | |
getHeight(node) { | |
if (node === null) { | |
return -1 | |
} else { | |
return Math.max(this.getHeight(node.left), this.getHeight(node.right)) + 1 | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment