Binary Trees

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

Binary Trees

© Oxford University Press 2014. All rights reserved.


Trees
• A tree is recursively defined as a set of one or more nodes where
one node is designated as the root of the tree and all the
remaining nodes can be partitioned into non-empty sets each of
which is a sub-tree of the root.
• Types of Trees
General Trees
Forests
Binary Trees
Expression Trees
Tournament Trees

© Oxford University Press 2014. All rights reserved.


General Trees
• General trees are data structures that store elements
hierarchically.
• The top node of a tree is the root node and each node, except the
root, has a parent.
• A node in a general tree (except the leaf nodes) may have zero or
more sub-trees.
• General trees which have 3 sub-trees per node are called ternary
trees.
• However, the number of sub-trees for any node may be variable.
For example, a node can have 1 sub-tree, whereas some other
node can have 3 sub-trees.
© Oxford University Press 2014. All rights reserved.
Forests
• A forest is a disjoint union of trees. A set of disjoint trees (or
forest) is obtained by deleting the root and the edges
connecting the root node to nodes at level 1.
• Every node of a tree is the root of some sub-tree. Therefore, all
the sub-trees immediately below a node form a forest.
• A forest can also be defined as an ordered set of zero or more
general trees.
• While a general tree must have a root, a forest on the other
hand may be empty because by definition it is a set, and sets
can be empty.
• We can convert a forest into a tree by adding a single node as
the root node of the tree.
© Oxford University Press 2014. All rights reserved.
Binary Trees
• A binary tree is a data structure which is defined as a collection of
elements called nodes.
• In a binary tree, the topmost element is called the root node, and
each node has 0, 1, or at the most 2 children.
• Every node contains a data element, a "left" pointer which points
to the left child, and a "right" pointer which points to the right
child.
• The root element is pointed by a "root" pointer.
• If root = NULL, then it means the tree is empty.
1 ROOT NODE

T1 T2
2 3

R – Root node (node 1)


4
5 6 7 T1- left sub-tree (nodes 2, 4, 5, 8, 9)
T2- right sub-tree (nodes 3, 6, 7, 10, 11, 12)
8 9 1 1 1
0 1 2

© Oxford University Press 2014. All rights reserved.


Formal Definition of a Binary Tree
• A binary tree is a finite set of elements that is
either empty or is partitioned into 3 subsets
– First subset contains a single element called the
root of the tree
– The other two subsets are themselves binary trees
called as left and right subtrees of the original
tree
– A left or right subtree can be empty

© Oxford University Press 2014. All rights reserved.


Binary Trees - Key Terms
• Parent: If N is any node in T that has left successor S1 and right successor
S2, then N is called the parent of S1 and S2. Correspondingly, S1 and S2 are
called the left child and the right child of N. Every node other than the root
node has a parent.
• Sibling: S1 and S2 are said to be siblings. In other words, all nodes that are
at the same level and share the same parent are called siblings (brothers).
• Level number: Every node in the binary tree is assigned a level number.
• The root node is defined to be at level 0.
• Every node is at one level higher than its parents.
• Leaf node: A node that has no children.
• Degree: Degree of a node is equal to the number of children that a node
has.

© Oxford University Press 2014. All rights reserved.


Binary Trees - Key Terms
• In-degree of a node is the number of edges arriving at
that node.
• Out-degree of a node is the number of edges leaving
that node.
• Edge: It is the line connecting a node N to any of its
successors
• Path: A sequence of consecutive edges is called a path.

© Oxford University Press 2014. All rights reserved.


Depth and height of a binary tree
• Depth:
• The depth of a node N is given as the length of the path from
the root to the node N. The depth of the root node is zero.
• Depth of a binary tree is the maximum level of any leaf in
the tree.
• Height:
• Height of a binary tree is the maximum level of any leaf in
the tree. (Same as depth of a binary tree)
• Height of a tree with only root node is 0
• Height of a NULL tree is -1..

© Oxford University Press 2014. All rights reserved.


