Introduction To Tree DS
Introduction To Tree DS
What is a tree?
A tree is a kind of data structure that is used to represent the data in hierarchical
form. It can be defined as a collection of objects or entities called as nodes that are
linked together to simulate a hierarchy. Tree is a non-linear data structure as the
data in a tree is not stored linearly or sequentially.
1. Binary Tree
A binary Tree is defined as a Tree data structure with at most 2 children. Since
each element in a binary tree can have only 2 children, we typically name them
the left and right child.
Example:
Consider the tree below. Since each node of this tree has only 2 children, it can
be said that this tree is a Binary Tree
Types of Binary Tree:
Binary Tree consists of following types based on the number of children:
a. Full Binary Tree
b. Almost complete Binary Tree
c. Complete Binary Tree
d. Binary Search Tree
2. Ternary Tree
A Ternary Tree is a tree data structure in which each node has at most three child
nodes, usually distinguished as “left”, “mid” and “right”.
Example:
Consider the tree below. Since each node of this tree has only 3 children, it can
be said that this tree is a Ternary Tree
Ternary Tree
2. AVL Tree
AVL tree is a self-balancing Binary Search Tree (BST) where the difference
between heights of left and right subtrees for any node cannot be more than one.
AVL Tree
3. Red-Black Tree
A red-black tree is a kind of self-balancing binary search tree where each node
has an extra bit, and that bit is often interpreted as the color (red or black). These
colors are used to ensure that the tree remains balanced during insertions and
deletions.
Although the balance of the tree is not perfect, it is good enough to reduce the
searching time and maintain it around O(log n) time, where n is the total number
of elements in the tree.
Rules That Every Red-Black Tree Follows:
1. Every node has a color either red or black.
2. The root of the tree is always black.
3. There are no two adjacent red nodes (A red node cannot have a red parent
or red child).
4. Every path from a node (including root) to any of its descendants’ NULL
nodes has the same number of black nodes.
5. All leaf (NULL) nodes are black nodes.
Red-Black Tree
4. B-Tree
B-Tree is a self-balancing search tree. In most of the other self-balancing search
trees (like AVL and Red-Black Trees), it is assumed that everything is in the main
memory.
Properties of B-Tree:
All leaves are at the same level.
B-Tree is defined by the term minimum degree ‘t‘. The value of ‘t‘ depends
upon disk block size.
Every node except the root must contain at least t-1 keys. The root may
contain a minimum of 1 key.
All nodes (including root) may contain at most (2*t – 1) keys.
The number of children of a node is equal to the number of keys in it plus 1.
All keys of a node are sorted in increasing order. The child between two
keys k1 and k2 contains all keys in the range from k1 and k2.
B-Tree grows and shrinks from the root which is unlike Binary Search Tree.
Binary Search Trees grow downward and also shrink from downward.
Like other balanced Binary Search Trees, the time complexity to search, insert
and delete is O(log n).
Insertion of a Node in B-Tree happens only at Leaf Node.