ds lect 16 - trees (ii)

50
Data Structures and Algorithms Binary Trees Author of the lecture slides: Dr. Sohail Aslam

Upload: programming-passion

Post on 11-Jul-2015

90 views

Category:

Engineering


5 download

TRANSCRIPT

Page 1: Ds   lect 16 - trees (ii)

Data Structures and Algorithms

Binary Trees

Author of the lecture slides: Dr. Sohail Aslam

Page 2: Ds   lect 16 - trees (ii)

Binary Tree Sequential Representation

If a complete binary tree with n nodes is represented sequentially, then

for any node with index i, 1<=i<=n, we have: parent(i) is at i/2 if i!=1. If i=1, i is at the root and has no

parent. leftChild(i) is at 2i if 2i<=n. If 2i>n, then i has no left child. rightChild(i) is at 2i+1 if 2i +1 <=n. If 2i +1 >n, then i has no

right child.

Page 3: Ds   lect 16 - trees (ii)

Sequential Representation

AB--C------D--.E

[1][2][3][4][5][6][7][8][9].[16]

[1][2][3][4][5][6][7][8][9]

ABCDEFGHI

A

B

E

C

D

A

B C

GE

I

D

H

F

(1) waste space(2) insertion/deletion

problem

Page 4: Ds   lect 16 - trees (ii)

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

Page 5: Ds   lect 16 - trees (ii)

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

Page 6: Ds   lect 16 - trees (ii)

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.

Page 7: Ds   lect 16 - trees (ii)

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.

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.

Page 8: Ds   lect 16 - trees (ii)

Searching for Duplicates

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

14

Page 9: Ds   lect 16 - trees (ii)

Searching for Duplicates

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

1415

Page 10: Ds   lect 16 - trees (ii)

Searching for Duplicates

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

14

15

Page 11: Ds   lect 16 - trees (ii)

Searching for Duplicates

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

14

15

4

Page 12: Ds   lect 16 - trees (ii)

Searching for Duplicates

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

14

154

Page 13: Ds   lect 16 - trees (ii)

Searching for Duplicates

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

14

154

9

Page 14: Ds   lect 16 - trees (ii)

Searching for Duplicates

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

14

154

9

Page 15: Ds   lect 16 - trees (ii)

Searching for Duplicates

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

14

154

9

7

Page 16: Ds   lect 16 - trees (ii)

Searching for Duplicates

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

14

154

9

7

Page 17: Ds   lect 16 - trees (ii)

Searching for Duplicates

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

14

154

9

7

18

Page 18: Ds   lect 16 - trees (ii)

Searching for Duplicates

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

14

154

9

7

18

Page 19: Ds   lect 16 - trees (ii)

Searching for Duplicates

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

14

154

9

7

18

3

Page 20: Ds   lect 16 - trees (ii)

Searching for Duplicates

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

14

154

9

7

183

Page 21: Ds   lect 16 - trees (ii)

Searching for Duplicates

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

14

154

9

7

183

5

Page 22: Ds   lect 16 - trees (ii)

Searching for Duplicates

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

14

154

9

7

183

5

Page 23: Ds   lect 16 - trees (ii)

Searching for Duplicates

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

14

154

9

7

183

5

16

Page 24: Ds   lect 16 - trees (ii)

Searching for Duplicates

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

14

154

9

7

183

5

16

Page 25: Ds   lect 16 - trees (ii)

Searching for Duplicates

4, 20, 17, 9, 14, 5

14

154

9

7

183

5

16

4

Page 26: Ds   lect 16 - trees (ii)

Searching for Duplicates

20, 17, 9, 14, 5

14

154

9

7

183

5

16

20

Page 27: Ds   lect 16 - trees (ii)

Searching for Duplicates

20, 17, 9, 14, 5

14

154

9

7

183

5

16 20

Page 28: Ds   lect 16 - trees (ii)

Searching for Duplicates

17, 9, 14, 5

14

154

9

7

183

5

16 20

17

Page 29: Ds   lect 16 - trees (ii)

Searching for Duplicates

17, 9, 14, 5

14

154

9

7

183

5

16 20

17

Page 30: Ds   lect 16 - trees (ii)

Searching for Duplicates

9, 14, 5

14

154

9

7

183

5

16 20

17

Page 31: Ds   lect 16 - trees (ii)

C++ Implementation – The TreeNode class

Page 32: Ds   lect 16 - trees (ii)

C++ Implementation – The TreeNode class

Page 33: Ds   lect 16 - trees (ii)

C++ Implementation – The TreeNode class

Page 34: Ds   lect 16 - trees (ii)

C++ Implementation – The BinaryTree class

Page 35: Ds   lect 16 - trees (ii)

C++ Implementation – The BinaryTree class

Page 36: Ds   lect 16 - trees (ii)

C++ Implementation – Building a BinaryTree

Page 37: Ds   lect 16 - trees (ii)

Trace of insert

17, 9, 14, 5

14

154

9

7

183

5

16 20

17pq

Page 38: Ds   lect 16 - trees (ii)

Trace of insert

17, 9, 14, 5

14

154

9

7

183

5

16 20

17p

q

Page 39: Ds   lect 16 - trees (ii)

Trace of insert

17, 9, 14, 5

14

154

9

7

183

5

16 20

17

pq

Page 40: Ds   lect 16 - trees (ii)

Trace of insert

17, 9, 14, 5

14

154

9

7

183

5

16 20

17

p

q

Page 41: Ds   lect 16 - trees (ii)

Trace of insert

17, 9, 14, 5

14

154

9

7

183

5

16 20

17

pq

Page 42: Ds   lect 16 - trees (ii)

Trace of insert

17, 9, 14, 5

14

154

9

7

183

5

16 20

17

p

q

Page 43: Ds   lect 16 - trees (ii)

Trace of insert

17, 9, 14, 5

14

154

9

7

183

5

16 20

17

pq

Page 44: Ds   lect 16 - trees (ii)

Trace of insert

17, 9, 14, 5

14

154

9

7

183

5

16 20

17

p

q

Page 45: Ds   lect 16 - trees (ii)

Trace of insert

17, 9, 14, 5

14

154

9

7

183

5

16 20

17

p

p->setRight( node );

node

Page 46: Ds   lect 16 - trees (ii)

Adding Traversal Functions to the BinaryTree class

Visiting /reading information of a node:

Page 47: Ds   lect 16 - trees (ii)

In-order (LNR) Traversal

Page 48: Ds   lect 16 - trees (ii)

Pre-order Traversal

Page 49: Ds   lect 16 - trees (ii)

Post-order Traversal

Page 50: Ds   lect 16 - trees (ii)

The driver/main function