Tree Data Structure
Topics to discuss
• Introduction
• Binary Trees
• Insertion in Binary Trees
• Binary Search Trees
• Traversing in Binary Trees
– Preorder
– Inorder
– Postorder
Tree Data Structures
•
There are a number of applications where linear data structures
are not appropriate.
•
Consider a genealogy tree of a family.
Mohammad Kamran
Imran Ali
Wajid
Majid
Irfan Ali
Qasim
Asim
Yasmeen
Fahd
Ahmad
Sara
Zia
Tree Data Structure
• A linear linked list will not be able to capture the
tree-like relationship with ease.
• Shortly, we will see that for applications that require
searching, linear data structures are not suitable.
• We will focus our attention on binary trees.
Binary Tree
• A binary tree is a finite set of elements that is either empty
or is partitioned into three disjoint subsets.
• The first subset contains a single element called the root of
the tree.
• The other two subsets are themselves binary trees called
the left and right sub trees.
• Each element of a binary tree is called a node of the tree.
Binary Tree
• Binary tree with 9 nodes.
A
B
C
D
F
E
G
H
I
Binary Tree
root
A
B
C
D
G
Left sub tree
F
E
H
I
Right sub tree
Binary Tree
• Recursive definition
A
node
B
C
D
Left sub tree
F
E
H
G
Right sub tree
I
Binary Tree
• Recursive definition
A
B
C
node
D
G
Left sub tree
F
E
H
I
Binary Tree
• Recursive definition
A
B
C
D
F
E
G
node
H
I
Binary Tree
• Recursive definition
A
node
B
C
D
F
E
G
H
I
Right sub tree
Binary Tree
• Recursive definition
A
B
C
node
D
F
E
H
G
Left sub tree
I
Right sub tree
Not a Tree
• Structures that are not trees.
A
B
C
D
F
E
G
H
I
Not a Tree
• Structures that are not trees.
A
B
C
D
F
E
G
H
I
Not a Tree
• Structures that are not trees.
A
B
C
D
F
E
G
H
I
Binary Tree: Terminology
parent
Left descendant
B
Right descendant
C
D
F
E
G
Leaf nodes
A
H
I
Leaf nodes
Binary Tree
• If every non-leaf node in a binary tree has nonempty left and right sub trees, the tree is termed a
strictly binary tree(Full Binary Tree).
A
B
C
D
G
F
J
E
K
H
I
Level of a Binary Tree Node
• The level of a node in a binary tree is defined as
follows:
Root has level 0,
Level of any other node is one more than the level
its parent (father).
• The depth of a binary tree is the maximum level of
any leaf in the tree.
Level of a Binary Tree Node
A 0
B 1
D 2
Level 0
C 1
F 2
E 2
G 3
Level 1
H 3
Level 2
I 3
Level 3
Perfect Binary Tree
• A Perfect binary tree of depth d is the strictly binary
all of whose leaves are at level d.
A
0
B 1
D 2
H 3
C 1
E 2
I
J 3
F 2
K
L 3
G 2
M3 N 3
O 3
Perfect Binary Tree
A
Level 0: 20 nodes
B
C
D
H
E
I
J
Level 1: 21 nodes
F
K
L
G
M
N
Level 2: 22 nodes
O
Level 3: 23 nodes
Perfect Binary Tree
• At level k, there are 2k nodes.
• Total number of nodes in the tree of depth d:
20+ 21+ 22 + ………. + 2d =
𝑑
𝑗
𝑑+1
2
=
2
−1
𝑗=0
Perfect Binary Tree
• If the tree is built out of ‘n’ nodes then
or
or
n = 2d+1 – 1
log2(n+1) = d+1
d = log2(n+1) – 1
• i.e., the depth of the complete binary tree built using ‘n’
nodes will be log2(n+1) – 1.
• For example, for n=100,000, log2(100001) is less than 20;
the tree would be 20 levels deep.
• The significance of this shallowness will become evident
later.
Operations on Binary Tree
• There are a number of operations that can be defined for a
binary tree.
• If p is pointing to a node in an existing tree then
left(p) returns pointer to the left sub tree
right (p) returns pointer to right sub tree
parent (p) returns the father of p
brother (p) returns brother of p.
info (p) returns content of the node.
However, for this course, only insertion and
deletion are important so far.
Operations on Binary Tree
• In order to construct a binary tree, the following
can be useful:
• setLeft (p, x) creates the left child node of p. The
child node contains the info ‘x’.
• setRight (p, x) creates the right child node of p.
The child node contains the info ‘x’.
Applications of Binary Trees
• A binary tree is a useful data structure when
two-way decisions must be made at each
point in a process.
• For example, suppose we wanted to find all
duplicates in a list of numbers:
14, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5
Applications of Binary Trees
• One way of finding duplicates is to compare
each number with all those that precede it.
14, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5
14, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5
Searching for Duplicates
• If the list of numbers is large and is growing, this
procedure
involves
a
large
number
of
comparisons.
• A linked list could handle the growth but the
comparisons would still be large.
• The number of comparisons can be drastically
reduced by using a binary tree.
• The tree grows dynamically like the linked list.
Searching for Duplicates
• The binary tree is built in a special way.
• The first number in the list is placed in a node
that is designated as the root of a binary tree.
• Initially, both left and right sub trees of the root
are empty.
• We take the next number and compare it with the
number placed in the root.
• If it is the same then we have a duplicate.
Searching for Duplicates
• Otherwise, we create a new tree node and put
the new number in it.
• The new node is made the left child of the root
node if the second number is less than the one in
the root.
• The new node is made the right child if the
number is greater than the one in the root.
Searching for Duplicates
14
14, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5
Searching for Duplicates
15
14
15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5
Searching for Duplicates
14
15
15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5
Searching for Duplicates
4
14
15
4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5
Searching for Duplicates
14
4
4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5
15
Searching for Duplicates
14
9
4
9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5
15
Searching for Duplicates
14
4
15
9
9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5
Searching for Duplicates
7
14
4
15
9
7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5
Searching for Duplicates
14
4
15
9
7
7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5
Searching for Duplicates
18
14
4
15
9
7
18, 3, 5, 16, 4, 20, 17, 9, 14, 5
Searching for Duplicates
14
4
15
9
7
18, 3, 5, 16, 4, 20, 17, 9, 14, 5
18
Searching for Duplicates
3
14
4
15
9
7
3, 5, 16, 4, 20, 17, 9, 14, 5
18
Searching for Duplicates
14
4
15
3
9
7
3, 5, 16, 4, 20, 17, 9, 14, 5
18
Searching for Duplicates
5
14
4
15
3
9
7
5, 16, 4, 20, 17, 9, 14, 5
18
Searching for Duplicates
14
4
15
3
9
7
5
5, 16, 4, 20, 17, 9, 14, 5
18
Searching for Duplicates
16
14
4
15
3
9
7
5
16, 4, 20, 17, 9, 14, 5
18
Searching for Duplicates
14
4
15
3
9
7
5
16, 4, 20, 17, 9, 14, 5
18
16
Searching for Duplicates
4
14
4
15
3
9
7
5
4, 20, 17, 9, 14, 5
18
16
Searching for Duplicates
20
14
4
15
3
9
7
5
20, 17, 9, 14, 5
18
16
Searching for Duplicates
14
4
15
3
9
7
5
20, 17, 9, 14, 5
18
16
20
Searching for Duplicates
17
14
4
15
3
9
7
5
17, 9, 14, 5
18
16
20
Searching for Duplicates
14
4
15
3
9
7
5
17, 9, 14, 5
18
16
20
17
Searching for Duplicates
14
4
15
3
9
7
5
9, 14, 5
18
16
20
17
Implementation
void insert(TreeNode<int>* root, int* info)
{
TreeNode<int>* node = new TreeNode<int>(info);
TreeNode<int> *p, *q;
p = q = root;
while( *info != *(p->getInfo()) && q != NULL )
{
p = q;
if( *info < *(p->getInfo()) )
q = p->getLeft();
else
q = p->getRight();
}
Implementation
if( *info == *(p->getInfo()) ){
cout << "attempt to insert duplicate: "
<< *info << endl;
delete node;
}
else if( *info < *(p->getInfo()) )
p->setLeft( node );
else
p->setRight( node );
} // end of insert
Trace of insert
p
q
17
4
14
15
3
9
7
5
17, 9, 14, 5
18
16
20
Trace of insert
p
17
14
q
4
3
15
9
7
5
17, 9, 14, 5
18
16
20
Trace of insert
17
14
p
q
4
3
15
9
7
5
17, 9, 14, 5
18
16
20
Trace of insert
17
14
p
4
3
9
7
5
17, 9, 14, 5
15
q
16
18
20
Trace of insert
17
14
4
15
3
9
7
5
17, 9, 14, 5
p
q
16
18
20
Trace of insert
17
14
4
15
3
p
9
7
5
17, 9, 14, 5
q
16
18
20
Trace of insert
17
14
4
15
3
9
7
5
17, 9, 14, 5
18
p
q
16
20
Trace of insert
17
14
4
15
3
9
7
5
17, 9, 14, 5
18
p
16
20
q
Trace of insert
14
4
15
3
9
7
18
p
16
20
5
node
17
17, 9, 14, 5
p->setRight( node );
Cost of Search
• Given that a binary tree is level d deep. How long
does it take to find out whether a number is
already present?
• Consider the insert(17) in the example tree.
• Each time around the while loop, we did one
comparison.
• After the comparison, we moved a level down.
Cost of Search
• With the binary tree in place, we can write a
routine find(x) that returns true if the number x
is present in the tree, false otherwise.
• How many comparison are needed to find out if x
is present in the tree?
• We do one comparison at each level of the tree
until either x is found or q becomes NULL.
Cost of Search
• If the binary tree is built out of n numbers, how
many comparisons are needed to find out if a
number x is in the tree?
• Recall that the depth of the complete binary tree
built using ‘n’ nodes will be log2(n+1) – 1.
• For example, for n=100,000, log2(100001) is less
than 20; the tree would be 20 levels deep.
Cost of Search
• If the tree is complete binary or nearly complete,
searching through 100,000 numbers will require
a maximum of 20 comparisons.
• Or in general, approximately log2(n).
• Compare this with a linked list of 100,000
numbers. The comparisons required could be a
maximum of n.
Binary Search Tree
• A binary tree with the property that items in the
left sub tree are smaller than the root and items
are larger or equal in the right sub tree is called
a binary search tree (BST).
• The tree we built for searching for duplicate
numbers was a binary search tree.
• BST and its variations play an important role in
searching algorithms.
Traversing a Binary Tree
• Suppose we have a binary tree, ordered (BST) or
unordered.
• We want to print all the values stored in the nodes of
the tree.
• In what order should we print them?
Traversing a Binary Tree
• Ways to print a 3 node tree:
14
4
15
(4, 14, 15), (4,15,14)
(14,4,15), (14,15,4)
(15,4,14), (15,14,4)
Traversing a Binary Tree
• In case of the general binary tree:
N
L
left
sub tree
node
right
R
sub tree
(L,N,R), (L,R,N)
(N,L,R), (N,R,L)
(R,L,N), (R,N,L)
Traversing a Binary Tree
• Three common ways
N
L
node
left
sub tree
Preorder:
Inorder:
Postorder:
right
sub tree R
(N,L,R)
(L,N,R)
(L,R,N)
Traversing a Binary Tree
void preorder(TreeNode<int>* treeNode)
{
if( treeNode != NULL )
{
cout << *(treeNode->getInfo())<<" ";
preorder(treeNode->getLeft());
preorder(treeNode->getRight());
}
}
Traversing a Binary Tree
void inorder(TreeNode<int>* treeNode)
{
if( treeNode != NULL )
{
inorder(treeNode->getLeft());
cout << *(treeNode->getInfo())<<" ";
inorder(treeNode->getRight());
}
}
Traversing a Binary Tree
void postorder(TreeNode<int>* treeNode)
{
if( treeNode != NULL )
{
postorder(treeNode->getLeft());
postorder(treeNode->getRight());
cout << *(treeNode->getInfo())<<" ";
}
}
Traversing a Binary Tree
14
4
15
3
9
7
18
16
5
Preorder: 14 4 3 9 7 5 15 18 16 17 20
20
17
Traversing a Binary Tree
14
4
15
3
9
7
18
16
5
Inorder: 3 4 5 7 9 14 15 16 17 18 20
20
17
Traversing a Binary Tree
14
4
15
3
9
7
5
Post order:
18
16
20
17
3 5 7 9 4 17 16 20 18 15 14
Search
• The function search searches the binary
search tree for a given item.
• If the item is found in the binary search
tree, it returns true; otherwise, it returns
false.
• Because the pointer root points to the root
node of the binary search tree, we must
begin our search at the root node.
Search
Deletion
• First we need to find the node that needs
to be deleted.
• For deletion, we have following four cases
Case 1
• The node to
be deleted
has no left
and right
subtrees; that
is, the node to
be deleted is
a leaf. For
example, the
node with info
45 is a leaf.
Case 2
•
The node to be
deleted has no left
subtree; that is, the
left subtree is
empty, but it has a
nonempty right
subtree. For
example, the left
subtree of node
with info 30 is
empty and its right
subtree is
nonempty.
Case 3
•
The node to be
deleted has no
right subtree;
that is, the right
subtree is
empty, but it
has a nonempty
left subtree. For
example, the
right subtree of
node with info
80 is empty and
its left subtree
is nonempty.
Case 4
• The node to
be deleted
has nonempty
left and right
subtrees. For
example, the
left and the
right subtrees
of node with
info 50 are
nonempty.
Deleted nodes in all four cases
Case 1
• We search the binary tree and
arrive at the node containing
45.
• Because this node is a leaf
and is the left child of its
parent, we can simply set the
llink of the parent node to
NULL and deallocate the
memory occupied by this node.
Case 2
• In this case, the node to be
deleted has no left subtree.
• Because 30 is the left child
of its parent node, we make
the llink of the parent node
point to the right child of 30
and then deallocate the
memory occupied by 30.
Case 3
• The node containing 80
has no right child and is
the right child of its
parent.
• Thus, we make the rlink
of the parent of 80, that
is, 70—point to the left
child of 80.
Case 4
• The node with info 50 has a
nonempty left subtree and a
nonempty right subtree.
• Here, we first reduce this case to
either Case 2 or Case 3 as follows.
• Suppose that we reduce it to Case
3—that is, the node to be deleted
has no right subtree.
• We find the immediate predecessor
of 50 in this binary tree, which is
48.
Case 4
• This is done by first going to the
left child of 50 and then locating
the rightmost node of the left
subtree of 50.
• To do so, we follow the rlink of the
nodes until we eventually arrive at
a node that has no right subtree.
• Next, we swap the info in the node
to be deleted with the info of its
immediate predecessor.
• We now apply Case 3 to
delete the node.
Exercises
Exercise
Exercise
0
You can add this document to your study collection(s)
Sign in Available only to authorized usersYou can add this document to your saved list
Sign in Available only to authorized users(For complaints, use another form )