- Notifications
You must be signed in to change notification settings - Fork407
update the second chapter exercises#16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Open
BubbleM wants to merge10 commits intooreillymedia:masterChoose a base branch fromBubbleM:master
base:master
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Uh oh!
There was an error while loading.Please reload this page.
Open
Changes fromall commits
Commits
Show all changes
10 commits Select commitHold shift + click to select a range
f8de2e5
add Chapter2 课后习题练习部分
hackerYun84d922d
learn chapter3 List
hackerYuna0ee25c
学习Chapter4 栈并基于栈实现了进制转换、回文、递归
hackerYunecbc7d1
add Queue files about practice
hackerYun9894ace
add LList files about practice
hackerYun8e05a7c
updata Dictionart class showAll() method
hackerYun35392a2
update Chapter8 Hash class
hackerYund4f2645
update and study Chapter10 BST file
hackerYun018098a
add Chapter11 Graph file
hackerYunb1b1dac
add Chap12 Sort
hackerYunFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Binary file removed9781449364939_files.zip
Binary file not shown.
37 changes: 37 additions & 0 deletionsChapter1/test.js
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// function factorial(number){ | ||
// if(number == 1){ | ||
// return number; | ||
// }else{ | ||
// return number * factorial(number-1); | ||
// } | ||
// } | ||
// print(factorial(5)); | ||
// function compare(num1, num2) { | ||
// return num1 - num2; | ||
// } | ||
// var nums = [3,1,2,100,4,200]; | ||
// nums.sort(compare); | ||
// print(nums); // 1,2,3,4,100,200 | ||
// Array.matrix = function(numrows,numcols,initial){ | ||
// var arr = []; | ||
// for(var i=0;i<numrows;i++){ | ||
// var columns = []; | ||
// for(var j=0;j<numcols;++j){ | ||
// columns[j]=initial; | ||
// } | ||
// arr[i]=columns; | ||
// } | ||
// return arr; | ||
// } | ||
// var nums = Array.matrix(5,5,[1,2,3]); | ||
// print(nums); | ||
var grades = [[89, 77, 78],[76, 82, 81],[91, 94, 89]]; | ||
print(grades[0][0]); | ||
print(grades[0][1]); | ||
print(grades[0][2]); |
306 changes: 125 additions & 181 deletionsChapter10/bst.js
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,195 +1,139 @@ | ||
//二叉查找树:一种特殊的二叉树,相对较小的值保存在左节点中,较大的值保存在右节点中。 | ||
//实现二叉查找树 | ||
function Node(data,left,right){ | ||
this.data = data; | ||
this.count = 1; | ||
this.left = left; | ||
this.right = right; | ||
this.show = show; | ||
} | ||
function show(){ | ||
return this.data; | ||
} | ||
function BST(){ //二叉查找树 | ||
this.root = null;//根节点 初始化为null,以此创建一个空节点 | ||
this.insert = insert; | ||
this.inOrder = inOrder; | ||
this.preOrder = preOrder; | ||
this.postOrder = postOrder; | ||
this.getMax = getMax; | ||
this.getMin = getMin; | ||
this.find = find; | ||
this.update = update; | ||
} | ||
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){//如果当前节点的左节点为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); | ||
putstr(node.show() + " "); | ||
inOrder(node.right); | ||
} | ||
} | ||
//先序 | ||
function preOrder(node){ | ||
if(!(node == null)){ | ||
putstr(node.show() + " "); | ||
preOrder(node.left); | ||
preOrder(node.right); | ||
} | ||
} | ||
//后序 | ||
function postOrder(node){ | ||
if(!(node == null)){ | ||
postOrder(node.left); | ||
postOrder(node.right); | ||
putstr(node.show() + " "); | ||
} | ||
} | ||
//查找最大值 | ||
function getMax(){ | ||
var current = this.root; | ||
while(!(current.right == null)){ | ||
current = current.right; | ||
} | ||
return current.data; | ||
} | ||
//查找最小值 | ||
// 在BST上因为较小的值总是在左子节点上,查找最小值,只需要遍历左子树,直到找到最后一个节点 | ||
function getMin(){ | ||
var current = this.root; | ||
while(!(current.left == null)){ | ||
current = current.left; | ||
} | ||
return current.data; | ||
} | ||
//查找给定值 | ||
function find(data){ | ||
var current = this.root; | ||
while( current != null){ | ||
if(current.data == data){ | ||
return current; | ||
}else if(data < current.data){ | ||
current = current.left; | ||
}else{ | ||
current = current.right; | ||
} | ||
} | ||
return null;//如果找到给定值,该方法返回保存该值的节点;如果没找到,该方法返回 null。 | ||
} | ||
//从二叉查找树上删除节点 | ||
function remove(data){ //简单地接受待删除数据 | ||
root = removeNode(this.root,data); | ||
} | ||
function removeNode(node,data){//删除节点 | ||
if(node == null){ | ||
return null; | ||
} | ||
if(data == node.data){ //判断当前节点是否包含待删除的数据 | ||
if(node.left == null && node.right == null){ //没有子节点的节点 叶子节点 | ||
return null;//只需将从父节点指向它的链接指向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; | ||
} | ||
} | ||
function update(data){ | ||
var grade = this.find(data); | ||
grade.count++; | ||
return grade; | ||
} | ||
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.