Thursday 15 April 2010

node.js - accessing a property of a node, javascript binary search tree -



node.js - accessing a property of a node, javascript binary search tree -

i'm new nodejs javascript , have simple problem here.

i have binary search tree (bst) in javascript. each node has value, , count. we're inserting words bst such each node represents word. upon insertion, if word in bst, want increment count of word, count property on node.

my problem comes when want display nodes , counts. displaying counts not working correctly. is, bst.prototype.showwords = function (node) not correct.

thank help , insight!!! files: bstnode.js - 'node class' bst.js - binary search tree class wordcount.js - reads in text file, splits on spaces words, , creates nodes. want each node represent word, , if word appears multiple times, count++ on node each time.

// wordcount.js var bst = require('./bst.js'); var fs = require('fs'); var countnodes = require('./countnodes.js'); // right here not work correctly. bst.prototype.showwords = function (node) { if (node !== null) { this.inorder(node.left); console.log(node.showcount()); this.inorder(node.right); } }; // file, , array of string-words var filepath = './simpletest.txt'; var contents = fs.readfilesync(filepath).tostring(); var stringarr = contents.split(' '); var mybst = new bst(); (var i=0; i<stringarr.length; i++){ var word = stringarr[i].trim(); var anode = mybst.find(word); console.log(anode); if ( anode !== null ) { // word exists in bst already, anode.count++; // node count ++ on 1 } else { // word dne in bst, add together 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 () { homecoming this.data; }; node.prototype.show2 = function () { homecoming (this.data + ' ' + this.count); }; module.exports = node; 'use strict'; // bst.js - has bst ctor function, , requires 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 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; } } homecoming current; }; module.exports = bst;

a friend helped me out, here's soln

// right here -- - -- // that's because should phone call recursively, not inorder function! bst.prototype.showwords = function (node) { if (node !== null) { this.showwords(node.left); console.log(node.showcount()); this.showwords(node.right); } };

javascript node.js binary-tree binary-search-tree

No comments:

Post a Comment