Binary Trees - Key Terms
• Similar binary trees: Given two binary trees T and T’ are said to be similar if
both these trees have the same structure.
TREE T
TREE T”
A F

B C G H

D I

E J

• Copies of binary trees: Two binary trees T and T’ are said to be copies if they
have similar structure and same content at the corresponding nodes.
TREE T
TREE T”
A A

B C B C

E
D E D

© Oxford University Press 2014. All rights reserved.


Strictly Binary Tree
• A binary tree in which every non-leaf node has non-empty left
and right subtrees is called as strictly binary tree.
• A strictly binary tree with n leaves always contains 2n -1
nodes

© Oxford University Press 2014. All rights reserved.


Complete or Full or Perfect Binary Tree
• A complete binary tree of height h is a strictly binary
tree all of whose leaves are at level h.

© Oxford University Press 2014. All rights reserved.


Almost Complete Binary Tree
• An almost complete binary tree is a binary tree in
which every level, except possibly the last, is
completely filled, and all nodes are as far left as
possible.
• In some books and literature this has been called as
complete binary tree

© Oxford University Press 2014. All rights reserved.


No of Nodes
• Complete binary tree
– No of Nodes n = 2h+1 – 1
– No of non-leaf nodes = 2h – 1

© Oxford University Press 2014. All rights reserved.


Extended Binary Trees
• Extended binary tree is a type of binary tree in which all the null sub tree of the
original tree are replaced with special nodes called external nodes whereas other
nodes are called internal nodes
• In the figure internal nodes are represented using a circle and external nodes are
represented using squares.
• To convert a binary tree into an extended tree, every empty sub-tree is replaced
by a new node. The original nodes in the tree are the internal nodes and the new
nodes added are called the external nodes.

Extended binary tree

Binary tree

© Oxford University Press 2014. All rights reserved.


Expression Trees
• Binary trees are widely used to store algebraic expressions. For
example, consider the algebraic expression Exp given as:
Exp = (a – b ) + ( c * d)
• This expression can be represented using a binary tree as shown
in figure
+

- *

a b c d

© Oxford University Press 2014. All rights reserved.


Example - Construct expression tree for the
expression –[a+(b-c)]*[(d-e)/(f+g-h)]

© Oxford University Press 2014. All rights reserved.


Representing Binary Trees
• Linked representation
• Array representation
• Implicit array representation (or sequential
representation)

© Oxford University Press 2014. All rights reserved.


Linked Representation of Binary Trees
• In computer’s memory, a binary tree can be maintained either using a
linked representation or using sequential representation.
• In linked representation of binary tree, every node will have three parts:
the data element, a pointer to the left node and a pointer to the right
node. So in C, the binary tree is built with a node type given as below.
struct node
{ 1

struct node* left; 2 3

int data; 4 5 6 7

struct node* right;


X 8 X X 9 X X 10 X X 11 X X 12 X
};

© Oxford University Press 2014. All rights reserved.


Array Representation of Binary Trees

© Oxford University Press 2013. All rights reserved.


Sequential Representation of Binary Trees
• Sequential representation of trees is done using a single or one
dimensional array. Though, it is the simplest technique for memory
representation, it is very inefficient as it requires a lot of memory
space.
• A sequential binary tree follows the rules given below:
• One dimensional array called TREE is used.
• The root of the tree will be stored in the first location. That is,
TREE[1] will store the data of the root element.
• The children of a node K will be stored in location (2*K) and (2*K+1).
• The maximum size of the array TREE is given as (2h-1), where h is the
height of the tree.
• An empty tree or sub-tree is specified using NULL. If TREE[1] = NULL,
then the tree is empty.

© Oxford University Press 2014. All rights reserved.


Sequential Representation of Binary Trees
20

15 35

39
12
17 21

36 45
18
16

© Oxford University Press 2014. All rights reserved.


Traversing a Binary Tree
• Traversing a binary tree is the process of visiting each node in the tree
exactly once in a systematic way.

