Getting Started With Trees 🌲
Tags: Data Structures, Programming, Computer Science, Trees, Technology

🌳 TYPES OF TREES 🌴

1. BINARY TREE —

Binary Tree Representation— only two children allowed, left and right.

A binary tree is a tree in which each node consists of at most two children. They can further be classified into :

Full Binary Tree — A binary tree where every node has exactly 2 children (except the leaf nodes, ofcourse !) .

A binary tree where every node has exactly 2 children (except the leaf nodes, ofcourse !) . Complete Binary Tree — A binary tree which is completely filled with a possible exception made only at the bottom-most level.

A binary tree which is completely filled with a possible exception made only at the bottom-most level. Perfect Binary Tree — A binary tree where each interior node has exactly two child nodes and each leaf node is at the same level.

The following illustration will make the difference clear.

Binary Trees are the most important and commonly used trees. We have just looked at the terminology of Trees and the types of binary trees. But once we have constructed our tree, we do it with the intent to reuse the data stored in the Tree later on for some purpose. Let’s see how we can access the the data stored in a binary Tree.

Binary Tree Traversals

Pre-Order Traversal : In this type of traversal, we visit the root node first, then the left sub-tree and lastly the right sub-tree. This happens recursively for all nodes in the Tree.

preorder(node)

if (node == null)

return

visit(node) // visit the root node at first

preorder(node.left) // then traverse left subtree first

preorder(node.right) // lastly traverse the right subtree

Here’s an visualization to make it clear on how the traversal functions —

Preorder traversal by Issac

In-Order Traversal : Here, we traverse the left sub-tree, then visit the root node and lastly move on to the right sub-tree.

inorder(node)

if (node == null)

return

inorder(node.left) // traverse left subtree first

visit(node) // visit the root node

inorder(node.right) // traverse the right subtree

Here’s the visualization —

Inorder traversal by Issac

Post-Order Traversal : Here, we start from the left sub-tree and move on traversing the right sub-tree and at last visit the root node.

postorder(node)

if (node == null)

return

postorder(node.left) // first traverse left subtree first

postorder(node.right) // then traverse the right subtree

visit(node) // lastly visit the root node at first

Visualization for post-order traversal —

Postorder traversal by Issac

There are other traversal techniques as well like the level-order traversal ( also known as BFS ) in which we visit each node according to their level that they appear in the Tree. There’s also an iterative version of these traversal which is slightly more difficult to implement. The recursion makes everything easier in Trees. Trees are themselves also known as recursive data structure since they involve sub-tree which recursively make up the tree as a whole

2. BINARY SEARCH TREE —

A Binary Search Tree (BST) is binary tree in which —

All nodes in the left sub-tree of root node are less than or equal to the root node

of root node are to the root node All nodes in the right sub-tree of root node are greater than the root node.

Sometimes, the right sub-tree might hold the value which is equal to the root node but that’s implementation dependent and both can be considered correct.

A BST is used mainly for the purpose of optimizing the search of a node in the Tree. The time complexity of search in BST is O(logN) as compared to O(N) in a normal Tree, where N is the number of nodes.

We can easily eliminate one sub-tree of BST while searching for a value by just comparing the given value with root node value. If the value to be searched is greater than root value, we go to right sub-tree, else we go into the left sub-tree. For this, we must also maintain the BST property while insertion as well as deletion from the Tree.

Insertion Visualization —

BST insertion, source — codesdope

Search Visualization —

BST searching, source — penjee

Lets look at other types of trees in short…

3. AVL TREES —

AVL Trees are just Binary Search Trees which are well balanced. A simple BST can get unbalanced and in the worst case, we may have a tree which has all its nodes on only the left or right side. To ensure that this case does not occur, we use AVL Trees which use different rules to keep a BST balanced.

4. RED-BLACK TREE —

Red-Black Tree are another type of self-balancing tree. It is given the name Red-Black because each node is either painted Red or Black according to properties of Red-Black Tree. Whenever new nodes are added, the nodes are rotated and painted again to maintain the property.

5. TRIE —

Trie is an N-ary search Tree which is based on prefix of a string. They are used to store associative data structure and for efficient retrieval of data and hence the name Trie. They are also called digital tree or a prefix tree.

The most common ones are Binary Tree and Binary Search Tree and constitute the majority of use cases. There are other types of Trees as well like Splay Trees, Segment Trees, Suffix Trees, B-Tree, B+ Tree etc. which are advanced data-structures used for various applications.