parent
ee9062177f
commit
7ecda3d53a
@ -1,10 +0,0 @@
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
@ -1,35 +0,0 @@
|
||||
// KMP 算法实例
|
||||
function kmpSearch(str, pattern) {
|
||||
// 生成 next 数组
|
||||
function getNext(pattern) {
|
||||
|
||||
}
|
||||
|
||||
const next = getNext(pattern);
|
||||
let i = 0;
|
||||
let j = 0;
|
||||
while (i < str.length && j < pattern.length) {
|
||||
if (j === -1 || str[i] === pattern[j]) {
|
||||
i++;
|
||||
j++;
|
||||
}
|
||||
}
|
||||
|
||||
if (j === pattern.length) {
|
||||
return i - j;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
//try to use KMP
|
||||
var strStr = function(haystack, needle) {
|
||||
return kmpSearch(haystack, needle);
|
||||
};
|
||||
// 本地git账号邮箱密码重新配置了
|
||||
// 再次尝试使用新身份提交
|
||||
|
||||
// 本地git账号邮箱密码重新配置了
|
||||
// 再次尝试使用新身份提交到非主支
|
||||
|
||||
// 本地git账号用户名改成cs了
|
||||
// 再次尝试使用新身份提交到非主支
|
@ -1,80 +0,0 @@
|
||||
// tree.js
|
||||
// 实现一个树结构
|
||||
class Tree {
|
||||
constructor(data) {
|
||||
this.data = data;
|
||||
this.children = [];
|
||||
}
|
||||
}
|
||||
module.exports = Tree;
|
||||
// 相关的方法
|
||||
// 插入子节点
|
||||
Tree.prototype.insert = function(data) {
|
||||
this.children.push(new Tree(data));
|
||||
}
|
||||
// 遍历树
|
||||
Tree.prototype.traverse = function(callback) {
|
||||
callback(this.data);
|
||||
for (let i = 0; i < this.children.length; i++) {
|
||||
this.children[i].traverse(callback);
|
||||
}
|
||||
}
|
||||
// 查找树
|
||||
Tree.prototype.find = function(data) {
|
||||
if (this.data === data) {
|
||||
return this;
|
||||
}
|
||||
for (let i = 0; i < this.children.length; i++) {
|
||||
let result = this.children[i].find(data);
|
||||
if (result) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
// 删除树
|
||||
Tree.prototype.remove = function(data) {
|
||||
for (let i = 0; i < this.children.length; i++) {
|
||||
if (this.children[i].data === data) {
|
||||
this.children.splice(i, 1);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
// 清空树
|
||||
Tree.prototype.clear = function() {
|
||||
this.children = [];
|
||||
}
|
||||
// 树的高度
|
||||
Tree.prototype.height = function() {
|
||||
let height = 0;
|
||||
for (let i = 0; i < this.children.length; i++) {
|
||||
let childHeight = this.children[i].height();
|
||||
if (childHeight > height) {
|
||||
height = childHeight;
|
||||
}
|
||||
}
|
||||
return height + 1;
|
||||
}
|
||||
// 树的深度
|
||||
Tree.prototype.depth = function() {
|
||||
let depth = 0;
|
||||
let parent = this.parent;
|
||||
while (parent) {
|
||||
depth++;
|
||||
parent = parent.parent;
|
||||
}
|
||||
return depth;
|
||||
}
|
||||
// 树的大小
|
||||
Tree.prototype.size = function() {
|
||||
let size = 1;
|
||||
}
|
||||
// 树的叶子节点
|
||||
Tree.prototype.leaf = function() {
|
||||
let leaf = 0;
|
||||
}
|
||||
// 树的节点数
|
||||
Tree.prototype.node = function() {
|
||||
let node = 0;
|
||||
}
|
@ -1,63 +0,0 @@
|
||||
//实现二叉树
|
||||
class BinaryTree {
|
||||
constructor(data) {
|
||||
this.data = data;
|
||||
this.left = null;
|
||||
this.right = null;
|
||||
}
|
||||
}
|
||||
BinaryTree.prototype.insert = function(data) {
|
||||
if (data < this.data) {
|
||||
if (this.left === null) {
|
||||
this.left = new BinaryTree(data);
|
||||
}
|
||||
}
|
||||
}
|
||||
BinaryTree.prototype.traverse = function(callback) {
|
||||
if (this.left !== null) {
|
||||
this.left.traverse(callback);
|
||||
}
|
||||
}
|
||||
// 查找树
|
||||
BinaryTree.prototype.find = function(data) {
|
||||
if (this.data === data) {
|
||||
return this;
|
||||
}
|
||||
for (let i = 0; i < this.children.length; i++) {
|
||||
let result = this.children[i].find(data);
|
||||
if (result) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
// 删除树
|
||||
BinaryTree.prototype.remove = function(data) {
|
||||
for (let i = 0; i < this.children.length; i++) {
|
||||
if (this.children[i].data === data) {
|
||||
this.children.splice(i, 1);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
// 清空树
|
||||
BinaryTree.prototype.clear = function() {
|
||||
this.children = [];
|
||||
}
|
||||
// 树的高度
|
||||
BinaryTree.prototype.height = function() {
|
||||
let height = 0;
|
||||
}
|
||||
// 树的深度
|
||||
BinaryTree.prototype.depth = function() {
|
||||
let depth = 0;
|
||||
}
|
||||
// 树的大小
|
||||
BinaryTree.prototype.size = function() {
|
||||
let size = 0;
|
||||
}
|
||||
// 树的叶子节点
|
||||
BinaryTree.prototype.leaf = function() {
|
||||
let leaf = 0;
|
||||
}
|
||||
module.exports = BinaryTree;
|
Loading…
Reference in new issue