27
Copyright © 2012 Pearson Education, Inc. Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees

Chapter 20 Binary Trees

Embed Size (px)

Citation preview

7/27/2019 Chapter 20 Binary Trees

http://slidepdf.com/reader/full/chapter-20-binary-trees 1/27

Copyright © 2012 Pearson Education, Inc.Copyright © 2012 Pearson Education, Inc.

Chapter 20:

Binary Trees

7/27/2019 Chapter 20 Binary Trees

http://slidepdf.com/reader/full/chapter-20-binary-trees 2/27

Copyright © 2012 Pearson Education, Inc.Copyright © 2012 Pearson Education, Inc.

20.1Definition and Application of

Binary Trees

7/27/2019 Chapter 20 Binary Trees

http://slidepdf.com/reader/full/chapter-20-binary-trees 3/27

Copyright © 2012 Pearson Education, Inc.Copyright © 2012 Pearson Education, Inc.

Definition and Application of

Binary Trees• Binary tree: a nonlinear linked list in which each

node may point to 0, 1, or two other nodes

• Each node contains

one or more

data fields and

two pointers

NULL   NULL

NULL NULL NULL NULL

7/27/2019 Chapter 20 Binary Trees

http://slidepdf.com/reader/full/chapter-20-binary-trees 4/27

Copyright © 2012 Pearson Education, Inc.Copyright © 2012 Pearson Education, Inc.

Binary Tree Terminoloy

• Tree pointer: like a

head pointer for a

linked list, it points to

the first node in the!inary tree

• "oot node: the node at

the top of the tree

NULL   NULL

NULL NULL NULL NULL

7/27/2019 Chapter 20 Binary Trees

http://slidepdf.com/reader/full/chapter-20-binary-trees 5/27

Copyright © 2012 Pearson Education, Inc.Copyright © 2012 Pearson Education, Inc.

Binary Tree Terminoloy

• #eaf nodes: nodes thatha$e no children

The nodes containin7 and 43 are leafnodes

NULL   NULL7

19

31

43

59

NULL NULL NULL NULL

7/27/2019 Chapter 20 Binary Trees

http://slidepdf.com/reader/full/chapter-20-binary-trees 6/27

Copyright © 2012 Pearson Education, Inc.Copyright © 2012 Pearson Education, Inc.

Binary Tree Terminoloy

• %hild nodes,children: nodes!elow a i$en node

The children of thenode containin 31 are the nodescontainin 19 and59

NULL   NULL7

19

31

43

59

NULL NULL NULL NULL

7/27/2019 Chapter 20 Binary Trees

http://slidepdf.com/reader/full/chapter-20-binary-trees 7/27Copyright © 2012 Pearson Education, Inc.Copyright © 2012 Pearson Education, Inc.

Binary Tree Terminoloy

• &arent node: nodea!o$e a i$en node

The parent of the nodecontainin 43 is thenode containin 59

NULL   NULL7

19

31

43

59

NULL NULL NULL NULL

7/27/2019 Chapter 20 Binary Trees

http://slidepdf.com/reader/full/chapter-20-binary-trees 8/27Copyright © 2012 Pearson Education, Inc.Copyright © 2012 Pearson Education, Inc.

Binary Tree Terminoloy

• '(!tree: the portion of

a tree from a node

down to the lea$es

The nodes containin

19 and 7 are the left

s(!tree of the node

containin 31  NULL   NULL7

19

31

43

59

NULL NULL NULL NULL

7/27/2019 Chapter 20 Binary Trees

http://slidepdf.com/reader/full/chapter-20-binary-trees 9/27Copyright © 2012 Pearson Education, Inc.Copyright © 2012 Pearson Education, Inc.

)ses of Binary Trees

• Binary search tree: dataorani*ed in a !inary treeto simplify searches

• #eft s(!tree of a node

