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

Changes to data structures to fix bugs: issue #6 #7

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ The problem sets below contain information on key data structures, sorting/searc
- [Stack](solutions/data-structures/stack)
- [Linked List](solutions/data-structures/linked-list)
- [Doubly Linked List](solutions/data-structures/doubly-linked-list)
- [Heap](solutions/data-structures/heap)
- [Binary Tree](solutions/data-structures/binary-tree)
- [Hash Table](solutions/data-structures/hash-table)
- [Binary Search Tree](solutions/data-structures/tree/binary-search-tree)
- [Binary Search Tree](solutions/data-structures/binary-search-tree)
- **Sorting**
- [Bubble Sort](solutions/algorithms/sorting/bubble-sort)
- [Selection Sort](solutions/algorithms/sorting/selection-sort)
Expand All @@ -39,6 +41,9 @@ The problem sets below contain information on key data structures, sorting/searc
- [Factorial](solutions/algorithms/numbers/factorial)
- [Fibonacci Number](solutions/algorithms/numbers/fibonacci)
- [Prime Number](solutions/algorithms/numbers/prime)
- [Happy Number](solutions/algorithms/numbers/happy)
- [Ugly Number](solutions/algorithms/numbers/ugly)
- [Lexicographical Numbers](solutions/algorithms/numbers/lexicographical)

## How it works

Expand Down
242 changes: 242 additions & 0 deletions assets/Binary_tree.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
76 changes: 76 additions & 0 deletions skeletons/data-structures/binary-tree/BinaryTree.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@


import BinaryTreeNode from './BinaryTreeNode';

export default class BinaryTree {
constructor() {
this.store = new BinaryTreeNode();
}




//Traverse the tree in-order and call callback on the value of each node.

inOrder(callback){

}


postOrder(callback){


}



preOrder(callback){


}


numberOfNodes(){

}

numberOfLeaves(){

}


height(){

}

numberOfLeaves(){

}

numberOfNodes(){

}


balanced(){

}


degenerate(){
}

perfect(){
}

complete(){
}



toString(){

}

}

63 changes: 63 additions & 0 deletions skeletons/data-structures/binary-tree/BinaryTreeNode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@

export default class BinaryTreeNode {
constructor(value, left = null, right = null) {
this.value = value;
this.left = left;
this.right = right;
}


//Traverse the nodes in-order and call callback on the value of each node.
inOrder(callback){

}



preOrder(callback){

}

postOrder(callback){

}


height(){


}

numberOfLeaves(){

}

numberOfNodes(){
}


balanced(){

}


balancedHeight(){

}


degenerate(){
}

perfect(){
}


complete(){
}

toString(){

}

}
16 changes: 16 additions & 0 deletions skeletons/data-structures/binary-tree/InOrderIterative.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@


import BinaryTreeNode from './BinaryTreeNode';
import Stack from './../stack/Stack';


export function inOrderIterative(tree,callback){
}








17 changes: 17 additions & 0 deletions skeletons/data-structures/binary-tree/PostOrderIterative.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@


import BinaryTreeNode from './BinaryTreeNode';
import Stack from './../stack/Stack';


export function postOrderIterative(tree,callback){

}








18 changes: 18 additions & 0 deletions skeletons/data-structures/binary-tree/PreOrderIterative.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
f

import BinaryTreeNode from './BinaryTreeNode';
import Stack from './../stack/Stack';


export function preOrderIterative(tree,callback){


}








48 changes: 48 additions & 0 deletions skeletons/data-structures/binary-tree/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Binary Tree


## Description

Just as a list is a data structure with nodes, where each node has one child, the next node, a binart tree is a data structure made up of nodex where each node has two children, a left child and a right child. The first node is called the root, and in Computer Science, trees are drawn downwards, for some reason.

![Binary Tree](../../../assets/Binary_tree.svg)

As well as a left and right child, binary tree nodes usually have a value. In the aboe picture, the number displayed is the value of the node. Sometimes nodes have a parent pointer, and sometimes they even have a pointer to their siblings or uncles.

We say a node's height is one greater than the max height of its children. A node without children has height one. A node without children is called a leaf. A node with children is called an interior node. Sometimes nodes will kepp track of their height. The depth of a node is how far it is from the root. The root has depth 1, its children have depth 2, etc.

Serveral kinds of binary tree are distinguised. A degenerate binary tree is one that have only one or zero children at each node. This means it is similar to a list.
A full binary tree has 0 or 2 nodes at every level. A complete binary tree has two nodes at every level, save the lowest level. At the lowest level, all nodes are as far left as possible. This becomes important later when we discuss heaps.

A perfect binary tree of height h, has all interior nodes with two children, and all leaves are at depth n. A balanced binary tree is one where the left and right subtrees of every node differ in height by no more than 1.



## Implementation

The usual minimal implementation of a binary tree node has a constructor that sets its value, left and right. Helper functions can copute the `height` of a node recursively. The `numberOfLeaves` and `numberOfNodes` are also easily computed recursively.

Test functions that determines where a binary node is `balanced`, `degenerate`, `perfect` or `complete` can be written.

A binary tree is a data structre that stores the root binary tree node.

## Binary Tree Problems

A binary tree can be traversed in several ways. In order traversal visits the left tree, then the right tree, and then visits the root. Pre-order visits the root, then the left and then the right subtree. Post-order visits the left subtree, then the right sibtree, then the root.

Write a method that takes a callback, and calls it on each node in pre-order, in-order, and post-order.

These methods are most easily written recursively. It is worthwhile to write these iteratively using a stack. Write

`inOrderIterative(tree,callback)`

`preOrderIterative(tree,callback)`

`postOrderIterative(tree,callback)`







Loading