Skip to content

Instantly share code, notes, and snippets.

@wyon
Last active October 26, 2016 07:43
Show Gist options
  • Save wyon/f5c63bd0be6ccbd792458db73f614e6d to your computer and use it in GitHub Desktop.
Save wyon/f5c63bd0be6ccbd792458db73f614e6d to your computer and use it in GitHub Desktop.
some js code
// 二叉树
// 节点construct
function Node(data, left, right) {
this.data = data;
this.left = left;
this.right = right;
this.show = show;
}
function show() {
return this.data;
}
// 二叉树
function BST() {
this.root = null;
this.insert = insert;
this.inOrder = inOrder;
this.remove = remove;
}
// 插入节点
function insert(data) {
var n = new Node(data, null, null);
if(this.root == null) {
this.root = n;
} else {
var current = this.root;
var parent;
while(true) {
parent = current;
if(data < current.data) {
current = current.left;
if(current == null) {
parent.left = n;
break;
}
} else {
current = current.right;
if(current == null) {
parent.right = n;
break;
}
}
}
}
}
// 中序遍历,结果升序输出
function inOrder(node) {
if(node != null) {
inOrder(node.left);
print(node.show() + ' ');
inOrder(node.right);
}
}
// 前序遍历
function preOrder(node) {
if(node != null) {
print(node.show() + " ");
preOrder(node.left);
preOrder(node.right);
}
}
// 后序遍历
function postOrder(node) {
if(node != null) {
postOrder(node.left);
postOrder(node.right);
print(node.show()+" ");
}
}
// 删除某键值为data的节点
function remove(data) {
this.root = removeNode(this.root, data);
}
// 获取以current为根树中最小节点
function getSmallest(current) {
if(current == null) {
return current;
}
while(!(current.left == null)) {
current = current.left;
}
return current;
}
function removeNode(node, data) {
if(node == null) {
return null;
}
if(data == node.data) {
if(node.left == null && node.right == null) {
return null;
}
if(node.left == null) {
return node.right;
}
if(node.right == null) {
return node.left;
}
var tempNode = getSmallest(node.right);
node.data = tempNode.data;
node.right = removeNode(node.right, tempNode.data);
return node;
} else if(data < node.data) {
node.left = removeNode(node.left, data);
return node;
} else {
node.right = removeNode(node.right, data);
return node;
}
}
// test
var nums = new BST();
nums.insert(23);
nums.insert(45);
nums.insert(16);
nums.insert(37);
nums.insert(3);
nums.insert(99);
nums.insert(22);
println('-----');
nums.remove(23);
println('-----');
println("Inorder traversal: ");
inOrder(nums.root);
println(str);
str.length = 0;
println("Preorder traversal: ");
preOrder(nums.root);
println(str);
str.length = 0;
println("Postorder traversal: ");
postOrder(nums.root);
println(str);
str.length = 0;
// lowerValue-upperValue 之间随机整数,包含lowerValue、upperValue
function selectFrom(lowerValue, upperValue) {
var choices = upperValue - lowerValue + 1;
return Math.floor(Math.random() * choices + lowerValue);
}
function inherits(Child, Parent) {
var F = function () {};
F.prototype = Parent.prototype;
Child.prototype = new F();
Child.prototype.constructor = Child;
}
function Student(props) {
this.name = props.name || 'Unnamed';
}
Student.prototype.hello = function () {
alert('Hello, ' + this.name + '!');
}
function PrimaryStudent(props) {
// 调用Student构造函数,绑定this变量
Student.call(this, props);
this.grade = props.grade || 1;
}
// 实现原型继承链:
inherits(PrimaryStudent, Student);
// 绑定其他方法到PrimaryStudent原型:
PrimaryStudent.prototype.getGrade = function () {
return this.grade;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment