35
1 Main Index Conten ts 1 Main Index Conten ts Tree Structures (3 (3 slides) slides) Tree Node Level and Pat h Len. (5 slides) (5 slides) Binary Tree Definition Selected Samples / Bina ry Trees Binary Tree Nodes Binary Search Trees Locating Data in a Tree Removing a Binary Tree Node stree ADT (4 slides) Using Binary Search Tre es Chapter 10 Chapter 10 Binary Trees Binary Trees Summary Slides (5 slides)

Main Index Contents 11 Main Index Contents Tree StructuresTree Structures (3 slides) Tree Structures Tree Node Level and Path Len. Tree Node Level and

  • View
    225

  • Download
    1

Embed Size (px)

Citation preview

1 Main IndexMain Index ContentsContents1 Main IndexMain Index ContentsContents

Tree Structures (3 slides) (3 slides)

Tree Node Level and Path Len.

(5 (5 slides)slides)

Binary Tree Definition

Selected Samples / Binary Trees

Binary Tree Nodes

Binary Search Trees

Locating Data in a Tree

Removing a Binary Tree Node

stree ADT (4 slides)

Using Binary Search Trees

- Removing Duplicates

Update Operations (3 slides) (3 slides)

Removing an Item From a Binary Tree

(7 slides) (7 slides)

Chapter 10 Chapter 10 – – Binary TreesBinary Trees

Summary Slides (5 slides)

2 Main IndexMain Index ContentsContents2 Main IndexMain Index ContentsContents

Tree StructuresTree Structures

P r e side n t - C E O

P r o duc t io nM a n a ge r

Sa le sM a n a ge r

Sh ip p in gSup e r v iso r

P e r so n n e lM a n a ge r

W a r e h o useSup e r v iso r

P ur c h a sin gSup e r v iso r

H IER A R C H IC A L TR EE S TR U C TU R E

3 Main IndexMain Index ContentsContents3 Main IndexMain Index ContentsContents

Tree StructuresTree Structures

+

e

/

ba

*

dc

-

B IN ARY EXPRES S IO N T REE FO R " a* b + (c -d ) / e "

4 Main IndexMain Index ContentsContents4 Main IndexMain Index ContentsContents

Tree StructuresTree Structures

A

JI

HGFE

DCB

(a)

(b )

A G EN ERAL T REE

5 Main IndexMain Index ContentsContents5 Main IndexMain Index ContentsContents

Tree Node Level and Path Tree Node Level and Path LengthLength

A

HG

FE

DCB

L e ve l: 0

L e ve l: 1

L e ve l: 2

L e ve l: 3

6 Main IndexMain Index ContentsContents6 Main IndexMain Index ContentsContents

Tree Node Level and Path Tree Node Level and Path Length – Depth DiscussionLength – Depth Discussion

A

ED

CB

GF

JIH

C o m p let e T ree (D ep t h 3 )

7 Main IndexMain Index ContentsContents7 Main IndexMain Index ContentsContents

Tree Node Level and Path Tree Node Level and Path Length – Depth DiscussionLength – Depth Discussion

A

ED

CB

GF

C o m p let e T ree (D ep t h 2 )F u ll w it h all p o s s ib le n o d es

8 Main IndexMain Index ContentsContents8 Main IndexMain Index ContentsContents

Tree Node Level and Path Tree Node Level and Path Length – Depth DiscussionLength – Depth Discussion

A

ED

CB

IH

N o n -C o m p let e T ree (D ep t h 3 )Lev el 2 is m is s in g n o d es

9 Main IndexMain Index ContentsContents9 Main IndexMain Index ContentsContents

Tree Node Level and Path Tree Node Level and Path Length – Depth DiscussionLength – Depth Discussion

A

ED

CB

GF

KIH

N o n -C o m p let eT ree (D ep t h 3 )N o d es at lev el 3 d o n o t o ccu rp y left m o s t p o s it io n s

10 Main IndexMain Index ContentsContents

Binary Tree DefinitionBinary Tree Definition A binary tree T is a finite set of nodes with one

of the following properties:– (a) T is a tree if the set of nodes is empty.

(An empty tree is a tree.)– (b) The set consists of a root, R, and exactly two

distinct binary trees, the left subtree TL and the right subtreeTR. The nodes in T consist of node R and all the nodes in TL and TR.

11 Main IndexMain Index ContentsContents11 Main IndexMain Index ContentsContents

Selected Samples of Binary Selected Samples of Binary TreesTrees

A

E

D

C

B

A

F

H

ED

CB

I

T ree ASiz e 9 D ep t h 3

T ree BSiz e 5 D ep t h 4

G

12 Main IndexMain Index ContentsContents12 Main IndexMain Index ContentsContents

Binary Tree NodesBinary Tree Nodes

Abs tract T re e M ode l

A

E F

H

D

CB

G

l e ft A ri gh t

T re e N ode M ode l

l e ft B ri gh t

l e ft E ri gh t

l e ft G ri gh t

l e ft D ri gh t

l e ft C ri gh t

l e ft H ri gh t

l e ft F ri gh t

13 Main IndexMain Index ContentsContents13 Main IndexMain Index ContentsContents

Binary Search TreesBinary Search Trees

6 53 0

3 71 0

2 5

1 5

B in ary Search T ree 1

B in ary Search T ree 2

5 3

3 0

5 9

6 2

5 0

B in ary Search T ree 3

14 Main IndexMain Index ContentsContents14 Main IndexMain Index ContentsContents

50

62373210

60533525

5530

15

Current Node Action --LOCATING DATA IN A TREE-Root = 50 Compare item = 37 and 50

37 < 50, move to the left subtreeNode = 30 Compare item = 37 and 30

37 > 30, move to the right subtreeNode = 35 Compare item = 37 and 35

37 > 35, move to the right subtreeNode = 37 Compare item = 37 and 37. Item found.

15 Main IndexMain Index ContentsContents15 Main IndexMain Index ContentsContents

Removing a Binary Search Tree Removing a Binary Search Tree NodeNode

6 53 0

3 71 0

2 5

1 5

\\ //

D elet e n o d e 2 5

3 0

6 51 0

3 7

1 56 5

3 71 0

3 0

1 5

B ad So lu t io n : 3 0 is o u t o f p lace G o o d So lu t io n

(a) (b )

16 Main IndexMain Index ContentsContents16 Main IndexMain Index ContentsContents

CLASS stree Constructors “d_stree.h”

stree();Create an empty search tree.

stree(T *first, T *last);Create a search tree with the elements from the

pointer range [first, last).

CLASS stree Opertions “d_stree.h”

void displayTree(int maxCharacters);Display the search tree. The maximum number of

characters needed to output a node value is maxCharacters.bool empty();

Return true if the tree is empty and false otherwise.

17 Main IndexMain Index ContentsContents17 Main IndexMain Index ContentsContents

CLASS stree Opertions “d_stree.h”

int erase(const T& item);Search the tree and remove item, if it is present;

otherwise, take no action. Return the number of elements removed.

Postcondition: If item is located in the tree, the size of the tree decreases by 1.

void erase(iterator pos);Erase the item pointed to the iterator pos.

Precondition: The tree is not empty and pos points to an item in the tree. If the iterator is invalid, thefunction throws the referenceError exception.

Postcondition: The tree size decreases by 1.

18 Main IndexMain Index ContentsContents18 Main IndexMain Index ContentsContents

CLASS stree Opertions “d_stree.h”

void erase(iterator first, iterator last);Remove all items in the iterator range [first, last).

Precondition: The tree is not empty. If empty, the function throws the underflowError exception.

Postcondition: The size of the tree decreases by the number of items in the range.

iterator find(const T& item);Search the tree by comparing item with the data

values in a path of nodes from the root of the tree. If a match occurs, return an iterator pointing to the matching value in the tree. If item is not in the tree, return the iterator value end().

19 Main IndexMain Index ContentsContents19 Main IndexMain Index ContentsContents

