Trees

The category contains posts related to the Trees data structure. This category is a child category in the Data Structures category,

Build Balanced Binary Search Tree from Array

Introduction This post describes the algorithm to build binary search tree from array of sorted elements. However, this technique can be used to build balanced binary search tree from array. A balanced binary tree is a binary tree which has minimum height. To know more about various aspects of a binary search tree, please visit my previous posts. What does balanced means? In this post as we are trying to build a BST, we will assume the input to be a sorted array. In case, we are just building a binary tree, we can take any array as an input. Let me explain the process with the help of a ...
Read More

Splitting and Merging B Tree Nodes

This post is to be read in conjunction with another post Introduction to B Trees. Here we learn that in certain operations the B Tree properties might get disturbed and it will need a fix. Splitting and Merging B Tree Nodes are the only operations which can re-establish the properties of the B Tree. How does a B Tree get unbalanced? The tree is not just to read and search data. There are operations which update the tree either by deleting a key, inserting a new key or just updating the values stored against the key. The Insert and Delete operations tend to modify the structure of the tree...
Read More

Introduction to B Trees

This post is just an introduction to B Trees and here we will discuss one of the complicated data structures, B Trees. I would advice the reader to get familiar with the Tree data structures, and Balanced Trees. For e.g. : Binary Search Trees, AVL Trees, Red Black Trees and 2-3-4 Trees. If you are not familiar with any/few of the above mentioned data structures, then please do not worry, I will try to explain everything here from scratch and hopefully you might not need any external support. But in case you are trying to learn the above topics, please look into the Additional Read section a...
Read More

Balanced Search Trees – AVL Tree

Introduction When we talk about balanced search trees, we specifically are talking about two things. A search trees. A height balanced tree. Here in this post we will consider a Binary Search Tree and will try to equip with features to self balance itself upon modifications. Before starting up with the logic and code part we need to understand the motive behind this kind of data structure and more precisely we need to understand the need of such a complex scenario. Apart from that we also need to understand what it means to be termed as balanced. What does balancing means? We sa...
Read More

Shortest path in Binary Search Tree

Introduction Another interview question for the most interesting data structure called BST (Binary Search Tree). To know more about BSTs check my previous post. Problem Statement : Given a Binary Search Tree, keyed on positive integers. The task is to find the Shortest path in Binary Search Tree which adds up to the number K. If no such path exists, return a message accordingly. Understanding the problem A node in a binary search tree always has a higher key at its right child compared to its left child. There are many path running from the roots to the leaves. When we add up the key val...
Read More

Inorder Predecessor of node in Binary Tree Using Parent Pointer

Introduction This is the seventh article in the Tree Traversals - Online Classes. This article presents ways to find out inorder predecessor of node in binary tree. As we have already read about the Inorder Successor it wont take much effort to understand the predecessor. Definition - Inorder Predecessor of node in Binary Tree If a node K is called the inorder predecessor of node N then N must be visited immediately after K in a inorder traversal of the tree. Below is a more formal definition of Inorder Predecessor: If the node has a left sub-tree, then inorder predecessor is the ...
Read More

Inorder Successor of node in Binary Tree Using Parent Pointer

Introduction This is the sixth article in the Tree Traversals - Online Classes. This article presents ways to find out inorder successor of node in binary tree. Before we jump into the code it would be good to correctly define the meaning of the problem. Definition - Inorder Successor of node in Binary Tree If a node K is called the inorder successor of node N then K must be visited immediately after N in a inorder traversal of the tree. Below is a more formal definition of Inorder Successor: If the node has a right sub-tree, then inorder successor is the left most node of the rig...
Read More