Giter Club home page Giter Club logo

binary-search-trees's Introduction

Binary-Search-Tree

The goal here was to create a balanced binary search tree in Javascript with the following functions;

  • buildTree(array) takes an array of data and turns it into a balanced binary tree full of Node objects
  • prettyprint() will console.log the tree in a structured format
  • insert(value) & delete(value) insert or remove the node containing the given value
  • find(value) finds the node containing the given value
  • levelOrder(callback) performs a breadth-first search and provide each node as the argument to the provided function
  • preorder(callback), inorder(callback) & postorder(callback) perform a depth-first search (DLR, LDR & LRD) and yield each node to the provided function as an argument
  • height(node) accepts a node and returns it's height (the number of edges in the longest path between the node and a leaf node)
  • depth(node) accepts a node and returns it's height (the number of edges in the longest path between the node and the tree's root node)
  • isBalanced() checks if the tree is balanced (the height of the every node on the left/right subtrees must be no more than 1)
  • rebalance() rebalances an unbalanced tree

Thoughts

Overall this was really useful practice to solidify my understanding of recursion. I spent absolutely ages on working out the solution to the find method. Solving the recursive element of the algorithm was pretty straightforward but it took me a while to work out the logic for removing a node with 2 children.

A lot of the issues I had with working on these algorithms was that most of the articles I found online would just give you the pseudocode solution as part of the explanation. Once I understood the logic I never had much trouble writing out the Javascript version so I was a little unsure if this was the optimal way to learn.

Either way I find solving these kind of puzzles extremely satisfying! There is a certain elegance in how optimised some of these algorithms are . I am concerned that I can't really see myself using these for web development very often though, so I'll most likely forget the logic and have to return to this repo in the future (Hi, future self!)

binary-search-trees's People

Contributors

casssb 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.