contains data $al(es +the data in the node

• "iht s(!tree of a nodecontains $al(es the

data in the nodeNULL   NULL7

19

31

43

59

NULL NULL NULL NULL

7/27/2019 Chapter 20 Binary Trees

http://slidepdf.com/reader/full/chapter-20-binary-trees 10/27Copyright © 2012 Pearson Education, Inc.Copyright © 2012 Pearson Education, Inc.

'earchin in a Binary Tree

1- 'tart at root node

2- Eamine node data:a- /s it desired $al(e Done

!- Else, is desired data + node

data "epeat step 2 with lefts(!tree

c- Else, is desired data nodedata "epeat step 2 withriht s(!tree

- %ontin(e (ntil desired$al(e fo(nd or NULL pointer reached

NULL   NULL7

19

31

43

59

NULL NULL NULL NULL

7/27/2019 Chapter 20 Binary Trees

http://slidepdf.com/reader/full/chapter-20-binary-trees 11/27Copyright © 2012 Pearson Education, Inc.Copyright © 2012 Pearson Education, Inc.

'earchin in a Binary TreeTo locate the node containin 43, 

 

Eamine the root node 331- first

 

'ince 43 > 31, eamine the riht child of the node containin 31, 359-

 

'ince 43 < 59, eamine the left child of the node containin 59, 343-

 

The node containin4 has !een fo(nd

NULL   NULL7

19

31

43

59

NULL NULL NULL NULL

7/27/2019 Chapter 20 Binary Trees

http://slidepdf.com/reader/full/chapter-20-binary-trees 12/27Copyright © 2012 Pearson Education, Inc.Copyright © 2012 Pearson Education, Inc.

20.2Binary 'earch Tree 5perations

7/27/2019 Chapter 20 Binary Trees

http://slidepdf.com/reader/full/chapter-20-binary-trees 13/27Copyright © 2012 Pearson Education, Inc.Copyright © 2012 Pearson Education, Inc.

Binary 'earch Tree 5perations

• %reate a !inary search tree orani*e datainto a !inary search tree

• /nsert a node into a !inary tree p(t node intotree in its correct position to maintain order 

• 6ind a node in a !inary tree locate a nodewith partic(lar data $al(e

• Delete a node from a !inary tree remo$e anode and ad7(st links to maintain !inary tree

7/27/2019 Chapter 20 Binary Trees

http://slidepdf.com/reader/full/chapter-20-binary-trees 14/27Copyright © 2012 Pearson Education, Inc.Copyright © 2012 Pearson Education, Inc.

Binary 'earch Tree 8ode

•  A node in a !inary tree is like a node ina linked list, with two node pointer fields:

struct TreeNode

{

int value;

TreeNode *left;

TreeNode *rigt;!

7/27/2019 Chapter 20 Binary Trees

http://slidepdf.com/reader/full/chapter-20-binary-trees 15/27

Copyright © 2012 Pearson Education, Inc.Copyright © 2012 Pearson Education, Inc.

%reatin a 8ew 8ode

•  Allocate memory for new node:ne"Node # ne" TreeNode;

• /nitiali*e the contents of the node:

ne"Node$>value # nu%;• 'et the pointers to NULL:

ne"Node$>Left

# ne"Node$>&igt

# NULL;   ne"Node

'3

NULLNULL

ne"Node

ne"Node

'3

7/27/2019 Chapter 20 Binary Trees

http://slidepdf.com/reader/full/chapter-20-binary-trees 16/27

Copyright © 2012 Pearson Education, Inc.Copyright © 2012 Pearson Education, Inc.

/nsertin a 8ode in a

Binary 'earch Tree

1- /f tree is empty, insert the new node as the rootnode

2- Else, compare new node aainst left or rihtchild, dependin on whether data $al(e of newnode is < or > root node

- %ontin(e comparin and choosin left or rihts(!tree (ntil NULL pointer fo(nd

4- 'et this NULL pointer to point to new node

7/27/2019 Chapter 20 Binary Trees

http://slidepdf.com/reader/full/chapter-20-binary-trees 17/27

Copyright © 2012 Pearson Education, Inc.Copyright © 2012 Pearson Education, Inc.

/nsertin a 8ode in a

Binary 'earch Tree

NULL   NULL7

19

31

43

59

root

Eamine this node first $al(e is + node, so o toleft s(!tree

Eamine this nodesecond $al(e is node, so o toriht s(!tree

'ince the riht s(!treeis NULL, insert here

NULL NULL NULL NULL

ne"Node

'3

NULLNULL

7/27/2019 Chapter 20 Binary Trees

http://slidepdf.com/reader/full/chapter-20-binary-trees 18/27

Copyright © 2012 Pearson Education, Inc.Copyright © 2012 Pearson Education, Inc.

Tra$ersin a Binary Tree

Three tra$ersal methods:1- /norder:

a- Tra$erse left s(!tree of node

!- &rocess data in node

c- Tra$erse riht s(!tree of node2- &reorder:

a- &rocess data in node

!- Tra$erse left s(!tree of node

c- Tra$erse riht s(!tree of node

- &ostorder:a- Tra$erse left s(!tree of node

!- Tra$erse riht s(!tree of node

c- &rocess data in node

7/27/2019 Chapter 20 Binary Trees

http://slidepdf.com/reader/full/chapter-20-binary-trees 19/27

Copyright © 2012 Pearson Education, Inc.Copyright © 2012 Pearson Education, Inc.

Tra$ersin a Binary Tree

T"A9E"'A#ET;5D

85DE'9/'/TED /85"DE"

/norder    7( 19( 31(

43( 59

&reorder    31( 19( 7(

59( 43

&ostorder    7( 19( 43(

59( 31

NULL   NULL7

19

31

43

59

NULL NULL NULL NULL

7/27/2019 Chapter 20 Binary Trees

http://slidepdf.com/reader/full/chapter-20-binary-trees 20/27

Copyright © 2012 Pearson Education, Inc.Copyright © 2012 Pearson Education, Inc.

'earchin in a Binary Tree

• 'tart at root node,

tra$erse the tree

lookin for $al(e

• 'top when $al(efo(nd or NULL 

pointer detected

• %an !e implemented

as a )ool f(nction

NULL   NULL7

19

31

43

59

NULL NULL NULL NULL

'earch for 43 ret(rn true'earch for 17 ret(rn false

7/27/2019 Chapter 20 Binary Trees

http://slidepdf.com/reader/full/chapter-20-binary-trees 21/27

Copyright © 2012 Pearson Education, Inc.Copyright © 2012 Pearson Education, Inc.

Deletin a 8ode from a

Binary Tree #eaf 8ode• /f node to !e deleted is a leaf node, replace

parent node<s pointer to it with a NULL pointer,

then delete the node

NULL7

19

NULL NULL

Deletin node with =  !efore deletion

NULL

19

NULL

Deletin node with =  after deletion

7/27/2019 Chapter 20 Binary Trees

http://slidepdf.com/reader/full/chapter-20-binary-trees 22/27

Copyright © 2012 Pearson Education, Inc.Copyright © 2012 Pearson Education, Inc.

Deletin a 8ode from a

Binary Tree 5ne %hild

• /f node to !e deleted has one child node,

ad7(st pointers so that parent of node to !e

deleted points to child of node to !e deleted,

then delete the node

7/27/2019 Chapter 20 Binary Trees

http://slidepdf.com/reader/full/chapter-20-binary-trees 23/27

Copyright © 2012 Pearson Education, Inc.Copyright © 2012 Pearson Education, Inc.

Deletin a 8ode from a

Binary Tree 5ne %hild

NULL   NULL7

19

31

43

59

NULL NULL NULL NULL

Deletin node with 1>  !efore deletion

NULL

7

31

43

59

NULL NULL

NULL NULL

Deletin node with 1>  after deletion

7/27/2019 Chapter 20 Binary Trees

http://slidepdf.com/reader/full/chapter-20-binary-trees 24/27

Copyright © 2012 Pearson Education, Inc.Copyright © 2012 Pearson Education, Inc.

Deletin a 8ode from a

Binary Tree Two %hildren

• /f node to !e deleted has left and riht children,  ?&romote< one child to take the place of the deleted

node

  #ocate correct position for other child in s(!tree ofpromoted child

• %on$ention in tet: promote the riht child,position left s(!tree (nderneath

7/27/2019 Chapter 20 Binary Trees

http://slidepdf.com/reader/full/chapter-20-binary-trees 25/27

Copyright © 2012 Pearson Education, Inc.Copyright © 2012 Pearson Education, Inc.

Deletin a 8ode from a

Binary Tree Two %hildren

NULL   NULL7

19

31

43

59

NULL NULL NULL NULL

Deletin node with 1  !efore deletion Deletin node with 1

  after deletion

43

59

NULL

NULL

7

19

NULL NULL

NULL

7/27/2019 Chapter 20 Binary Trees

http://slidepdf.com/reader/full/chapter-20-binary-trees 26/27

Copyright © 2012 Pearson Education, Inc.Copyright © 2012 Pearson Education, Inc.

20.Template %onsiderations for

Binary 'earch Trees

% f

7/27/2019 Chapter 20 Binary Trees

http://slidepdf.com/reader/full/chapter-20-binary-trees 27/27

C i ht © 2012 P Ed ti IC i ht © 2012 P Ed ti I

Template %onsiderations for

Binary 'earch Trees

• Binary tree can !e implemented as a

template, allowin flei!ility in determinin

type of data stored• /mplementation m(st s(pport relational

operators >, <, and ## to allow

comparison of nodes