访问节点的属性javascript二进制搜索树

accessing a property of a node, javascript binary search tree

本文关键字:二进制 搜索树 javascript 属性 节点 访问      更新时间:2023-09-26

我是nodeJS javascript的新手,这里有一个简单的问题。

我有一个javascript的二进制搜索树(BST)。每个节点都有一个值和一个计数。我们在BST中插入单词,这样每个节点代表一个单词。插入后,如果单词已经在BST中,我们希望增加该单词的计数,其中count是Node上的一个属性。

当我想显示节点及其计数时,问题就来了。显示计数工作不正常。也就是说,BST.prototype.showWords=函数(节点)不正确。

感谢您的帮助和洞察力!!!文件:
bstnode.js-"节点类"BST.js-二进制搜索树类wordCount.js-读取文本文件,在空格上拆分以获取单词,并创建节点。我希望每个节点代表一个单词,如果该单词出现多次,则每次在该节点上计数++。


// wordCount.js
var BST = require('./bst.js');
var fs = require('fs');
var countNodes = require('./countNodes.js');

// THIS RIGHT HERE DOES NOT WORK CORRECTLY.
BST.prototype.showWords = function (node) {
  if (node !== null) {
    this.inOrder(node.left);
    console.log(node.showCount());
    this.inOrder(node.right);
  }
};

// get the file, and get it into an array of string-words
var filePath = './simpleTest.txt';
var contents = fs.readFileSync(filePath).toString();
var stringArr = contents.split(' ');
var myBST = new BST();
for (var i=0; i<stringArr.length; i++){
  var word = stringArr[i].trim();
  var aNode = myBST.find(word);
  console.log(aNode);
  if ( aNode !== null ) {  // then word exists in BST already,
    aNode.count++;             // node count ++ on that one
  }
  else {                    // then word dne in BST, so add it now!
    myBST.insert(word);
  }
}
myBST.showWords(myBST.root);

// bstnode.js
'use strict';
var Node = function (data, left, right) {
    this.data = data;
    this.count = 1;
    this.left = left;
    this.right = right;
};
Node.prototype.show = function () {
    return this.data;
};
Node.prototype.show2 = function () {
    return (this.data + ' ' + this.count);
};
module.exports = Node;

'use strict';
// bst.js - has the BST CTor function, and requires the bstnode in
var Node = require('./bstnode');
// BST CTor function
var BST = function() {      // Binary Search Tree class
    this.root = null;
};
BST.prototype.insert = function (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;
                }
            }
        }
    }
};
// inOrder: log VALUES in order starting from node param
BST.prototype.inOrder = function (node) {
    if (node !== null) {
        this.inOrder(node.left);
        console.log(node.show() + " ");
        this.inOrder(node.right);
    }
};
BST.prototype.find = function (data) {
    var current = this.root;
    while (current && current.data !== data) {
        if (data < current.data) {
            current = current.left;
        } else {
            current = current.right;
        }
    }
    return current;
};
module.exports = BST;

一个朋友帮了我一把,这是soln

// THIS RIGHT HERE -- - --
// That's because it should call itself recursively, not the inOrder function!
BST.prototype.showWords = function (node) {
  if (node !== null) {
    this.showWords(node.left);
    console.log(node.showCount());
    this.showWords(node.right);
  }
};