Anda di halaman 1dari 112

Bismillah

Presentation
 Muhammad Irfan Khan (S-31021)
 Sheraz Khan (S-31030)
 BE(SE) -16
 Data structure
Fig 12.8 - Trees
• A tree is a non-linear data structure that consists of a
root node and potentially many levels of additional
nodes that form a hierarchy
• Nodes that have no children are called leaf nodes
• Non-root and non-leaf nodes are called internal
nodes root node
imagine an upside
down tree
internal
nodes

A tree data structure


leaf nodes
Tree can represent inheritance relationship between classes.
Organization chart
represented via a tree data
structure
president root node

VP VP VP VP

mgr mgr mgr

mgr

leaf nodes
Binary Trees
• A binary tree is defined recursively. Either
it is empty (the base case) or it consists of
a root and two subtrees, each of which is a
binary tree

root node
• Binary trees and trees typically are
represented using references as dynamic
links, though it is possible to use fixed
representations like arrays

leaf nodes
Tree Data Structures
• There are a number of applications where
linear data structures are not appropriate.
• Consider a genealogy tree of a family.
Mohammad Aslam Khan

Sohail Aslam Javed Aslam Yasmeen Aslam

Haaris Saad Qasim Asim Fahd Ahmad Sara Omer


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 subtrees.
• Each element of a binary tree is called a node of
the tree.
Binary Tree
• Binary tree with 9 nodes.
A

B C

D E F

G H I
Binary Tree
root

B C

D E F

G H I

Left subtree Right subtree


Binary Tree
• Recursive definition
A
root

B C

D E F

Left subtree G H I

Right subtree
Binary Tree
• Recursive definition
A

B C
root

D E F

G H I

Left subtree
Binary Tree
• Recursive definition
A

B C

D E F

root
G H I
Binary Tree
• Recursive definition
A
root

B C

D E F

G H I

Right subtree
Binary Tree
• Recursive definition
A

B C
root

D E F

G H I

Left subtree Right subtree


Not a Tree
• Structures that are not trees.
A

B C

D E F

G H I
Not a Tree
• Structures that are not trees.
A

B C

D E F

G H I
Not a Tree
• Structures that are not trees.
A

B C

D E F

G H I
Binary Tree: Terminology

parent
A

Left descendant B C Right descendant

D E F

G H I

Leaf nodes Leaf nodes


Binary Tree
• If every non-leaf node in a binary tree has non-empty left and right subtrees, the tree is
termed a strictly binary tree.

B C

D E J F

G 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 Level 0

B 1 C 1 Level 1

D 2 E 2 F 2 Level 2

G 3 H 3 I 3 Level 3
Complete Binary Tree
• A complete binary tree of depth d is the strictly
binary all of whose leaves are at level d.
0
A

B 1 C 1

D 2 E 2 F 2 G 2

H 3 I J 3 K L 3 M 3 N 3 O 3
Complete Binary Tree

A Level 0: 20 nodes

B C Level 1: 21 nodes

D E F G Level 2: 22 nodes

H I J K L M N O Level 3: 23 nodes
Complete Binary Tree
• At level k, there are 2k nodes.
• Total number of nodes in the tree of depth
d:
d
20+ 21+ 22 + ………. + 2d =  2j = 2d+1 – 1
j=0

• In a complete binary tree, there are 2d leaf


nodes and (2d - 1) non-leaf (inner) nodes.
Complete Binary Tree
• If the tree is built out of ‘n’ nodes then

n = 2d+1 – 1
or log2(n+1) = d+1
or 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 subtree
 right(p) returns pointer to right subtree
 parent(p) returns the father of p
 brother(p) returns brother of p.
 info(p) returns content of the node.
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 subtree
 right(p) returns pointer to right subtree
 parent(p) returns the father of p
 brother(p) returns brother of p.
 info(p) returns content of the node.
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 subtrees 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 15

4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5


Searching for Duplicates

9 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


Searching for Duplicates

7 14

4 15

7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5


Searching for Duplicates

14

4 15

7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5


Searching for Duplicates

18 14

4 15

18, 3, 5, 16, 4, 20, 17, 9, 14, 5


Searching for Duplicates

14

4 15

9 18

18, 3, 5, 16, 4, 20, 17, 9, 14, 5


Searching for Duplicates

3 14

4 15

9 18

3, 5, 16, 4, 20, 17, 9, 14, 5


Searching for Duplicates

14

4 15

3 9 18

3, 5, 16, 4, 20, 17, 9, 14, 5


