0% found this document useful (0 votes)
10 views

Binary Tree Copy. 1

Uploaded by

forg28826
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Binary Tree Copy. 1

Uploaded by

forg28826
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

lOMoARcPSD|21950806

Wondershare
PDFelement
1.a TREES

Representation as a Degree –Two Tree

To obtain degree-two tree representation of a tree, rotate the right- sibling pointers in the left child-
right sibling tree clockwise by 45 degrees. In a degree-two representation, the two children of anode
are referred as left and right children.

Binary Trees

Introduction

In a normal tree, every node can have any number of children. Binary tree is a special type of tree
data structure in which every node can have a maximum of 2 children. One is known as left child
and the other is known as right child.

A tree in which every node can have a maximum of two children is called as Binary Tree.

In a binary tree, every node can have either 0 children or 1 child or 2 children but not more than 2
children. Example

There are different types of binary trees and they are...

Downloaded by Sreya S ([email protected])


lOMoARcPSD|21950806

Wondershare
PDFelement
1.b TREES

1. Strictly Binary Tree

In a binary tree, every node can have a maximum of two children. But in strictly binary tree, every
node should have exactly two children or none. That means every internal node must have exactly
two children. A strictly Binary Tree can be defined as follows...

A binary tree in which every node has either two or zero number of children is called Strictly Binary
Tree. Strictly binary tree is also called as Full Binary Tree or Proper Binary Tree or 2-Tree

2. Complete Binary Tree

In a binary tree, every node can have a maximum of two children. But in strictly binary tree, every
node should have exactly two children or none and in complete binary tree all the nodes must have
exactly two children and at every level of complete binary tree there must be 2 level number of
nodes. For example at level 2 there must be 2^2 = 4 nodes and at level 3 there must be 2^3 = 8
nodes.

A binary tree in which every internal node has exactly two children and all leaf nodes are at same
level is called Complete Binary Tree.

Complete binary tree is also called as Perfect Binary Tree

3. Extended Binary Tree

A binary tree can be converted into Full Binary tree by adding dummy nodes to existing nodes
wherever required.

The full binary tree obtained by adding dummy nodes to a binary tree is called as Extended Binary
Tree.

Abstract Data Type

Downloaded by Sreya S ([email protected])


lOMoARcPSD|21950806

Wondershare
PDFelement
1.c TREES

Definition: A binary tree is a finite set of nodes that is either empty or consists of a root and two
disjoint binary trees called left subtree and right subtree.

ADT contains specification for the binary tree ADT.

Structure Binary_Tree(abbreviated BinTree) is

objects: a finite set of nodes either empty or consisting of a root node, left Binary_Tree, and right
Binary_Tree.

Functions:

for all bt, bt1, bt2  BinTree, item  element

Bintree Create()::= creates an empty binary tree

Boolean IsEmpty(bt)::= if (bt==empty binary tree) return TRUE else return FALSE

BinTree MakeBT(bt1, item, bt2)::= return a binary tree whose left subtree is bt1, whose right
subtree is bt2, and whose root node contains the data item

Bintree Lchild(bt)::= if (IsEmpty(bt)) return error else return the left subtree of bt

element Data(bt)::= if (IsEmpty(bt)) return error else return the data in the root node of bt

Bintree Rchild(bt)::= if (IsEmpty(bt)) return error else return the right subtree of bt

Samples of Trees
Complete Binary Tree

A A 1 A

B B 2 B C

C Skewed Binary Tree


3 D E F G
D

4 H I
E 5

CHAPTER 5 10

Differences between A Tree and A Binary Tree

• The subtrees of a binary tree are ordered; those of a tree are not ordered.

Downloaded by Sreya S ([email protected])


lOMoARcPSD|21950806

Wondershare
PDFelement
1.d TREES

Above two trees are different when viewed as binary trees. But same when viewed as trees.

Properties of Binary Trees

1.Maximum Number of Nodes in BT

 The maximum number of nodes on level i of a binary tree is 2i-1, i>=1.


 The maximum number of nodes in a binary tree of depth k is 2k-1, k>=1.

Proof By Induction:

