Skip to content

Commit 7463f00

Browse files
committed
Add skeletons for heap and binary tree
1 parent 8a49f80 commit 7463f00

22 files changed

+1325
-0
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
2+
3+
import BinaryTreeNode from './BinaryTreeNode';
4+
5+
export default class BinaryTree {
6+
constructor() {
7+
this.store = new BinaryTreeNode();
8+
}
9+
10+
11+
12+
13+
//Traverse the tree in-order and call callback on the value of each node.
14+
15+
inOrder(callback){
16+
17+
}
18+
19+
20+
postOrder(callback){
21+
22+
23+
}
24+
25+
26+
27+
preOrder(callback){
28+
29+
30+
}
31+
32+
33+
numberOfNodes(){
34+
35+
}
36+
37+
numberOfLeaves(){
38+
39+
}
40+
41+
42+
height(){
43+
44+
}
45+
46+
numberOfLeaves(){
47+
48+
}
49+
50+
numberOfNodes(){
51+
52+
}
53+
54+
55+
balanced(){
56+
57+
}
58+
59+
60+
degenerate(){
61+
}
62+
63+
perfect(){
64+
}
65+
66+
complete(){
67+
}
68+
69+
70+
71+
toString(){
72+
73+
}
74+
75+
}
76+
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
2+
export default class BinaryTreeNode {
3+
constructor(value, left = null, right = null) {
4+
this.value = value;
5+
this.left = left;
6+
this.right = right;
7+
}
8+
9+
10+
//Traverse the nodes in-order and call callback on the value of each node.
11+
inOrder(callback){
12+
13+
}
14+
15+
16+
17+
preOrder(callback){
18+
19+
}
20+
21+
postOrder(callback){
22+
23+
}
24+
25+
26+
height(){
27+
28+
29+
}
30+
31+
numberOfLeaves(){
32+
33+
}
34+
35+
numberOfNodes(){
36+
}
37+
38+
39+
balanced(){
40+
41+
}
42+
43+
44+
balancedHeight(){
45+
46+
}
47+
48+
49+
degenerate(){
50+
}
51+
52+
perfect(){
53+
}
54+
55+
56+
complete(){
57+
}
58+
59+
toString(){
60+
61+
}
62+
63+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
3+
import BinaryTreeNode from './BinaryTreeNode';
4+
import Stack from './../stack/Stack';
5+
6+
7+
export function inOrderIterative(tree,callback){
8+
}
9+
10+
11+
12+
13+
14+
15+
16+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
3+
import BinaryTreeNode from './BinaryTreeNode';
4+
import Stack from './../stack/Stack';
5+
6+
7+
export function postOrderIterative(tree,callback){
8+
9+
}
10+
11+
12+
13+
14+
15+
16+
17+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
f
2+
3+
import BinaryTreeNode from './BinaryTreeNode';
4+
import Stack from './../stack/Stack';
5+
6+
7+
export function preOrderIterative(tree,callback){
8+
9+
10+
}
11+
12+
13+
14+
15+
16+
17+
18+
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Binary Tree
2+
3+
4+
## Description
5+
6+
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.
7+
8+
![Binary Tree](../../../assets/Binary_tree.svg)
9+
10+
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.
11+
12+
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.
13+
14+
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.
15+
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.
16+
17+
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.
18+
19+
20+
21+
## Implementation
22+
23+
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.
24+
25+
Test functions that determines where a binary node is `balanced`, `degenerate`, `perfect` or `complete` can be written.
26+
27+
A binary tree is a data structre that stores the root binary tree node.
28+
29+
## Binary Tree Problems
30+
31+
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.
32+
33+
Write a method that takes a callback, and calls it on each node in pre-order, in-order, and post-order.
34+
35+
These methods are most easily written recursively. It is worthwhile to write these iteratively using a stack. Write
36+
37+
`inOrderIterative(tree,callback)`
38+
39+
`preOrderIterative(tree,callback)`
40+
41+
`postOrderIterative(tree,callback)`
42+
43+
44+
45+
46+
47+
48+

0 commit comments

Comments
 (0)