# Tree in Data Structure

### Tree

A tree is a non-linear data structure by which hierarchical data is displayed. As we know that there are many trees in the forest, similarly the data structure also contains many types of trees, such as - binary tree, expression tree, tournament tree, binary search tree, threaded tree, AVL tree, and B-tree.

### Basic terminology of the tree

**Root node:**The topmost node in the tree is known as the**root node**. The whole tree is referenced through the root node.**Edge:**Edge is linked between the two nodes.**Leaf node:**A leaf node is a node that has no child node.**Parent node:**A parent-node can have multiple child nodes.**Child node:**A child has only one parent node.**Path:**The path is a set of successive edges from the initial node to the terminal node.

### Uses of Tree

- The tree is a non-linear data structure, so it is used to store the information in a non-linear form.
- If we arrange the data in the form of a tree, we can search the data quickly.

### Types of Tree

### 1. Binary Tree

In the data structure, a binary tree is such type of tree in which each node has only a maximum of two children. Those are called the **left child** and **right child**.

### 2. Binary Search Tree

A binary search tree is a type of tree in which every node is organized in the sorted order. It is also called an **ordered binary tree**.

### Properties of BST

- The left sub-tree value is less than the root node.
- Similarly, the right sub-tree value is higher than the root node.
- This rule is reapplied to all left and right sub-trees of the root.

### 3. Empty Tree

When there is no any single node in the tree is called an **empty tree**.

### 4. B-Tree

B-tree stands for the balanced tree. A B-tree is an M-Way (multi-way) tree that is specially designed for use in the disk. An M-way tree can have many children. A node in an M-way tree can have many keys.

If the number of keys in a node of the M-way tree is N, the number of children in that node will be k + 1.

The B-tree is always perfectly balanced. That is, every leaf node of the B-tree has the same depth. The height-balanced and weight-balanced tree is not sufficient to store very large data, so the B-tree is used to eliminate this drawback. B-tree is mostly used in file systems and DBMS. B-tree is a method to locate and place files in a database.

**Searching algorithm of B-tree**

BtreeSearch(x, k)i = 1 while i ? n[x] and k ? keyi[x] // n[x] means number of keys in x node do i = i + 1 if i n[x] and k = keyi[x] then return (x, i) if leaf [x] then return NIL else return BtreeSearch(ci[x], k)

### 5. AVL Tree

AVL tree is a self-balancing binary search tree. It is also called a **height-balanced tree**. It was invented by **GM Adelson - Velsky, **and **EM Landis** in **1962**. This tree is named AVL in honor of its inventors.

It can be defined as a height-balanced binary search tree. In this tree, every node or vertex is connected with a balance factor. This balancing factor is calculated by this formula.

Balance factor (BF) = height of left (sub-tree) - height of right (sub-tree) |

If the tree is balanced, its balance factor of every node will be in between in -1 to 1. If the tree is unbalanced, that tree can be balanced with the help of rotation.

If it has N nodes, its height will be log2(N + 1).