Induction Base: The root is the only node on level i=1.Hence ,the maximum number of nodes on
level i=1 is 2i-1=20=1.

Induction Hypothesis: Let I be an arbitrary positive integer greater than 1.Assume that maximum
number of nodes on level i-1 is 2i-2.

Induction Step: The maximum number of nodes on level i-1 is 2i-2 by the induction hypothesis. Since
each node in a binary tree has a maximum degree of 2,the maximum number of nodes on level i is
two times the maximum number of nodes on level i-1,or 2i-1.
k

The maximum number of nodes in a binary tree of depth k is 2


i 1
i 1
 2k  1

2.Relation between number of leaf nodes and degree-2 nodes: For any nonempty binary tree, T, if
n0 is the number of leaf nodes and n2 the number of nodes of degree 2, then n0=n2+1.

PROOF: Let n and B denote the total number of nodes and branches in T. Let n0, n1, n2
represent the nodes with zero children, single child, and two children respectively.

B+1=n  B=n1+2n2 ==> n1+2n2+1= n,

n1+2n2+1= n0+n1+n2 ==> n0=n2+1

3. A full binary tree of depth k is a binary tree of depth k having 2 -1 nodes, k>=0.

A binary tree with n nodes and depth k is complete iff its nodes correspond to the nodes numbered
from 1 to n in the full binary tree of depth k.

Binary Tree Representation

A binary tree data structure is represented using two methods. Those methods are 1)Array
Representation 2)Linked List Representation

10

Downloaded by Sreya S ([email protected])


lOMoARcPSD|21950806

Wondershare
PDFelement
1.e TREES

1)Array Representation: In array representation of binary tree, we use a one dimensional array (1-D
Array) to represent a binary tree. To represent a binary tree of depth 'n' using array representation,
we need one dimensional array with a maximum size of

A complete binary tree with n nodes (depth = log n + 1) is represented sequentially, then for
any node with index i, 1<=i<=n, we have: a) parent(i) is at i/2 if i!=1. If i=1, i is at the root and
has no parent. b)left_child(i) ia at 2i if 2i<=n. If 2i>n, then i has no left child. c) right_child(i) is at
2i+1 if 2i +1 <=n. If 2i +1 >n, then i has no right child.
[1] A
Disadvantages:(1) waste of space
(2) insertion/deletion problem [2] B
[3] C
[4] D
A [1] A [5] E
[2] B [6] F
[3] -- [7]
B G
[4] C [8]
A H
[5] -- [9]
C I
[6] --
[7] -- B C
D [8] D
[9] --
. . D E F G
E
[16] E

H
CHAPTER 5
I 13

2. Linked Representation

We use linked list to represent a binary tree. In a 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...

typedef struct node *tree_pointer;

typedef struct node {

int data;

tree_pointer left_child, right_child;};

11

Downloaded by Sreya S ([email protected])


lOMoARcPSD|21950806

Wondershare
PDFelement
1.f TREES

Binary Tree Traversals

When we wanted to display a binary tree, we need to follow some order in which all the nodes of
that binary tree must be displayed. In any binary tree displaying order of nodes depends on the
traversal method. Displaying (or) visiting order of nodes in a binary tree is called as Binary Tree
Traversal.

There are three types of binary tree traversals.

1)In - Order Traversal 2)Pre - Order Traversal 3)Post - Order Traversal

Binary Tree Traversals


• 1. In - Order Traversal ( leftChild - root - rightChild )
• I - D - J - B - F - A - G - K - C – H
• 2. Pre - Order Traversal ( root - leftChild - rightChild )
• A - B - D - I - J - F - C - G - K – H
• 3. Post - Order Traversal ( leftChild - rightChild - root )
• I - J - D - F - B - K - G - H - C – A

CHAPTER 5 14

1. In - Order Traversal ( leftChild - root - rightChild )

In In-Order traversal, the root node is visited between left child and right child. In this traversal, the
left child node is visited first, then the root node is visited and later we go for visiting right child
node. This in-order traversal is applicable for every root node of all subtrees in the tree. This is
performed recursively for all nodes in the tree.

