Binary Tree

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

BINARY TREE

Tree represents the nodes connected by edges. We will discuss


binary tree or binary search tree specifically.
Binary Tree is a special data structure used for data storage
purposes. A binary tree has a special condition that each node
can have a maximum of two children. A binary tree has the
benefits of both an ordered array and a linked list as search is as
quick as in a sorted array and insertion or deletion operation are
as fast as in linked list.

Important Terms

Following are the important terms with respect to tree.


 Path − Path refers to the sequence of nodes along the edges
of a tree.
 Root − The node at the top of the tree is called root. There is
only one root per tree and one path from the root node to any
node.
 Parent − Any node except the root node has one edge
upward to a node called parent.
 Child − The node below a given node connected by its edge
downward is called its child node.
 Leaf − The node which does not have any child node is
called the leaf node.
 Subtree − Subtree represents the descendants of a node.
 Visiting − Visiting refers to checking the value of a node
when control is on the node.
 Traversing − Traversing means passing through nodes in a
specific order.
 Levels − Level of a node represents the generation of a
node. If the root node is at level 0, then its next child node is
at level 1, its grandchild is at level 2, and so on.

Binary Tree Representations

A binary tree data structure is represented using two methods.


Those methods are as follows...

1. Array Representation
2. Linked List Representation

Consider the following binary tree...


1. Array Representation of Binary Tree

In array representation of a binary tree, we use one-dimensional


array (1-D Array) to represent a binary tree.
Consider the above example of a binary tree and it is represented
as follows...

A B C D E F H I J - - - K - -

2. Linked List Representation of Binary Tree


We use a double linked list to represent a binary tree. In a double
linked list, every node consists of three fields. First field for storing
left child address, second for storing actual data and third for
storing right child address.
In this linked list representation, a node has the following
structure...

The above example of the binary tree represented using Linked


list representation is shown as follows...
Types of Binary Trees (Based on Structure)

 Rooted binary tree: It has a root node and every node has
atmost two children.

 Full binary tree: It is a tree in which every node in the tree has
either 0 or 2 children.
o The number of nodes, n, in a full binary tree is atleast n =
2h – 1, and atmost n = 2h+1 – 1, where h is the height of the
tree.

 Perfect binary tree: It is a binary tree in which all interior nodes


have two children and all leaves have the same depth or same
level.

o A perfect binary tree with l leaves has n = 2l-1 nodes.


 Complete binary tree: It is a binary tree in which every level,
except possibly the last, is completely filled, and all nodes are as
far left as possible.

o The number of internal nodes in a complete binary tree of n


nodes is floor(n/2).

 Balanced binary tree: A binary tree is height balanced if it


satisfies the following constraints:

1. The left and right subtrees' heights differ by at most one,


AND

2. The left subtree is balanced, AND

3. The right subtree is balanced

An empty tree is height balanced.


 Degenarate tree: It is a tree is where each parent node has only
one child node. It behaves like a linked list.

You might also like