Você está na página 1de 3

Binary

   Trees  (continued)  
 
Exercise:  Write  the  pre-­‐order,  in-­‐order,  post-­‐order  traversal  of  the  following  binary  
tree:  
 

 
 
 
 
 
 
 
Pre-­order:     P  F  BA  HM  SRW  
 
In-­order:   AB  F  HM  P  RSW  
 
Post-­order:   AB  MH  F  RWS  P  
 
 
Binary  Tree  -­  Ancestors  &  Descendants:  
 
Let  X  and  Y  be  two  nodes  of  a  tree.  
 
Definition:    X  is  an  ancestor  of  Y  if  
     X  is  a  parent  of  Y,  or  
     X  is  the  ancestor  of  the  parent  of  Y  
 
Definition:  X  is  descendant  of  Y  if    
     X  is  a  child  of  Y,  or  
     X  is  a  descendant  of  a  child  of  Y  
 
Example:    
P  and  F  are  ancestors  of  B.    
B,  A,  H,  and  M  are  descendants  of  F  
   
 
 
Binary  Trees  –  Depth    
 
Definition:  The  depth  of  a  node  is  the  number  of  links  in  the  path  from  the  root  to  a  
given  node.  
 
What  is  a  recursive  definition  of  height?  
 
 
Recursively:    
     Depth  of  node  Y  is    
    0,  if  Y  is  root   (base  case)  
    1  +  depth  of  parent  of  Y,  otherwise   (recursive  case)  
 
 
Binary  Trees  –  Height  
 
Definition:  The  height  of  a  node  is  the  number  of  links  in  the  longest  path  from  
that  node  to  a  leaf  node.  
 
Definition:  The  height  of  a  binary  tree  T  is  the  height  of  the  root  node.  
 

 
 
What  is  the  maximum  height  of  a  tree?    
 
  n-­‐1      
 
The  tree  is  like  a  linked  list,  where  for  every  node,  only  one  of  left  or  right  has  a  
subtree.  How  different  trees  have  height  n-­‐1?  
 
What  is  the  minimum  height  of  a  tree?    
 
  floor(log(n))  
 
Every  node  has  two  children,  except  at  the  bottom  two  levels  of  the  tree.  
 
What  is  a  recursive  definition  of  height?  
Height  recursively:  
           Height  of  node  Y  is  
        0,  if  Y  is  leaf   (Base  case)  
        1  +  max  (height  of  left  subtree  of  Y,     (Recursive  case)    
  height  of  right  subtrees  of    Y),    otherwise  
 
//  precondition:  node  !=  null  
public int height(BTNode<E> node) {
if (node.left == null && node.right == null) {
return 0;
}
if (node.left == null) {
return 1 + height(node.right);
}
if (node.right == null) {
return 1 + height(node.left);
}

int left = height(node.left);


int right = height(node.right);
if (left > right) return left + 1;
else return right + 1
}

Because the height of a leaf node is 0, it is convenient to define the height of an empty
tree to be -1. Why?

public int height(BTNode<E> node) {


if (node == null) return -1;

int left = height(node.left);


int right = height(node.right);
if (left > right) {
return left + 1;
{
else {
return right + 1;
}
}
 
Write your methods with as few tests as you can.  
 
What  is  the  runtime  of  the  height  method  on  a  tree  with  n  nodes?  
 
Runtime:  O(n)  
It  visits  each  node  once  and  does  a  constant  amount  of  work  for  each  node.  

Você também pode gostar