Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Readme demo results in errors #12

Open
freiit opened this issue Jan 27, 2022 · 5 comments · May be fixed by #13
Open

Readme demo results in errors #12

freiit opened this issue Jan 27, 2022 · 5 comments · May be fixed by #13
Labels

Comments

@freiit
Copy link

freiit commented Jan 27, 2022

I just wanted to try out the project and copy/pasted the example in the README-file.

var tree = new AvlTree();
           ^

TypeError: AvlTree is not a constructor

Anything I can do to get this up and running?

@Tyriar
Copy link
Member

Tyriar commented Jan 27, 2022

How are you importing it? The readme shows:

var AvlTree = require('@tyriar/avl-tree');

Which would mean you may need to do this for import:

import * as AvlTree from '@tyriar/avl-tree';

You could log/debug AvlTree to see what it is which might also give you your answer.

@freiit
Copy link
Author

freiit commented Feb 1, 2022

I copy/pasted the code 1:1 from the README file: see gist and executed it with node dummy.js.

@Tyriar
Copy link
Member

Tyriar commented Feb 1, 2022

Looks like the readme is a little out of date, the following works for me which has a change on the require line and also changes .size() to .size:

// Import npm module
var AvlTree = require('@tyriar/avl-tree').AvlTree;

// Construct AvlTree
var tree = new AvlTree();

// Insert keys
tree.insert(1);
tree.insert(2);
tree.insert(3);
tree.insert(4);
tree.insert(5);
console.log('size: ' + tree.size);
console.log('contains 2: ' + tree.contains(2));
console.log('contains 7: ' + tree.contains(7));
// > size: 5
// > contains 2: true
// > contains 7: false

// Delete a key
tree.delete(2);
console.log('size: ' + tree.size);
console.log('contains 2: ' + tree.contains(2));
// > size: 4
// > contains 2: false

// Construct custom compare AvlTree
tree = new AvlTree(function (a, b) {
  return a.localeCompare(b);
});
tree.insert('a');
tree.insert('A');
tree.insert('b');
tree.insert('B');

// Delete the minimum key
var minKey = tree.findMinimum();
tree.delete(minKey);
console.log('minKey: ' + minKey);
console.log('new minKey: ' + tree.findMinimum());
// > min key: 'a'
// > new min key: 'A'

FYI the module actually uses the TypeScript port at https://github.com/gwtw/ts-avl-tree, so @tyriar/avl-tree points at that repo

@Tyriar Tyriar reopened this Feb 1, 2022
@Tyriar Tyriar changed the title AvlTree is not a constructor Readme demo results in errors Feb 1, 2022
@Tyriar Tyriar added bug and removed question labels Feb 1, 2022
@freiit
Copy link
Author

freiit commented Feb 1, 2022

Thanks. :-) Shall I make a patch?

@Tyriar
Copy link
Member

Tyriar commented Feb 1, 2022

Sure, but I think the most correct thing to do would be call out this is mainly for education and recommend https://github.com/gwtw/ts-avl-tree instead actual usage

freiit added a commit to freiit/js-avl-tree that referenced this issue Feb 7, 2022
@freiit freiit linked a pull request Feb 7, 2022 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants