Skip to content

Commit a81bcda

Browse files
committed
adds script for binary trees
1 parent 1ccb7c7 commit a81bcda

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

trees/binaryTreeScript.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Binary Trees and Tree Traversal
2+
3+
Binary trees are trees whose nodes can only have up to two children, hence binary. We store this data as the left and right of a node.
4+
5+
We'll use a function to create our binary node object. a binary node receives a key as an argument, has a left and right property set to null, as well as methods to addLeft and addRight.
6+
7+
addLeft and addRight receive a key, create a new node, update left and right respectively, and then return the new node.
8+
9+
Now we can create our binaryTree factory function. Since trees must have a root, our function receives a rootKey as an argument. We can then create the root node using our node factory function. We pass this to the object we return from the function.
10+
11+
Binary trees have three specific types of traversals: in order, pre order and post order. Let's create a traversals enum. Each item in our enum will be named after the type of traversal it is. The value of each will be a function that we can use when traversing our tree.
12+
13+
In order traversal always visits the left branch, then our current node, follwed by the right branch. Since these traversal functions are called recursively, we need to guard against a node that is null. So if the node is not null, we call our traversal down the left branch, then visit our current node, then traverse down the right branch.
14+
15+
Pre order traversal is very similar, but we change the order so that we visit our current node first, then followed by the left branch, then the right branch.
16+
17+
Post order traversal visits the left and right branches before visiting the current node.
18+
19+
Now, let's add a print method to our tree that accepts an orderType as an argument. The default will be inorder, as that's the most common traversal.
20+
21+
We'll keep our print function simple. As we visit each node, we'll add the key to the result and return a string. Using our enum, we can trigger the correct method call to start our traversal.
22+
23+
Let's create a simple binary tree, I have one to copy paste in here, We can call print on it several times and compare the results.

0 commit comments

Comments
 (0)