Skip to content

Instantly share code, notes, and snippets.

@bomsy
Last active October 20, 2019 21:12
Show Gist options
  • Save bomsy/0e5aa93a039197a33cd8fdf594afeb2d to your computer and use it in GitHub Desktop.
Save bomsy/0e5aa93a039197a33cd8fdf594afeb2d to your computer and use it in GitHub Desktop.

Binary Search Tree

insert, delete, search - O(logn)

The key of the current node must be greater than the key of the left child node and less than the key of the right child node.

Non-linear datastructure

  • e.g of linear data structures arrays, queues, linkedlists, stacks

root, child, parent, ansestor, desendant, leaf, siblings

Binary tree - node with at most 2 children

Tree Traversal

BFS - breadth first search

DFS - depth first search preorder, postorder, inorder

Inorder - best for bst, would always return a sorted list

  • traverse left
  • display node
  • traverse right

Preorder

  • display node
  • traverse left
  • traverse right

Postorder

  • traverse left
  • traverse right
  • display node

As with all binary trees, one may conduct a pre-order traversal or a post-order traversal, but neither are likely to be useful for binary search trees. An in-order traversal of a binary search tree will always result in a sorted list of node items (numbers, strings or other comparable items).

O(n) as every node must be visited

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment