Binary Tree Copy. 1
Binary Tree Copy. 1
Wondershare
PDFelement
1.a TREES
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
Wondershare
PDFelement
1.b TREES
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
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.
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.
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.
objects: a finite set of nodes either empty or consisting of a root node, left Binary_Tree, and right
Binary_Tree.
Functions:
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
4 H I
E 5
CHAPTER 5 10
• The subtrees of a binary tree are ordered; those of a tree are not ordered.
Wondershare
PDFelement
1.d TREES
Above two trees are different when viewed as binary trees. But same when viewed as trees.
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
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.
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.
A binary tree data structure is represented using two methods. Those methods are 1)Array
Representation 2)Linked List Representation
10
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...
int data;
11
Wondershare
PDFelement
1.f TREES
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.
CHAPTER 5 14
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
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.
I-D-J-B-F-A-G-K-C–H
Algorithm
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
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
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
14
Wondershare
PDFelement
1.i TREES
CHAPTER 5 15
CHAPTER 5 16
15
Wondershare
PDFelement
1.j TREES
16
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 −
17