Lecture12 Updated

Embed Size (px)

Citation preview

  • 8/9/2019 Lecture12 Updated

    1/52

    Lecture #12

    Binary Tree Traversals• Using Binary Trees to Evaluate Expressions

    • Binary Search Trees• Binary Search Tree Operations

     – Searching for an item – Inserting a ne item – !in"ing the minimum an" maximum items – rinting out the items in or"er –

    $eleting the hole tree

  • 8/9/2019 Lecture12 Updated

    2/52

    Binary Tree Traversals%hen e process all the no"es in a tree& it's calle" a

    traversal(

    There are four common ays to traverse a tree(

    1( re)or"er traversal2( In)or"er traversal*( ost)or"er traversal

      +( Level)or"er traversal

    Let's see a pre)or"er traversal first,

  • 8/9/2019 Lecture12 Updated

    3/52

    The reor"er Traversal

    reor"er-  1( rocess the current no"e(  2( rocess the no"es in the

    left su.)tree(  *( rocess the no"es in the

    right su.)tree(

    By /process the current no"e0 e typically mean one ofthe folloing-

    1( rint the current no"e's value out(2( Search the current no"e to see if its value matches

    the one you're searching for(*( "" the current no"e's value to a total for the tree+( Etc

    NULL

      /a0

      /.0   /c0NULL

      /"0NULL NULL NULL  /e0NULL

    root

  • 8/9/2019 Lecture12 Updated

    4/52

    voi" reOr"er34o"e 5cur67

      if 3cur 88 4ULL6 99 if empty& return  return:

      cout ;; cur)

  • 8/9/2019 Lecture12 Updated

    5/52

    voi" reOr"er34o"e 5cur67

      if 3cur 88 4ULL6 99 if empty& return  return:

      cout ;; cur)

  • 8/9/2019 Lecture12 Updated

    6/52

    voi" reOr"er34o"e 5cur67

      if 3cur 88 4ULL6 99 if empty& return  return:

      cout ;; cur)

  • 8/9/2019 Lecture12 Updated

    7/52

    voi" reOr"er34o"e 5cur67

      if 3cur 88 4ULL6 99 if empty& return  return:

      cout ;; cur)

  • 8/9/2019 Lecture12 Updated

    8/52

    voi" reOr"er34o"e 5cur67

      if 3cur 88 4ULL6 99 if empty& return  return:

      cout ;; cur)

  • 8/9/2019 Lecture12 Updated

    9/52

    voi" reOr"er34o"e 5cur67

      if 3cur 88 4ULL6 99 if empty& return  return:

      cout ;; cur)

  • 8/9/2019 Lecture12 Updated

    10/52

    The In)or"er Traversal1( rocess the no"es in the left

    su.)tree(2( rocess the current no"e(*( rocess the no"es in the right

    su.)tree(

    voi" InOr"er34o"e 5cur67

      if 3cur 88 4ULL6 99 if empty& return  return:

      InOr"er3cur)

  • 8/9/2019 Lecture12 Updated

    11/52

    The In)or"er Traversal1( rocess the no"es in the left

    su.)tree(2( rocess the current no"e(*( rocess the no"es in the right

    su.)tree(

    voi" InOr"er34o"e 5cur67

      if 3cur 88 4ULL6 99 if empty& return  return:

      InOr"er3cur)

  • 8/9/2019 Lecture12 Updated

    12/52

    The ost)or"er Traversal1( rocess the no"es in the left

    su.)tree(2( rocess the no"es in the right

    su.)tree(*( rocess the current no"e(

    voi" ostOr"er34o"e 5cur67

      if 3cur 88 4ULL6 99 if empty& return  return:

      ostOr"er3cur)

  • 8/9/2019 Lecture12 Updated

    13/52

    The ost)or"er Traversal1( rocess the no"es in the left

    su.)tree(2( rocess the no"es in the right

    su.)tree(*( rocess the current no"e(

    voi" ostOr"er34o"e 5cur67

      if 3cur 88 4ULL6 99 if empty& return  return:

      ostOr"er3cur)

  • 8/9/2019 Lecture12 Updated

    14/52

    The Level Or"er TraversalIn a level or"er traversal e visit each level's no"es& from

    left to right& .efore visiting no"es in the next level(

    NULL

      /a0

      /.0   /c0

      /"0NULL NULL NULL

      /e0NULL

    root

    >ere's the algorithm-

    1( Use a temp pointer varia.le an"

    a ?ueue of no"e pointers(2( Insert the root no"e pointer

    into the ?ueue(*( %hile the ?ueue is not empty-

    ( $e?ueue the top no"epointer an" put it in temp(B( rocess the no"e(@( "" the no"e's chil"ren to

    ?ueue if they are not 4ULL(

    front rear

    A

    A2 AC

    C AD

    720780

    800760

    A

    A

    temp A

    a

    A2AC

    A2

    AC

    .

    CAD

    AC

    CAD

    c

    NULL

      /f0NULL

    900

    C

    AD

    " Etc

  • 8/9/2019 Lecture12 Updated

    15/52

    Traversal Overvie& art 1

    NULL

      /a0

      /.0   /c0

    NULL

      /"0NULL NULL NULL

      /e0NULL

    root

    re)or"er

    #1

    #2

    #*

    #+

    #F

    #C ##1

    #1*

    #1+

    #1F

    #1D

    #1

    NULL

      /a0

      /.0   /c0

    NULL

      /"0

    NULL NULL NULL

      /e0

    NULL

    root

    In)or"er

    #1+

    #1

    #A

    #2

    #+

    #D #C#1

    #12

    #1* #1F #1A

    #1

    #D #A #11 #12

    #1A #1C

    #* #F # #11

    #1D #1C

    1( rocess current no"e

    2( Traverse left*( Traverse right

    1( Traverse left

    2( rocess current no"e*( Traverse right

  • 8/9/2019 Lecture12 Updated

    16/52

    Traversal Overvie& art 2

    NULL

      /a0

      /.0   /c0

    NULL

      /"0NULL NULL NULL

      /e0NULL

    root

    ost)or"er

    #1

    #1

    #12

    #2

    #F

    #D #A#1

    #11

    #1*

    #1+

    #1A

    #1C

    NULL

      /a0

      /.0   /c0

    NULL

      /"0

    NULL NULL NULL

      /e0

    NULL

    root

    Level)or"er

    #1

    #2

    #+#F

    #*

    #* #+ #C #

    #1F #1D

    1( Traverse left2( Traverse right*( rocess current no"e

  • 8/9/2019 Lecture12 Updated

    17/52

    Big)Oh of TraversalsG

    Huestion- %hat're the .ig)ohs of each of our traversalsG

    nser- %ell& since a traversal must  visit each no"eexactly once

      an" since there are n no"es in a tree

      the .ig)oh for any of the traversals is

    O3n6

  • 8/9/2019 Lecture12 Updated

    18/52

    Traversal @hallenge

    @hallenge- %hat or"er ill thefolloing no"es .e printe" out ife use an in)or"er traversalG

    NULL

      /Larry0

      /!ran0   /on"a0

      /$anny0NULL NULL NULL

      /JacK0NULL

    root

      /Tom0NULL

      /Sam0NULL NULL

    • The class ill split intoleft an" right teams

    • One stu"ent from eachteam ill come up to the

    .oar"• Each stu"ent can either

     – rite one ne item or – fix a single error in

    their teammatessolution

    • Then the next to peoplecome up& etc(

    • The team that completes

    their program first ins,

    ULES

  • 8/9/2019 Lecture12 Updated

    19/52

    Expression Evaluation%e can represent arithmetic expressions using a

    .inary tree(

    ptr

    !or example& the tree onthe left represents the

    expression- 3FD653*)16

    Once you have an expressionin a tree& its easy to

    evaluate it an" get theresult(

    Let's see ho,

  • 8/9/2019 Lecture12 Updated

    20/52

    Expression Evaluation

    1( If the current no"e is a

    num.er& return its value(

    2( ecursively evaluate the leftsu.tree an" get the result(

    *( ecursively evaluate the right

    su.tree an" get the result(+( pply the operator in the

    current no"e to the left an"right results: return the

    result(

    ptr

    3FD653*)16

    >ere's our evaluation function( %e start .y passing in a

    pointer to the root of the tree(

    cur

    1( If the current no"e is a

    num.er& return its value(2( ecursively evaluate the left

    su.tree an" get the result(

    *( ecursively evaluate the rightsu.tree an" get the result(

    +( pply the operator in thecurrent no"e to the left an"right results: return theresult(

    cur

    1( If the current no"e is anum.er& return its value(

    2( ecursively evaluate the leftsu.tree an" get the result(

    *( ecursively evaluate the rightsu.tree an" get the result(

    +( pply the operator in thecurrent no"e to the left an"right results: return theresult(

       c    u    r

  • 8/9/2019 Lecture12 Updated

    21/52

    Expression Evaluation

    1( If the current no"e is a

    num.er& return its value(

    2( ecursively evaluate the leftsu.tree an" get the result(

    *( ecursively evaluate the right

    su.tree an" get the result(+( pply the operator in the

    current no"e to the left an"right results: return the

    result(

    ptr

    3FD653*)16

    >ere's our evaluation function( %e start .y passing in a

    pointer to the root of the tree(

    cur

    1( If the current no"e is a

    num.er& return its value(2( ecursively evaluate the left

    su.tree an" get the result(

    *( ecursively evaluate the rightsu.tree an" get the result(

    +( pply the operator in thecurrent no"e to the left an"right results: return theresult(

    cur

    esult 8 F

    1( If the current no"e is anum.er& return its value(

    2( ecursively evaluate the leftsu.tree an" get the result(

    *( ecursively evaluate the rightsu.tree an" get the result(

    +( pply the operator in thecurrent no"e to the left an"right results: return theresult(

       c    u    r

  • 8/9/2019 Lecture12 Updated

    22/52

    Expression Evaluation

    1( If the current no"e is a

    num.er& return its value(

    2( ecursively evaluate the leftsu.tree an" get the result(

    *( ecursively evaluate the right

    su.tree an" get the result(+( pply the operator in the

    current no"e to the left an"right results: return the

    result(

    ptr

    3FD653*)16

    >ere's our evaluation function( %e start .y passing in a

    pointer to the root of the tree(

    cur

    1( If the current no"e is a

    num.er& return its value(2( ecursively evaluate the left

    su.tree an" get the result(

    *( ecursively evaluate the rightsu.tree an" get the result(

    +( pply the operator in thecurrent no"e to the left an"right results: return theresult(

    cur

    esult 8 F

    esult 8 D

    FD 8 11

  • 8/9/2019 Lecture12 Updated

    23/52

    Expression Evaluation

    1( If the current no"e is a

    num.er& return its value(

    2( ecursively evaluate the leftsu.tree an" get the result(

    *( ecursively evaluate the right

    su.tree an" get the result(+( pply the operator in the

    current no"e to the left an"right results: return the

    result(

    ptr

    3FD653*)16

    >ere's our evaluation function( %e start .y passing in a

    pointer to the root of the tree(

    cur

    *)1 8 2

    esult 8 11

    1( If the current no"e is a

    num.er& return its value(2( ecursively evaluate the left

    su.tree an" get the result(

    *( ecursively evaluate the rightsu.tree an" get the result(

    +( pply the operator in thecurrent no"e to the left an"right results: return theresult(

      c   u  result 8 *

    esult 8 1

  • 8/9/2019 Lecture12 Updated

    24/52

    Expression Evaluation

    1( If the current no"e is a

    num.er& return its value(2( ecursively evaluate the left

    su.tree an" get the result(

    *( ecursively evaluate the right

    su.tree an" get the result(+( pply the operator in the

    current no"e to the left an"right results: return the

    result(

    ptr

    3FD653*)16

    >ere's our evaluation function( %e start .y passing in a

    pointer to the root of the tree(

    cur

    *)1 8 2

    esult 8 11

    esult 8 2

    1152822

    The result is 22(

  • 8/9/2019 Lecture12 Updated

    25/52

    Expression Evaluation

    1( If the current no"e is a

    num.er& return its value(2( ecursively evaluate the left

    su.tree an" get the result(

    *( ecursively evaluate the right

    su.tree an" get the result(+( pply the operator in the

    current no"e to the left an"right results: return the

    result(

    >ere's our evaluation function( %e start .y passing in a

    pointer to the root of the tree(

    Huestion- %hich otheralgorithm "oes this remin"

     you ofG

  • 8/9/2019 Lecture12 Updated

    26/52

    Binary Search Trees

    Binary Search Trees are a type of .inary tree ithspecific properties that maKe them very efficient to

    search for a value in the tree(

    LiKe regular Binary Trees&e store an" search forvalues in Binary Search

    Trees

    >ere's an example BST

    NULL

      /Larry0

      /!ran0   /on"a0

      /$anny0NULL NULL NULL

      /JacK0NULL

    root

      /Tom0NULL

      /Sam0NULL NULL

  • 8/9/2019 Lecture12 Updated

    27/52

  • 8/9/2019 Lecture12 Updated

    28/52

    Binary Search TreesHuestion- %hich of the folloing are vali" BSTsG

    NULL

      /Larry0

      /!ran0   /on"a0

      /$anny0NULL NULL NULL

      /4icK0NULL

    NULL

      /Larry0

      /!ran0

      /$anny0NULL

    NULL

      /lex0NULL NULL

    NULL

      /Nanny0

      /my0   /4icK0

    NULL

      /Na""y0NULL

    NULLNULLNULL

  • 8/9/2019 Lecture12 Updated

    29/52

    Operations on a Binary Search Tree

    • $etermine if the .inary search tree is empty• Search the .inary search tree for a value• Insert an item in the .inary search tree• $elete an item from the .inary search tree• !in" the height of the .inary search tree• !in" the num.er of no"es an" leaves in the

    .inary search tree• Traverse the .inary search tree• !ree the memory use" .y the .inary search tree

    >ere's hat e can "o to a BST-

    hi B T

  • 8/9/2019 Lecture12 Updated

    30/52

    Searching a BSTInput- value to search for

    Output- TUE if foun"& !LSE otheriseStart at the root of the treePeep going until e hit the 4ULL pointer

      If is e?ual to current no"e's value& then foun",  If is less than current no"e's value& go left  If is greater than current no"e's value& go right

    If e hit a 4ULL pointer& not foun"(

    NULL

      /Larry0

      /!ran0   /on"a0

    NULL NULL NULL  /Mary0NULL

    NULL

      /Barry0

    Let's search for

    Mary(

    Mary 88 LarryGGMary ; LarryGGMary 88 !ranGGMary ; !ranGGMary < !ranGGMary 88 MaryGG

    S hi BST

  • 8/9/2019 Lecture12 Updated

    31/52

    Searching a BSTStart at the root of the tree

    Peep going until e hit the 4ULL pointer  If is e?ual to current no"e's value& then foun",  If is less than current no"e's value& go left  If is greater than current no"e's value& go right

    If e hit a 4ULL pointer& not foun"(

    Sho ho to search for-

    1( Phang2( $ale*( Sam

  • 8/9/2019 Lecture12 Updated

    32/52

    Searching a BST >ere are to "ifferent BST search algorithms in @&

    one recursive an" one iterative-

     bool Search(int V, Node *ptr)

    {

    if (ptr == NULL)return(false); // nope

      else if (V == ptr-!alue)

      return(true); // found"""

      else if (V # ptr-!alue)

      return(Search(V,ptr-left));

      elsereturn(Search(V,ptr-ri$ht));

    %

     bool Search(int V,Node *ptr)

    {

     &hile (ptr "= NULL)

      { if (V == ptr-!alue)

    return(true);

      else if (V # ptr-!alue)

      ptr = ptr-left;

      else

      ptr = ptr-ri$ht;  %

    return(false); // nope

    %

    Let's trace through the recursive version

  • 8/9/2019 Lecture12 Updated

    33/52

    ecursive BST SearchLets search for 1+(

     bool Search(int V, Node *ptr)

    {

    if (ptr == NULL)

    return(false); // nope

      else if (V == ptr-!alue)  return(true); // found"""

      else if (V # ptr-!alue)

      return(Search(V, ptr-left));

      else

    return(Search(V, ptr-ri$ht));

    %

    1*

      A   1A

    NULL NULL

    NULLNULL

    *NULLNULL

      1+   1

    NULL

    poot

    !oid 'ain(!oid)

    {

      bool bnd;

      bnd = Search(,p+oot);

    %

    ptr)

  • 8/9/2019 Lecture12 Updated

    34/52

    ecursive BST SearchLets search for 1+(

     bool Search(int V, Node *ptr)

    {

    if (ptr == NULL)

    return(false); // nope

      else if (V == ptr-!alue)  return(true); // found"""

      else if (V # ptr-!alue)

      return(Search(V, ptr-left));

      else

    return(Search(V, ptr-ri$ht));

    %

    1*

      A   1A

    NULL NULL

    NULLNULL

    *NULLNULL

      1+   1

    NULL

    poot

    !oid 'ain(!oid)

    {

      bool bnd;

      bnd = Search(,p+oot);

    %

    ptr)<

     bool Search(int V, Node *ptr){

    if (ptr == NULL)

    return(false); // nope

      else if (V == ptr-!alue)

      return(true); // found"""

      else if (V # ptr-!alue)  return(Search(V, ptr-left));

      else

    return(Search(V, ptr-ri$ht));

    %

    ptr)<

      true

    true

    true

  • 8/9/2019 Lecture12 Updated

    35/52

    ecursive BST SearchLets search for 1+(

     bool Search(int V, Node *ptr)

    {

    if (ptr == NULL)

    return(false); // nope

      else if (V == ptr-!alue)  return(true); // found"""

      else if (V # ptr-!alue)

      return(Search(V, ptr-left));

      else

    return(Search(V, ptr-ri$ht));

    %

    1*

      A   1A

    NULL NULL

    NULLNULL

    *NULLNULL

      1+   1

    NULL

    poot

    !oid 'ain(!oid)

    {

      bool bnd;

      bnd = Search(,p+oot);

    %

    ptr)<

      true  true

    true

    true

    true

    h f h

  • 8/9/2019 Lecture12 Updated

    36/52

    Big Oh of BST SearchHuestion-

    In the average BST ith 4 values&ho many steps are re?uire" to

    fin" our valueG

    Huestion-In the orst case BST ith4 values& ho many steps are

    re?uire" fin" our valueG

    Huestion-If there are + .illion no"es in a BST& ho

    many steps ill it taKe to perform a searchG %O%,4o that's IN,

    FQ eliminate",FQeliminate",

    FQeliminate",

    FQ

    eliminate",

    ight, log2346 steps

    ight, 4 steps

    Just *2,

    I ti 4 l I t BST

  • 8/9/2019 Lecture12 Updated

    37/52

    Inserting 4e alue Into BST

    To insert a ne no"e in our BST& e must place the

    ne no"e so that the resulting tree is still a vali"BST ,

    %here oul" the folloingne values goG

    @arly

    Carly

    Pen

      Ken

    lice

    Alice

    I ti 4 l I t BST

  • 8/9/2019 Lecture12 Updated

    38/52

    Inserting 4e alue Into BST

    If the tree is empty  llocate a ne no"e an" put into it  oint the root pointer to our ne no"e( $O4E,

    Input- value  to insert

    Start at the root of the tree

    %hile e're not "one

    If is greater than current no"e's value  If there is a right chil"& then go right  ELSE allocate a ne no"e an" put into it&

      set current no"e's right pointer to ne no"e( $O4E,

      If is e?ual to current no"e's value& $O4E, 3nothing to "o(((6

    If is less than current no"e's value  If there is a left chil"& then go left  ELSE allocate a ne no"e an" put into it& an"  set current no"e's left pointer to ne no"e( $O4E,

    4 h @ @ " ,

    J t ith l

    n" here's our Binary Search

    void insert(const std::string &value)

  • 8/9/2019 Lecture12 Updated

    39/52

    4o the @ @o"e,

    struct 4o"e7 

    st"--string value:  4o"e 5left&5right:=:

    Just as ith a regular.inary tree& e use a no"estruct to hol" our items(>oever let's a"" a constructor to

    our 4o"e so e can easily create ane one,

     4o"e3const st"--string Rmyal6  7  value 8 myal:  left 8 right 8 4ULL:

      =

    class BinarySearchTree7pu.lic-

    BinarySearchTree36  7  mroot 8 4ULL:

      =  voi" insert3const st"--string Rvalue6  7    =

    private-

      4o"e 5mroot:=:

    Our BST class has a single

    mem.er varia.le the rootpointer to the tree(

    n" our constructor initialies thatroot pointer to 4ULL

    hen e create a ne tree(3This in"icates the tree is empty6

    n" here s our Binary SearchTree class(

    4o let's see our complete

    insertion function in @(

    void insert(const std string &value) {

      if (mroot !! NULL) { mroot ! ne" Node(value)# return# $

      Node %cur ! mroot#  for (##)  {

      if (value !! cur'value) return#

    if (value cur'value) {

      if (cur'left ! NULL)cur ! cur'left#

      else  {  cur'left ! ne" Node(value)#  return#  $  $

      else if (value ' cur'value)  {

      if (cur'rig*t ! NULL)cur ! cur'rig*t#

      else {

      cur'rig*t ! ne" Node(value)#  return#  $  $  $

     $

    If our tree isempty&

    allocate ane no"e an"point the

    root pointerto it then

    e're "one,

    Start traversing"on from theroot of the tree(

    for3::6 is thesame as an

    infinite loop(

    If our value isalrea"y in the

    tree& then e're"one ) Vust

    return(

    If the value toinsert is less

    than the current

    no"e's value&then go left(

    If there is ano"e to our left&a"vance to that

    no"e an"continue(

    Otherise e'vefoun" the

    proper spot forour ne value,

    "" our value as

    the left chil" ofthe current

    no"e(

    If the value e

    ant to insert isgreater than thecurrent no"e's

    value& thentraverse9insert

    to the right(

    void insert(const std::string &value)

  • 8/9/2019 Lecture12 Updated

    40/52

    ( g ) {

      if (mroot !! NULL) { mroot ! ne" Node(value)# return# $

      Node %cur ! mroot#  for (##)  {

      if (value !! cur'value) return#

    if (value cur'value) {

      if (cur'left ! NULL)cur ! cur'left#

      else  {  cur'left ! ne" Node(value)#  return#  $  $

      else if (value ' cur'value)  {

      if (cur'rig*t ! NULL)cur ! cur'rig*t#

      else {

      cur'rig*t ! ne" Node(value)#  return#  $  $  $

     $

    !oid 'ain(!oid)

    {

      inarSearch.ree bst;

      bstinsert(0Larr1);

     

      bstinsert(02hil1);

    %

    mroot 4ULL

    /Larry0NULL NULL

    void insert(const std::string &value)

  • 8/9/2019 Lecture12 Updated

    41/52

    ( g ) {

      if (mroot !! NULL) { mroot ! ne" Node(value)# return# $

      Node %cur ! mroot#  for (##)  {

      if (value !! cur'value) return#

    if (value cur'value) {

      if (cur'left ! NULL)cur ! cur'left#

      else  {

      cur'left ! ne" Node(value)#  return#  $  $

      else if (value ' cur'value)  {

      if (cur'rig*t ! NULL)cur ! cur'rig*t#

      else {

      cur'rig*t ! ne" Node(value)#  return#  $  $  $

     $

    !oid 'ain(!oid)

    {

      inarSearch.ree bst;

      bstinsert(0Larr1);

     

      bstinsert(02hil1);

    %

    mroot

    NULL

      /Larry0

      /!ran0   /on"a0

    NULL NULL

    NULL

      /Barry0

    hil 88 LarryGGhil ; LarryGG

    hil < LarryGG

    Ghil 88 on"aGGhil ; on"aGG

    G

    /hil0NULL NULL

    cur

    I i 4 l I BST

  • 8/9/2019 Lecture12 Updated

    42/52

    Inserting 4e alue Into BST

    s ith BST Search& there is a recursive version ofthe Insertion algorithm too( Be familiar ith it,

    Huestion-

    Miven a ran"om array of num.ers if you insert them oneat a time into a BST& hat ill the BST looK liKeG

    Huestion-

    Miven a or"ere" array of num.ers if you insert themone at a time into a BST& hat ill the BST looK liKeG

    Bi Oh f BST I ti

  • 8/9/2019 Lecture12 Updated

    43/52

    Big Oh of BST Insertion

    So& hat's the .ig)oh of BST InsertionGight, It's also O3log2n6

    %hyG Because e have to first use a .inary search to fin"

    here to insert our no"e an" .inary search is O3log2n6(

    Once e've foun" the right spot& e can insert our neno"e in O316 time(

    Mroovy Ba.y,

    !i "i Ni R N f BST

  • 8/9/2019 Lecture12 Updated

    44/52

    Huestion- %hat's the .ig)oh to fin" the minimum or

    maximum elementG

    NULL

      /Larry0

      /!ran0   /on"a0

    NULL NULL

    NULL

      /Barry0 /hil0NULL NULL

    !in"ing Nin R Nax of a BST >o "o e fin" the minimum an" maximum values in a BSTG

    int 3et4in(node *p+oot)

    {  if (p+oot == NULL)

      return(-); // e'pt

      &hile (p+oot-left "= NULL)

      p+oot = p+oot-left;

      return(p+oot-!alue);

    %

    int 3et4a5(node *p+oot)

    {  if (p+oot == NULL)

      return(-); // e'pt

      &hile (p+oot-ri$ht "= NULL)

      p+oot = p+oot-ri$ht;

      return(p+oot-!alue);

    %

     The minimum value is locate" at the left)most no"e( The maximum value is locate" at the right)most no"e(

    !i "i Ni R N f BST

  • 8/9/2019 Lecture12 Updated

    45/52

    >opefully you're getting the i"ea that most treefunctions can .e "one recursively

    !in"ing Nin R Nax of a BST n" here are recursive versions for you

    int 3et4in(node *p+oot)

    {

      if (p+oot == NULL)

      return(-); // e'pt

      if (p+oot-left == NULL)

      return(p+oot-!alue);

      return(3et4in(p+oot-left));

    %

    int 3et4a5(node *p+oot)

    {

      if (p+oot == NULL)

      return(-); // e'pt

      if (p+oot-ri$ht == NULL)

      return(p+oot-!alue);

      return(3et4a5(p+oot-ri$ht));

    %

    i i BST I l h . i l O "

  • 8/9/2019 Lecture12 Updated

    46/52

    voi" InOr"er34o"e 5cur67

      if 3cur 88 4ULL6 99 if empty& return  return:

      InOr"er3cur)

  • 8/9/2019 Lecture12 Updated

    47/52

    !reeing The %hole Tree

    %hen e are "one ith our BST& e have to free every

    no"e in the tree& one at a time(

    Huestion- @an anyone thinK of an algorithm for thisG

    voi" !reeTree34o"e 5cur67  if 3cur 88 4ULL6 99 if empty& return  return:

      !reeTree3cur)

  • 8/9/2019 Lecture12 Updated

    48/52

    !reeing The %hole Tree

    voi" !reeTree34o"e 5cur67  if 3cur 88 4ULL6

    return:  !reeTree3cur)

  • 8/9/2019 Lecture12 Updated

    49/52

    ! i Th %h l T

  • 8/9/2019 Lecture12 Updated

    50/52

    !reeing The %hole Tree

    NULL

      /Larry0

      /!ran0   /on"a0NULL

    /Ma..y0NULL NULL

    cur)<

    cur)<

    voi" !reeTree34o"e 5cur67  if 3cur 88 4ULL6

    return:  !reeTree3cur)

  • 8/9/2019 Lecture12 Updated

    51/52

    !reeing The %hole Tree

    NULL

      /Larry0

      /!ran0   /on"a0NULL

    cur)<

    cur)<

    voi" !reeTree34o"e 5cur67  if 3cur 88 4ULL6

    return:  !reeTree3cur)

  • 8/9/2019 Lecture12 Updated

    52/52

    ppen"ix Sli"es