Searching for Duplicates

5 14

4 15

3 9 18

5, 16, 4, 20, 17, 9, 14, 5


Searching for Duplicates

14

4 15

3 9 18

5, 16, 4, 20, 17, 9, 14, 5


Searching for Duplicates

16 14

4 15

3 9 18

16, 4, 20, 17, 9, 14, 5


Searching for Duplicates

14

4 15

3 9 18

7 16

16, 4, 20, 17, 9, 14, 5


Searching for Duplicates

4 14

4 15

3 9 18

7 16

4, 20, 17, 9, 14, 5


Searching for Duplicates

20 14

4 15

3 9 18

7 16

20, 17, 9, 14, 5


Searching for Duplicates

14

4 15

3 9 18

7 16 20

20, 17, 9, 14, 5


Searching for Duplicates

17 14

4 15

3 9 18

7 16 20

17, 9, 14, 5
Searching for Duplicates

14

4 15

3 9 18

7 16 20

5 17

17, 9, 14, 5
Searching for Duplicates

14

4 15

3 9 18

7 16 20

5 17

9, 14, 5
Trace of insert
p
17 q 14

4 15

3 9 18

7 16 20

17, 9, 14, 5
Trace of insert
p
17 14

4 q 15

3 9 18

7 16 20

17, 9, 14, 5
Trace of insert

17 14

4 p 15
q
3 9 18

7 16 20

17, 9, 14, 5
Trace of insert

17 14

4 p 15

3 9 q 18

7 16 20

17, 9, 14, 5
Trace of insert

17 14

4 15

3 9 p 18
q
7 16 20

17, 9, 14, 5
Trace of insert

17 14

4 15

3 9 p 18

7 q 16 20

17, 9, 14, 5
Trace of insert

17 14

4 15

3 9 18

7 p 16 20
q
5

17, 9, 14, 5
Trace of insert

17 14

4 15

3 9 18

7 p 16 20

5 q

17, 9, 14, 5
Trace of insert

14

4 15

3 9 18

7 p 16 20

5 node 17

17, 9, 14, 5 p->setRight( node );


Trace of insert

14

4 15

3 9 18

7 p 16 20

5 node 17

17, 9, 14, 5 p->setRight( node );


Binary Search Tree
• A binary tree with the property that items
in the left subtree are smaller than the root
and items are larger or equal in the right
subtree 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 node

L left right R
subtree subtree