CLASS stree Opertions “d_stree.h”

Piar<iterator, bool> insert(const T& item);If item is not in the tree, insert it and return an iterator-bool pair where the iterator is the location of the

newelement and the Boolean value is true. If item is already in the tree, return the pair where the iterator locates the existing item and the Boolean value is false.

Postcondition: The size of the tree is increased by 1 if item is not present in the tree.

int size();Return the number of elements in the tree.

20 Main IndexMain Index ContentsContents20 Main IndexMain Index ContentsContents

Using Binary Search Trees Using Binary Search Trees Application: Removing DuplicatesApplication: Removing Duplicates

7

52

37 3 2 5 3 2 9 3 2 3 5 7 9

9v v

21 Main IndexMain Index ContentsContents21 Main IndexMain Index ContentsContents

Update Operations: Update Operations: 11stst of 3 steps of 3 steps

1)- The function begins at the root node and compares item 32 with the root value 25. Since 32 > 25, we traverse the right subtree and look at node 35.

2 5

4 01 2

3 52 0

p aren t

t

(a)St ep 1 : C o m p are 3 2 an d 2 5 .T rav ers e t h e righ t s u b t ree.

22 Main IndexMain Index ContentsContents22 Main IndexMain Index ContentsContents

Update Operations: Update Operations: 22ndnd of 3 steps of 3 steps

2)- Considering 35 to be the root of its own subtree, we compare item 32 with 35 and traverse the left subtree of 35.

(b )St ep 2 : C o m p are 3 2 an d 3 5 .T rav ers e t h e left s u b t ree.

2 5

4 01 2

3 52 0

t

p aren t

23 Main IndexMain Index ContentsContents23 Main IndexMain Index ContentsContents

Update Operations: Update Operations: 33rdrd of 3 steps of 3 steps

1)- Create a leaf node with data value 32. Insert the new node as the left child of node 35.

newNode = getSTNode(item,NULL,NULL,parent);

parent->left = newNode;

(c)St ep 3 : In s ert 3 2 as left ch ildo f p aren t 3 5

2 5

4 01 2

3 52 0 p aren t

3 2

24 Main IndexMain Index ContentsContents24 Main IndexMain Index ContentsContents

Removing an Item From a Removing an Item From a Binary TreeBinary Tree

1 0D

P

D e le te le a f n o d e 1 0 .p N o d e P tr-> le ft is d N o d e

N o re p la c e m e n t is n e c e s s a ry.p N o d e P tr-> le ft is N U L L

B efo re A ft er

4 0

3 5

6 53 0

5 0

3 32 6

2 5

3 42 9

2 8

P

4 0

3 5

6 53 0

5 0

3 32 6

2 5

3 42 9

2 8

25 Main IndexMain Index ContentsContents25 Main IndexMain Index ContentsContents

Removing an Item From a Removing an Item From a Binary TreeBinary Tree

D

P

D e le te n o de 3 5 with o n ly a le f t ch ild: No de R is th e le f t ch ild.

B e fo re

R

40

35

6530

50

3310 26

25

3429

28

P

A tta ch n o de R to th e pa re n t .

A fte r

R

40

33

6530

50

10 26

25

34

29

28

26 Main IndexMain Index ContentsContents26 Main IndexMain Index ContentsContents

Removing an Item From a Removing an Item From a Binary TreeBinary Tree

P

D e le te n o d e 2 6 w ith o n ly a rig h t c h ild : N o d e R is th e rig h t c h ild .

P

A tta c h n o d e R to th e p a re n t.

B efo re A ft er

R

R

4 0

3 5

6 53 0

5 0

3 31 0 2 6

2 5

3 42 9

2 8

4 0

3 5

6 53 0

5 0

3 31 0 2 9

2 5

3 42 8

D

27 Main IndexMain Index ContentsContents

Delete node 25 with two childrenNode R is the smallest node to the right

27 Main IndexMain Index ContentsContents

Removing an Item From a Removing an Item From a Binary TreeBinary Tree

P

D

R

p O fR N o d eP t r = d N o d eP t r