In the above example of binary tree, first we try to visit left child of root node 'A', but A's left child is
a root node for left subtree. so we try to visit its (B's) left child 'D' and again D is a root for subtree
with nodes D, I and J. So we try to visit its left child 'I' and it is the left most child. So first we

12

Downloaded by Sreya S ([email protected])


lOMoARcPSD|21950806

Wondershare
PDFelement
1.g TREES

visit 'I'then go for its root node 'D' and later we visit D's right child 'J'. With this we have completed
the left part of node B. Then visit 'B' and next B's right child 'F' is visited. With this we have
completed left part of node A. Then visit root node 'A'. With this we have completed left and root
parts of node A. Then we go for right part of the node A. In right of A again there is a subtree with
root C. So go for left child of C and again it is a subtree with root G. But G does not have left part so
we visit 'G' and then visit G's right child K. With this we have completed the left part of node C.
Then visit root node'C' and next visit C's right child 'H' which is the right most child in the tree so we
stop the process.

That means here we have visited in the order of I - D - J - B - F - A - G - K - C - H using In-Order


Traversal.

In-Order Traversal for above example of binary tree is

I-D-J-B-F-A-G-K-C–H

Algorithm

Until all nodes are traversed −

Step 1 − Recursively traverse left subtree.

Step 2 − Visit root node.

Step 3 − Recursively traverse right subtree.

void inorder(tree_pointer ptr) /* inorder tree traversal */ Recursive


{
if (ptr) {
inorder(ptr->left_child);
printf(<%d=, ptr->data);
indorder(ptr->right_child);
}
}
2. Pre - Order Traversal ( root - leftChild - rightChild )
In Pre-Order traversal, the root node is visited before left child and right child nodes. In this
traversal, the root node is visited first, then its left child and later its right child. This pre-order
traversal is applicable for every root node of all subtrees in the tree.

In the above example of binary tree, first we visit root node 'A' then visit its left child 'B' which is a
root for D and F. So we visit B's left child 'D' and again D is a root for I and J. So we visit D's left
child'I' which is the left most child. So next we go for visiting D's right child 'J'. With this we have
completed root, left and right parts of node D and root, left parts of node B. Next visit B's right
child'F'. With this we have completed root and left parts of node A. So we go for A's right
child 'C' which is a root node for G and H. After visiting C, we go for its left child 'G' which is a root

13

Downloaded by Sreya S ([email protected])


lOMoARcPSD|21950806

Wondershare
PDFelement
1.h TREES

for node K. So next we visit left of G, but it does not have left child so we go for G's right child 'K'.
With this we have completed node C's root and left parts. Next visit C's right child 'H' which is the
right most child in the tree. So we stop the process.
That means here we have visited in the order of A-B-D-I-J-F-C-G-K-H using Pre-Order Traversal.

Algorithm

Until all nodes are traversed −

Step 1 − Visit root node.

Step 2 − Recursively traverse left subtree.

Step 3 − Recursively traverse right subtree.

void preorder(tree_pointer ptr) /* preorder tree traversal */ Recursive


{
if (ptr) {
printf(<%d=, ptr->data);
preorder(ptr->left_child);
preorder(ptr->right_child);
}
}
3. Post - Order Traversal ( leftChild - rightChild - root )

In Post-Order traversal, the root node is visited after left child and right child. In this traversal, left
child node is visited first, then its right child and then its root node. This is recursively performed
until the right most node is visited.
Here we have visited in the order of I - J - D - F - B - K - G - H - C - A using Post-Order Traversal.

Algorithm

Until all nodes are traversed −


Step 1 − Recursively traverse left subtree.
Step 2 − Recursively traverse right subtree.
Step 3 − Visit root node.
void postorder(tree_pointer ptr) /* postorder tree traversal */ Recursive
{
if (ptr) {
postorder(ptr->left_child);
postorder(ptr->right_child);
printf(<%d=, ptr->data);
}
}

14

Downloaded by Sreya S ([email protected])


lOMoARcPSD|21950806

Wondershare
PDFelement
1.i TREES

Arithmetic Expression Using BT


+ inorder traversal
A / B * C * D + E
infix expression
* E
preorder traversal
+ * * / A B C D E
* D prefix expression
postorder traversal
A B / C * D * E +
/ C
postfix expression
level order traversal
A B + * E * D / C A B

CHAPTER 5 15

Trace Operations of Inorder Traversal


Call of inorder Value in root Action Call of inorder Value in root Action
1 + 11 C
2 * 12 NULL
3 * 11 C printf
4 / 13 NULL
5 A 2 * printf
6 NULL 14 D
5 A printf 15 NULL
7 NULL 14 D printf
4 / printf 16 NULL
8 B 1 + printf
9 NULL 17 E
8 B printf 18 NULL
10 NULL 17 E printf
3 * printf 19 NULL

CHAPTER 5 16

Iterative Inorder Traversal (using stack)


void iter_inorder(tree_pointer node)
{
int top= -1; /* initialize stack */
tree_pointer stack[MAX_STACK_SIZE];
for (;;) {
for (; node; node=node->left_child)
add(&top, node); /* add to stack */
node= delete(&top); /* delete from stack */
if (!node) break; /* empty stack */
printf(<%D=, node->data);
node = node->right_child;
}
}
In iterative inorder traversal, we must create our own stack to add and remove nodes as in recursion.
Analysis: Let n be number of nodes in tree, every node of tree is placed on and removed from the stack exactly once.
So time complexity is O(n). The space requirement is equal to the depth of tree which is O(n).

15

Downloaded by Sreya S ([email protected])


lOMoARcPSD|21950806

Wondershare
PDFelement
1.j TREES

Level Order Traversal ( Using Queue) -- Traversal without Stack


void level_order(tree_pointer ptr) /* level order tree traversal */
{
int front = rear = 0;
tree_pointer queue[MAX_QUEUE_SIZE];
if (!ptr) return; /* empty queue */
addq(front, &rear, ptr);
for (;;) {
ptr = deleteq(&front, rear);
if (ptr) {
printf(<%d=, ptr->data);
if (ptr->left_child)
addq(front, &rear, ptr->left_child);
if (ptr->right_child)
addq(front, &rear, ptr->right_child);
}
else break;
} }
Level order Traversal is implemented with circular queue. In this order, we visit the root first, then root’s left child
followed by root’s right child. We continue in this manner, visiting the nodes at each new level from left most to right
most nodes.
We begin by adding root to the queue. The function operates by deleting the node at the front of the queue, printing
out the node’s data field, and adding the node’s left and right children to the queue. The level order traversal for above
arithmetic expression is + * E * D / C A B.

Binary Search Trees

Binary Search Tree Representation


Binary Search tree exhibits a special behavior. A node's left child must have value less than its
parent's value and node's right child must have value greater than it's parent value.

16

Downloaded by Sreya S ([email protected])


lOMoARcPSD|21950806

Wondershare
PDFelement
1.k TREES

We're going to implement tree using node object and connecting them through references.

Definition: A binary search tree (BST) is a binary tree. It may be empty. If it is not empty,then all
nodes follows the below mentioned properties −

 Every element has a unique key.


 The keys in a nonempty left subtree (right subtree) are smaller (larger) than the key in the
root of subtree.
 The keys in a nonempty right subtree larger than the key in the root of subtree.
 The left and right subtrees are also binary search trees.

left sub-tree and right sub-tree and can be defined as −

left_subtree (keys) ≤ node (key) ≤ right_subtree (keys)

Fig: Example Binary Search Trees


ADT for Dictionary:

BST Basic Operations


The basic operations that can be performed on binary search tree data structure, are following −

 Search − search an element in a binary search tree.

 Insert − insert an element into a binary search tree / create a tree.

 Delete − Delete an element from a binary search tree.

 Height -- Height of a binary search tree.

Searching a Binary Search Tree


Let an element k is to search in binary search tree. Start search from root node of the search tree. If
root is NULL, search tree contains no nodes and search unsuccessful. Otherwise, compare k with
the key in the root. If k equals the root’s key, terminate search, if k is less than key value, search

17

Downloaded by Sreya S ([email protected])

You might also like