• There are three different algorithms for tree traversals, which differ in
the order in which the nodes are visited.

• These algorithms are:


✔ Pre-order algorithm
✔ In-order algorithm

✔ Post-order algorithm

© Oxford University Press 2014. All rights reserved.


Pre-order Algorithm
• To traverse a non-empty binary tree in preorder, the following
operations are performed recursively at each node.
• The algorithm starts with the root node of the tree and continues
by:
✔ Visiting the root node
✔ Traversing the left subtree A

✔ Traversing the right subtree B C

D E

A, B, D, C, E, F, G, H and I F

H I

© Oxford University Press 2014. All rights reserved.


In-order Algorithm
• To traverse a non-empty binary tree in in-order, the following operations are
performed recursively at each node.
• The algorithm starts with the root node of the tree and continues by,
✔ Traversing the left subtree
✔ Visiting the root node
✔ Traversing the right subtree A

B C

D E

B, D, A, E, H, G, I, F and C
G

H I

© Oxford University Press 2014. All rights reserved.


Post-order Algorithm
• To traverse a non-empty binary tree in post-order, the following
operations are performed recursively at each node.
• The algorithm starts with the root node of the tree and continues
by,
✔ Traversing the left subtree
✔ Traversing the right subtree A

✔ Visiting the root node B C

D E

D, B, H, I, G, F, E, C and A G

H I

© Oxford University Press 2014. All rights reserved.


Tree Traversals
A What is a the result of a
post order traversal of
the tree to the left?
A. F C G A K H L D J
C D B. F G C K L H J D A
C. A C F G D H K L J
D. A C D F G H J K L
E. L K J H G F D C A
F G H J

K L

© Oxford University Press 2014. All rights reserved.


Example
The inorder and preorder traversals of a binary
tree are as follows:
Inorder – EACKFHDBG
Preorder – FAEKCDHGB
Construct the Binary tree

© Oxford University Press 2014. All rights reserved.


Inorder – EACKFHDBG
Preorder – FAEKCDHGB

© Oxford University Press 2013. All rights


reserved.
Recursive preorder traversal
void preorder( NODEPTR tree)
{if( tree != NULL)
{
printf(“%d”, tree->data);
preorder(tree->left);
preorder(tree->right);
}
}
© Oxford University Press 2014. All rights reserved.
Recursive inorder traversal
void inorder( NODEPTR tree)
{if( tree != NULL)
{
inorder(tree->left);
printf(“%d”, tree->data);
inorder(tree->right);
}
}
© Oxford University Press 2014. All rights reserved.
Recursive postorder traversal
void postorder( NODEPTR tree)
{if( tree != NULL)
{
postorder(tree->left);
postorder(tree->right);
printf(“%d”, tree->data);
}
}
© Oxford University Press 2014. All rights reserved.
Non-recursive inorder traversal
void inorder( NODEPTR tree)
{ NODEPTR p;
STACK s;
p=tree;
do{
while (p!=NULL)
{
push(s, p);
p=p->left;
}
If(!empty(s))
{
p=pop(s);
printf(“%d”, p->data);
}
} while(!empty(s) || p!= NULL)
}

© Oxford University Press 2014. All rights reserved.


Applications of Trees
• Trees are used to store simple as well as complex data. Here simple
means an int value, char value and complex data (structure).
• Trees are often used for implementing other types of data structures like
hash tables, sets, and maps.
• A self-balancing tree, Red-black tree is used in kernel scheduling to
preempt massively multi-processor computer operating system use.
• Another variation of tree, B-trees are used to store tree structures on
disc. They are used to index a large number of records.
• B-trees are also used for secondary indexes in databases, where the index
facilitates a select operation to answer some range criteria.
• Trees are used for compiler construction.
• Trees are also used in database design.
• Trees are used in file system directories.
• Trees are also widely used for information storage and retrieval in symbol
tables.

© Oxford University Press 2014. All rights reserved.

You might also like