Giter Site home page Giter Site logo

frhd143 / binarysearchtree Goto Github PK

View Code? Open in Web Editor NEW
2.0 1.0 0.0 8 KB

This repository contains an implementation of Binary Search Tree (BST) data structure in Python.

License: MIT License

Python 100.00%
binary-search-tree binary-tree data-structures cpp data-science

binarysearchtree's Introduction

Binary Search Tree (BST) Implementation in Python

This repository contains an implementation of Binary Search Tree (BST) data structure in Python.

Overview

A Binary Search Tree is a binary tree in which each node has at most two children, and for each node, all the values in the left subtree are less than the node's value, and all the values in the right subtree are greater than the node's value.

The following operations can be performed on a BST:

  • insert(element): inserts a new node into the tree
  • remove(element): removes a node from the tree
  • find(element): finds an element in the tree
  • pre_order_walk(): returns a list of the nodes traversed in pre-order
  • in_order_walk(): returns a list of the nodes traversed in in-order
  • post_order_walk(): returns a list of the nodes traversed in post-order
  • get_tree_height(): returns the height of the tree
  • get_min(): returns the smallest node in the tree
  • get_max() returns the largest node in the tree
  • to_graphviz(): returns a code the can be used to visualize the tree

Usage

To use the BST tree, simply import the BST class from the module and create an instance of it:

from BST import BST

tree = BST()

You can then use the methods listed above to insert, remove, and find elements in the tree.

tree.insert(5)
tree.insert(3)
tree.insert(8)

print(tree.find(3)) # True
print(tree.find(10)) # False

print(tree.pre_order_walk()) 
print(tree.in_order_walk()) 

You can also use https://dreampuf.github.io/GraphvizOnline/ to visulize the tree. To this you need to call the to_graphviz() function:

print(tree.to_graphviz())

The function prints the code you need to copy and paste in the website above.

Contributing

Contributions to this project are welcome. If you find a bug or want to suggest an improvement, please open an issue or submit a pull request. Or email me here: [email protected]

License

This code is released under the MIT License.

binarysearchtree's People

Contributors

frhd143 avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.