(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 node

left right
L subtree subtree R

Preorder: (N,L,R)
Inorder: (L,N,R)
Postorder: (L,R,N)
Traversing a Binary Tree
14

4 15

3 9 18

7 16 20

5 17

Preorder: 14 4 3 9 7 5 15 18 16 17 20
Traversing a Binary Tree
14

4 15

3 9 18

7 16 20

5 17

Inorder: 3 4 5 7 9 14 15 16 17 18 20
Traversing a Binary Tree
14

4 15

3 9 18

7 16 20

5 17

Postorder: 3 5 7 9 4 17 16 20 18 15 14
Level-order Traversal
• There is yet another way of traversing a
binary tree that is not related to recursive
traversal procedures discussed previously.
• In level-order traversal, we visit the nodes
at each level before proceeding to the next
level.
• At each level, we visit the nodes in a left-
to-right order.
Level-order Traversal
14

4 15

3 9 18

7 16 20

5 17

Level-order: 14 4 15 3 9 18 7 16 20 5 17
Level-order Traversal
• There is yet another way of traversing a
binary tree that is not related to recursive
traversal procedures discussed previously.
• In level-order traversal, we visit the nodes
at each level before proceeding to the next
level.
• At each level, we visit the nodes in a left-
to-right order.
Level-order Traversal
14

4 15

3 9 18

7 16 20

5 17

Level-order: 14 4 15 3 9 18 7 16 20 5 17
Level-order Traversal
• How do we do level-order traversal?
• Surprisingly, if we use a queue instead of
a stack, we can visit the nodes in level-
order.
• Here is the code for level-order traversal:
Level-order Traversal
14

4 15

3 9 18

7 16 20

5 17

Queue: 14
Output:
Level-order Traversal
14

4 15

3 9 18

7 16 20

5 17

Queue: 4 15
Output: 14
Level-order Traversal
14

4 15

3 9 18

7 16 20

5 17

Queue: 15 3 9
Output: 14 4
Level-order Traversal
14

4 15

3 9 18

7 16 20

5 17

Queue: 3 9 18
Output: 14 4 15
Level-order Traversal
14

4 15

3 9 18

7 16 20

5 17

Queue: 9 18
Output: 14 4 15 3
Level-order Traversal
14

4 15

3 9 18

7 16 20

5 17

Queue: 18 7
Output: 14 4 15 3 9
Level-order Traversal
14

4 15

3 9 18

7 16 20

5 17

Queue: 7 16 20
Output: 14 4 15 3 9 18
Level-order Traversal
14

4 15

3 9 18

7 16 20

5 17

Queue: 16 20 5
Output: 14 4 15 3 9 18 7
Level-order Traversal
14

4 15

3 9 18

7 16 20

5 17

Queue: 20 5 17
Output: 14 4 15 3 9 18 7 16
Level-order Traversal
14

4 15

3 9 18

7 16 20

5 17

Queue: 5 17
Output: 14 4 15 3 9 18 7 16 20
Level-order Traversal
14

4 15

3 9 18

7 16 20

5 17

Queue: 17
Output: 14 4 15 3 9 18 7 16 20 5
Level-order Traversal
14

4 15

3 9 18

7 16 20

5 17

Queue:
Output: 14 4 15 3 9 18 7 16 20 5 17
Deleting a node in BST
• As is common with many data structures,
the hardest operation is deletion.
• Once we have found the node to be
deleted, we need to consider several
possibilities.
• If the node is a leaf, it can be deleted
immediately.
Deleting a node in BST
• If the node has one child, the node can be
deleted after its parent adjusts a pointer to
bypass the node and connect to inorder
successor.
6

2 8

1 4

3
Deleting a node in BST
• The inorder traversal order has to be
maintained after the delete.

6 6

2 8 2 8

1 4 1 4

3 3
Deleting a node in BST
• The inorder traversal order has to be
maintained after the delete.

6 6 6
 

2 8 2 8 2 8

1 4 1 4 1 3

3 3
Deleting a node in BST
• The complicated case is when the node to
be deleted has both left and right subtrees.
• The strategy is to replace the data of this
node with the smallest data of the right
subtree and recursively delete that node.
Deleting a node in BST
Delete(2): locate inorder successor

2 8

1 5

4
Inorder
successor
Deleting a node in BST
Delete(2): locate inorder successor

6  Inorder successor will be the left-most


node in the right subtree of 2.
2 8
 The inorder successor will not have a left
child because if it did, that child would be
1 5 the left-most node.

4
Inorder
successor
Deleting a node in BST
Delete(2): copy data from inorder successor

6  6

2 8 3 8

1 5 1 5

3 3

4 4
Deleting a node in BST
Delete(2): remove the inorder successor

6  6  6

2 8 3 8 3 8

1 5 1 5 1 5

3 3 3

4 4 4
Deleting a node in BST
Delete(2)

 6  6

3 8 3 8

1 5 1 5

3 4

4
Deleting a node in BST
• As is common with many data structures,
the hardest operation is deletion.
• Once we have found the node to be
deleted, we need to consider several
possibilities.
• If the node is a leaf, it can be deleted
immediately.
Deleting a node in BST
• If the node has one child, the node can be
deleted after its parent adjusts a pointer to
bypass the node and connect to inorder
successor.
6

2 8

1 4

3
Deleting a node in BST
• The inorder traversal order has to be
maintained after the delete.

6 6

2 8 2 8

1 4 1 4

3 3
Deleting a node in BST
• The inorder traversal order has to be
maintained after the delete.

6 6 6
 

2 8 2 8 2 8

1 4 1 4 1 3

3 3
Deleting a node in BST
• The complicated case is when the node to
be deleted has both left and right subtrees.
• The strategy is to replace the data of this
node with the smallest data of the right
subtree and recursively delete that node.
Deleting a node in BST
Delete(2): locate inorder successor

2 8

1 5

4
Inorder
successor
Deleting a node in BST
Delete(2): locate inorder successor

6  Inorder successor will be the left-most


node in the right subtree of 2.
2 8
 The inorder successor will not have a left
child because if it did, that child would be
1 5 the left-most node.

4
Inorder
successor
Deleting a node in BST
Delete(2): copy data from inorder successor

6  6

2 8 3 8

1 5 1 5

3 3

4 4
Deleting a node in BST
Delete(2): remove the inorder successor

6  6  6

2 8 3 8 3 8

1 5 1 5 1 5

3 3 3

4 4 4
Deleting a node in BST
Delete(2)

 6  6

3 8 3 8

1 5 1 5

3 4

Anda mungkin juga menyukai