访问节点的属性,javascript二叉搜索树

时间:2014-11-13 14:39:54

标签: javascript node.js binary-tree binary-search-tree

我是nodeJS javascript的新手,我在这里遇到了一个简单的问题。

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

当我想显示节点及其计数时,我的问题出现了。显示计数无法正常工作。也就是说,BST.prototype.showWords = function(node)不正确。

感谢您的帮助和见解!!! 文件:
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;

1 个答案:

答案 0 :(得分:0)

一位朋友帮助了我,这里是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);
  }
};