Вы находитесь на странице: 1из 27

Chapter 20:

Binary Trees

Copyright 2012 Pearson Education, Inc.

20.1
Definition and Application of Binary Trees

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
Copyright 2012 Pearson Education, Inc.

NULL NULL NULL

NULL

Binary Tree Terminolo y


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
Copyright 2012 Pearson Education, Inc.

NULL NULL NULL

NULL

Binary Tree Terminolo y


#eaf nodes: nodes that ha$e no children The nodes containin 7 and 43 are leaf nodes
7
NULL

31 19
NULL NULL

59 43
NULL NULL NULL

Copyright 2012 Pearson Education, Inc.

Binary Tree Terminolo y


%hild nodes, children: nodes !elow a i$en node The children of the node containin 31 are the nodes containin 19 and 59

31 19 7
NULL NULL NULL

59 43
NULL NULL NULL

Copyright 2012 Pearson Education, Inc.

Binary Tree Terminolo y


&arent node: node a!o$e a i$en node The parent of the node containin 43 is the node containin 59
7
NULL

31 19
NULL NULL

59 43
NULL NULL NULL

Copyright 2012 Pearson Education, Inc.

Binary Tree Terminolo y


'(!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
31 19 7
NULL NULL

59 43
NULL NULL NULL

NULL

Copyright 2012 Pearson Education, Inc.

)ses of Binary Trees


Binary search tree: data or ani*ed in a !inary tree to simplify searches #eft s(!tree of a node contains data $al(es + the data in the node "i ht s(!tree of a node contains $al(es , the data in the node
7
NULL

31 19
NULL NULL

59 43
NULL NULL NULL

Copyright 2012 Pearson Education, Inc.

'earchin in a Binary Tree


12-

'tart at root node E.amine node data:


a!-

c-

/s it desired $al(e0 Done Else, is desired data + node data0 "epeat step 2 with left s(!tree Else, is desired data , node data0 "epeat step 2 with ri ht s(!tree 19 7
NULL
Copyright 2012 Pearson Education, Inc.

31 59
NULL NULL NULL

1-

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

43
NULL

NULL

'earchin in a Binary Tree


To locate the node containin 43,
2 2 2 2 E.amine the root node 331- first 'ince 43 > 31, e.amine the ri ht child of the node containin 31, 359'ince 43 < 59, e.amine the left child of the node containin 59, 343The node containin 41 has !een fo(nd

31 19 7
NULL NULL NULL NULL

59 43
NULL NULL

Copyright 2012 Pearson Education, Inc.

20.2
Binary 'earch Tree 5perations

Copyright 2012 Pearson Education, Inc.

Binary 'earch Tree 5perations


%reate a !inary search tree 2 or ani*e data into a !inary search tree /nsert a node into a !inary tree 2 p(t node into tree in its correct position to maintain order 6ind a node in a !inary tree 2 locate a node with partic(lar data $al(e Delete a node from a !inary tree 2 remo$e a node and ad7(st links to maintain !inary tree

Copyright 2012 Pearson Education, Inc.

Binary 'earch Tree 8ode


A node in a !inary tree is like a node in a linked list, with two node pointer fields:
struct TreeNode { int value; TreeNode *left; TreeNode *rig t; !

Copyright 2012 Pearson Education, Inc.

%reatin a 8ew 8ode


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

/nitiali*e the contents of the node:


ne"Node$>value # nu%;

'et the pointers to NULL:


ne"Node$>Left # ne"Node$>&ig t # NULL;
'3

ne"Node

ne"Node

'3
NULL
Copyright 2012 Pearson Education, Inc.

NULL

/nsertin a 8ode in a Binary 'earch Tree


/f tree is empty, insert the new node as the root node 2- Else, compare new node a ainst left or ri ht child, dependin on whether data $al(e of new node is < or > root node 1- %ontin(e comparin and choosin left or ri ht s(!tree (ntil NULL pointer fo(nd 4- 'et this NULL pointer to point to new node
1-

Copyright 2012 Pearson Education, Inc.

/nsertin a 8ode in a Binary 'earch Tree


ne"Node E.amine this node first 2 $al(e is + node, so o to left s(!tree E.amine this node second 2 $al(e is , node, so o to ri ht s(!tree root

'3
NULL NULL

31 59
NULL NULL

19 7
NULL

43
NULL

NULL NULL

'ince the ri ht s(!tree is NULL, insert here


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 ri ht s(!tree of node

2- &reorder:
a- &rocess data in node !- Tra$erse left s(!tree of node c- Tra$erse ri ht s(!tree of node

1- &ostorder:
a- Tra$erse left s(!tree of node !- Tra$erse ri ht s(!tree of node c- &rocess data in node

Copyright 2012 Pearson Education, Inc.

Tra$ersin a Binary Tree


T"A9E"'A# 85DE' :ET;5D 9/'/TED /8 5"DE" /norder 7( 19( 31( 43( 59 31( 19( 7( 59( 43 7( 19( 43( 59( 31

31 19 7
NULL NULL NULL

59 43
NULL NULL NULL &ostorder &reorder

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(e fo(nd or NULL pointer detected %an !e implemented as a )ool f(nction

31 19 7
NULL NULL NULL

59 43
NULL NULL NULL

'earch for 430 ret(rn true 'earch for 170 ret(rn false

Copyright 2012 Pearson Education, Inc.

Deletin a 8ode from a Binary Tree 2 #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
19 7
NULL NULL NULL Deletin node with = 2 after deletion

19
NULL NULL

Deletin node with = 2 !efore deletion

Copyright 2012 Pearson Education, Inc.

Deletin a 8ode from a Binary Tree 2 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

Copyright 2012 Pearson Education, Inc.

Deletin a 8ode from a Binary Tree 2 5ne %hild


31 19 7
NULL NULL NULL

31 59 43
NULL NULL NULL

7
NULL NULL

59 43
NULL NULL NULL

Deletin node with 1> 2 !efore deletion

Deletin node with 1> 2 after deletion

Copyright 2012 Pearson Education, Inc.

Deletin a 8ode from a Binary Tree 2 Two %hildren


/f node to !e deleted has left and ri ht children,
2 ?&romote< one child to take the place of the deleted node 2 #ocate correct position for other child in s(!tree of promoted child

%on$ention in te.t: promote the ri ht child, position left s(!tree (nderneath

Copyright 2012 Pearson Education, Inc.

Deletin a 8ode from a Binary Tree 2 Two %hildren


31 19 7
NULL NULL NULL

59 59 43
NULL NULL NULL

43 19 7
NULL NULL NULL NULL Deletin node with 11 2 after deletion

NULL

Deletin node with 11 2 !efore deletion

Copyright 2012 Pearson Education, Inc.

20.1
Template %onsiderations for Binary 'earch Trees

Copyright 2012 Pearson Education, Inc.

Template %onsiderations for Binary 'earch Trees


Binary tree can !e implemented as a template, allowin fle.i!ility in determinin type of data stored /mplementation m(st s(pport relational operators >, <, and ## to allow comparison of nodes

Copyright 2012 Pearson Education, Inc.

Вам также может понравиться