rN o d eP t r

p N o d eP t r

4 0

3 5

6 53 0

5 0

3 31 0 2 6

2 5

3 42 9

2 8

P

R

R

4 0

3 5

6 53 0

5 0

3 31 0

2 6

3 4

2 9

2 8

B efo re rep lacin g D b y R A ft er rep lacin g D b y R

28 Main IndexMain Index ContentsContents28 Main IndexMain Index ContentsContents

Removing an Item From a Removing an Item From a Binary TreeBinary Tree

4 0

3 5

6 53 0

D e le te n o d e 3 0 w ith tw o c h ild re n .

5 0

3 31 0 2 6

2 5

3 42 9

2 8

4 0

3 5

6 5

O rp h a n e d s u b tre e s .

5 0

3 31 0

2 5

3 4

2 6

2 9

2 8

29 Main IndexMain Index ContentsContents29 Main IndexMain Index ContentsContents

Removing an Item From a Removing an Item From a Binary TreeBinary Tree

D

R

d N o d eP t r

rN o d eP t rp O fR N o d eP t r

Pp N o d eP t r 4 0

3 5

6 53 0

5 0

3 31 0 2 6

2 5

3 42 9

2 8

P

R

4 0

3 5

6 53 0

5 0

3 41 0

2 6

3 3

2 9

2 8

p N o d eP t r

DR

rN o d eP t r

p O fR N o d eP t r

B efo re u n lin k in g R A ft e r u n lin k in g R

30 Main IndexMain Index ContentsContents30 Main IndexMain Index ContentsContents

Removing an Item From a Removing an Item From a Binary TreeBinary Tree

P

4 0

3 5

6 53 3

5 0

3 41 0

2 6

2 9

2 8

p N o d eP t r

RrN o d eP t r

P

4 0

3 5

6 53 0

5 0

3 41 0

2 6

2 9

2 8

p N o d eP t r

D3 3R

rN o d eP t r

B efo re rep lacin g D b y R A ft er rep lacin g D b y R

31 Main IndexMain Index ContentsContents31 Main IndexMain Index ContentsContents

Summary Slide 1Summary Slide 1

§- trees

- hierarchical structures that place elements in nodes along branches that originate from a root.

- Nodes in a tree are subdivided into levels in which the topmost level holds the root node.

§- Any node in a tree may have multiple successors at the next level. Hence a tree is a non-linear structure.

- Tree terminology with which you should be familiar:

parent | child | descendents | leaf node | interior node | subtree.

32 Main IndexMain Index ContentsContents32 Main IndexMain Index ContentsContents

Summary Slide 2Summary Slide 2

§- Binary Trees- Most effective as a storage structure if it has high

density §- ie: data are located on relatively short paths from the

root.

§- A complete binary tree has the highest possible density

- an n-node complete binary tree has depth int(log2n).

- At the other extreme, a degenerate binary tree is equivalent to a linked list and exhibits O(n) access times.

33 Main IndexMain Index ContentsContents33 Main IndexMain Index ContentsContents

Summary Slide 3Summary Slide 3

§- Traversing Through a Tree- There are six simple recursive algorithms for tree

traversal.

- The most commonly used ones are:

1) inorder (LNR)

2)postorder (LRN)

3)preorder (NLR).

- Another technique is to move left to right from level to level.§- This algorithm is iterative, and its implementation

involves using a queue.

34 Main IndexMain Index ContentsContents34 Main IndexMain Index ContentsContents

Summary Slide 4Summary Slide 4

§- A binary search tree stores data by value instead of position

- It is an example of an associative container.§- The simple rules

“== return”

“< go left”

“> go right”

until finding a NULL subtree make it easy to build a binary search tree that does not allow duplicate values.

35 Main IndexMain Index ContentsContents35 Main IndexMain Index ContentsContents

Summary Slide 5Summary Slide 5

§- The insertion algorithm can be used to define the path to locate a data value in the tree.

§- The removal of an item from a binary search tree is more difficult and involves finding a replacement node among the remaining values.