-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree_wasm.cpp
More file actions
71 lines (58 loc) · 1.67 KB
/
Copy pathtree_wasm.cpp
File metadata and controls
71 lines (58 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <emscripten/bind.h>
#include <sstream>
#include "BinaryTree.hpp"
using namespace emscripten;
// Wrapper class to expose BinaryTree<int> with JS-friendly methods
class BinaryTreeWrapper {
private:
BinaryTree<int> tree;
public:
BinaryTreeWrapper() : tree() {}
void insert(int item) {
tree.insert(item);
}
void remove(int item) {
tree.remove(item);
}
bool find(int item) {
return tree.find(item) != nullptr;
}
int nodeCount() {
return tree.nodeCount();
}
int height() {
return tree.height();
}
int leavesCount() {
if (tree.nodeCount() == 0) return 0;
return tree.leavesCount();
}
std::string preOrder() {
std::ostringstream oss;
tree.preOrder(oss);
return oss.str();
}
std::string inOrder() {
std::ostringstream oss;
tree.inOrder(oss);
return oss.str();
}
std::string postOrder() {
std::ostringstream oss;
tree.postOrder(oss);
return oss.str();
}
};
EMSCRIPTEN_BINDINGS(tree_module) {
class_<BinaryTreeWrapper>("BinaryTree")
.constructor<>()
.function("insert", &BinaryTreeWrapper::insert)
.function("remove", &BinaryTreeWrapper::remove)
.function("find", &BinaryTreeWrapper::find)
.function("nodeCount", &BinaryTreeWrapper::nodeCount)
.function("height", &BinaryTreeWrapper::height)
.function("leavesCount", &BinaryTreeWrapper::leavesCount)
.function("preOrder", &BinaryTreeWrapper::preOrder)
.function("inOrder", &BinaryTreeWrapper::inOrder)
.function("postOrder", &BinaryTreeWrapper::postOrder);
}