Trees

Trees are a big part of programming.

A tree starts out with one root node.

_config.yml

Then, it has some children nodes otherwise known as parent/internal nodes, and the tree ends with a leaf, which is a node with no children.

_config.yml

There are different types of trees. One of them is the binary tree, where each node has 0-2 child nodes; up to 2 nodes. In this case, 0 means null.

_config.yml

Now here comes the binary search tree. the binary search tree is where each node on the left of the parent is less in value than the parent, and each node on the right of the parent is greater in value than the parent. This way of programming is very very fast; it can reduce up to half of the possibilities of the nodes.

_config.yml

In a binary search tree, there is something called insert. To insert, you basically do the same things as trying to find a value in the tree, however, you only insert when the value of the certain space the value belongs to is null.

_config.yml

Now, there is both a good and bad part of everything. There are 2 ‘types’ of binary trees, balanced and unbalanced. An unbalanced tree has nodes that go starting from 1, 2, 3, 4, 5 … so on, inserting to the right. This only creates a normal array of numbers. However, on the other hand, a balanced tree has numbers that are literally ‘balanced’, starting from 4, then 2, 5 … and so on, making it much faster.

_config.yml

In a binary search tree, there is something called traversing. There are 3 types of traversing. The first way is Inorder, the second is Preorder, and the last on is Postorder.

Preorder means that you look at the root, then the left, then the right.

_config.yml

Inorder means that you look at the left node, the root node, then the right node.

_config.yml

And finally, the Postorder means that you start from the left node, to the right node, to the root.

_config.yml

Typically, for a balanced binary search tree, you would want to use the inorder traversing, mainly because it keeps the tree balanced.

Written on April 2, 2019