下面的代码找不到错误的地方,让我知道是什么问题...我试图构造二进制搜索树,但无法构造。
function BinarySearchTree(){
var Node = function(key){
this.key = key;
this.left = null;
this.right = null;
};
var root = null;
}
//Inserting a Key into a tree
this.insert = function(key){
var newNode = new Node(key);
if(root === null){
root = newNode;
}else{
insertNode(root, newNode);
}
};
var insertNode = function(node, newNode){
if(newNode.key < node.key){
if(node.left === null){
node.left = newNode;
}else{
insertNode(node.left, newNode);
}
}else{
if(node.right === null){
node.right = newNode;
}else{
insertNode(node.right, newNode);
}
}
};
var tree = BinarySearchTree();
tree.insert(11);
答案 0 :(得分:2)
评论者提到了代码的一些问题,因为它们导致了挂断。首先,您在this
或constructor
函数外部使用prototype
,因此this
将被评估为引用了全局对象。另外,当您将BinarySearchTree
分配给变量tree
时,您不是在调用构造函数,因此,如果要使用console.log(tree)
,它将返回值undefined
。 / p>
然后如何解决您的问题?
首先,您可以在BinarySearchTree
(此后称为BST)定义中添加检查,以返回对象的实例,以防万一您忘记了new
关键字。
if (!(this instanceof BinarySearchTree)) {
return new BinarySearchTree();
}
第二,您想通过将Node
替换为root
和var
,将this.Node
构造函数和this.root
变量绑定到BST对象。现在,您以后可以在要定义的各种prototype
函数中访问它们。
第三,如上所述,重构对insert
函数的定义如下:
BinarySearchTree.prototype.insert = function(){}
当然,请使用适当的代码填写该函数。对insertNode
做同样的事情。
BinarySearchTree.prototype.insertNode = function(){}
我并不是说代码会起作用,但这应该会创建树作为对象所需要的原型继承。
重构
function BinarySearchTree(){
if (!(this instanceof BinarySearchTree)) {
return new BinarySearchTree();
}
this.Node = function(key){
this.key = key;
this.left = null;
this.right = null;
};
this.root = null;
}
//Inserting a Key into a tree
BinarySearchTree.prototype.insert = function(key){
var newNode = new this.Node(key);
if(this.root === null){
this.root = newNode;
}else{
this.insertNode(this.root, newNode);
}
};
BinarySearchTree.prototype.insertNode = function(node, newNode){
if(newNode.key < node.key){
if(node.left === null){
node.left = newNode;
}else{
this.insertNode(node.left, newNode);
}
}else{
if(node.right === null){
node.right = newNode;
}else{
this.insertNode(node.right, newNode);
}
}
};
var tree = BinarySearchTree();
tree.insert(11);
答案 1 :(得分:1)
您需要进行一些更改才能使其正常运行。
new
扩展“构造函数”功能BinarySearchTree
创建function BinarySearchTree(){
this.Node = function(key){
this.key = key;
this.left = null;
this.right = null;
};
this.root = null;
}
//Inserting a Key into a tree
BinarySearchTree.prototype.insert = function(key){
var newNode = new this.Node(key);
if(this.root === null){
this.root = newNode;
}else{
this.insertNode(this.root, newNode);
}
};
BinarySearchTree.prototype.insertNode = function(node, newNode){
if(newNode.key < node.key){
if(node.left === null){
node.left = newNode;
}else{
this.insertNode(node.left, newNode);
}
}else{
if(node.right === null){
node.right = newNode;
}else{
this.insertNode(node.right, newNode);
}
}
};
var tree = new BinarySearchTree();
tree.insert(11);
tree.insert(12);
tree.insert(10);
console.log(tree.root)
的实例
After that try with this code---
<ngx-datatable #myTable
class="material server-scrolling-table"
[rows]="rows"
[columns]="columns"
[columnMode]="'force'"
[headerHeight]="headerHeight"
[rowHeight]="rowHeight"
[loadingIndicator]="isLoading"
[selectionType]="'checkbox'"
[scrollbarV]="true"
[hidden]="hideTable"
(scroll)="onScroll($event.offsetY)"
>
<ngx-datatable-row-detail [rowHeight]="100" #myDetailRow (toggle)="onDetailToggle($event)">
<ng-template let-row="row" let-expanded="expanded" ngx-datatable-row-detail-template>
<div style="padding-left:35px;">
<div><strong>Address</strong></div>
<div>{{row.address.city}}</div>
</div>
</ng-template>
</ngx-datatable-row-detail>
<ngx-datatable-column *ngFor="let col of columns"
[name]="col.prop">
</ngx-datatable-column>
<ngx-datatable-column [width]="50"
[resizeable]="false"
[sortable]="false"
[draggable]="false"
[canAutoResize]="false">
<ng-template let-row="row" let-expanded="expanded" ngx-datatable-cell-template>
<a href="javascript:void(0)"
[class.datatable-icon-right]="!expanded"
[class.datatable-icon-down]="expanded"
title="Expand/Collapse Row"
(click)="toggleExpandRow(row)">Click on this link
</a>
</ng-template>
</ngx-datatable-column>
</ngx-datatable>