Wasm Builders 🧱

Cover image for Inorder Successor in Binary Search Tree with C++ and WASM
Silvana
Silvana

Posted on

Inorder Successor in Binary Search Tree with C++ and WASM

Hello, today we are going to show you a little about using wasm and c++.
To get started, lets do the install part

Environment Setup

To compile your demo, you must install the following:

C++
[https://docs.microsoft.com/en-us/cpp/build/vscpp-step-0-installation?view=msvc-170]

Wasmer

Wasmer is an open-source runtime for executing WebAssembly on the Server.
[https://docs.wasmer.io/]

Wasienv

Wasienv is a tool to compile different programming languages to WebAssembly, so you can run your programs on the Browser, or in the Server.
[https://github.com/wasienv/wasienv]

Wasmtime

You will find wasmtime at [https://wasmtime.dev/]

The example we are going to use is related to Binary tree. In Binary Tree, Inorder successor of a node is the next node in Inorder traversal of the Binary Tree. Inorder Successor is NULL for the last node in Inorder traversal.

In Binary Search Tree, Inorder Successor of an input node can also be defined as the node with the smallest key greater than the key of the input node. So, it is sometimes important to find next node in sorted order.

Image description

The file used is:

Image description

Image description

Image description

Compiling Using g++

g++ TestBst.cpp

Compile to WASM Binary using the following Command:
Since the Code has been written in C++, we need to figure out a way to generate a WebAssembly Binary. That's why we will be using wasienv in order to generate a .wasm binary from this cpp file.
When you have your cpp file created, you can execute wasic++

wasic++ testBst.cpp -o testBst.wasm

Executing it using WASM Runtime

wasmtime testBst.wasm

Image description

This is the result of using C++ and wasm.
Please if you have any tips, feel free to comment.

Latest comments (0)