Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

recursion: sort binary search tree #16

Merged
merged 6 commits into from
May 13, 2020
Merged

Conversation

VeronicaM
Copy link
Owner

@VeronicaM VeronicaM commented May 11, 2020

All PRs

  • I have self-reviewed my code
  • I have written tests which cover new and changed functionality
  • All tests pass
  • I have commented on difficult to understand code

Where Relevant

  • I have compared and validated my UI changes against the designs
  • I've updated relevant documentation e.g. README

Description

What is this PR about ?

Sort a search tree

What was implemented ?

Sort a search tree using recursion

  1. Identified base case as when the current tree node is null: if(!tree.getNode()) return
  2. Identified the recursive case a double recursion for both left and right tree
  • Used a first recursive call to build the sorted left tree by concatenating its current node once the recursion algorithm starts to pop calls one by one in reverse order from its stack.

  • Used a second recursive call to build the sorted right tree through the first recursive call

  • Returned the combined left and right tree which are now sorted and filtered out undefined values that were pushed as the recursive calls hit an undefined node.

Runtime and space complexity: O(n x m) where n and m are the depth of the right and left trees

What did you learn ?

  • What a binary search tree is

  • How to represent a binary search tree as an object with currentNode value and nested left and right trees

  • How to use recursion to walk through all its nodes both in the right and left trees

  • How to use concatenation in a recursive algorithms to avoid having to store the result array outside of the recursive function thus introducing side effects.

What links/documents relate to this PR ?

Testing

Steps required to test

  • cd interviews/recursion/sort-binary-tree
  • run node sort-binary-tree.js

All tests should pass
Test cases can be found in interviews/recursion/sort-binary-tree/testData

@VeronicaM VeronicaM added the WIP label May 11, 2020
@VeronicaM VeronicaM changed the title recursion: sort binary tree recursion: sort binary tree - WIP May 11, 2020
@VeronicaM VeronicaM removed the WIP label May 13, 2020
@VeronicaM VeronicaM changed the title recursion: sort binary tree - WIP recursion: sort search tree - WIP May 13, 2020
@VeronicaM VeronicaM changed the title recursion: sort search tree - WIP recursion: sort binary search tree May 13, 2020
@VeronicaM VeronicaM merged commit bc7cedb into master May 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants