Skip to content
/ bst Public

Binary Search Tree related functions in Ruby, JS and Python

Notifications You must be signed in to change notification settings

mcrd25/bst

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Binary Search Tree Implementations and Vizualizations

This is a project with implementations of a binary search tree data structure and it's traversal methods in Python, Ruby, and JavaScript.

Getting Started

These instructions will help you set up and run the code in your local machine.

Prequisites

  • Python 3.x
  • Ruby 3.x
  • NodeJS 18.x

Installing

Clone or download the repository to your local machine and then navigate to the project directory in your terminal. git clone https://github.com/mcrd25/bst.git
cd bst

Running the code

To run the code currently in the project repository, run the following commands in the terminal:

Python

python3 main.py
The code takes in an array of integers and creates a binary search tree. It then outputs the pre-order and in-order traversal of the tree.

Ruby

ruby bst_pre_order.rb
The code takes in an array of integers and creates a binary search tree. It then outputs the pre-order traversal of the tree.
ruby bst_in_order.rb
The code takes in an array of integers and creates a binary search tree. It then outputs the in-order traversal of the tree.

Code Structure

Python

The code is composed of two main classes: Node and BinarySearchTree. The Node class has two instance variables, data and left and right, which represent the value stored in the node and the left and right child nodes, respectively. The BinarySearchTree class has one instance variable, root, which represents the root node of the tree.

The BinarySearchTree class contains three methods: add_to_search_tree, pre_order_traversal, and in_order_traversal. The add_to_search_tree method takes in a node and data (most likely an integer in our exampls), and adds a new node to the binary search tree by following the binary search tree property. The pre_order_traversal method takes in a node and returns the pre-order traversal of the tree starting from that node and liekwise with the in_order_traversal method returning the in-order traversal of the tree.

The code also contains three functions: binary_search_tree_pre_order, binary_search_tree_in_order and insert_array (HelperMethods module). The binary_search_tree functions takes in an array of integers, creates a binary search tree using the insert_array function, and returns the pre-order and in-order traversal of the tree as a string. The insert_array function takes in an array of integers and returns a binary search tree created by inserting each element of the array into the tree.

Ruby

Basically the same structure except the pre-order traversals and in-order traversals are in their own scripts and not part of the BinarySearchTree class (Tree.rb). It's pretty straightforward.

References

Authors

👤 Maya D. - child of God

🤝 Contributing

Contributions, issues, and feature requests are welcome!

Feel free to check the issues page.

Show your support

Give a ⭐️ if you like this project!

Acknowledgments

📝 License

About

Binary Search Tree related functions in Ruby, JS and Python

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages