next up previous contents
Next: Binary Search Trees: ADT Up: Trees: ADT Previous: Tree Traversals: tree   Contents

Binary Trees: ADT [55]




\begin{picture}(268,188)(26,646)
\thicklines\put(160,820){\circle{28}}
\put( 40,...
...05,810){\makebox(0,0)[lb]{\raisebox{0pt}[0pt][0pt]{\twlrm right}}}
\end{picture}



Each node has 0, 1, or 2 children.

Applications: many including expression trees.



Binary Trees: binary [56]

% gcc -o binary binary.c                               > tcc binary.c
% binary
> h
h(elp; q(uit; r(oot x; i(nsert [l|r]+ x (ex: i llr x); P(reorder; p(ostorder; 
I(norder; e(xpression s; H(eight

Data File with Test Case: binary.dat

r h
i l e, i r o, i ll l, i lr l, i lrl o, i lrr w, i rl r, i rlr l, i rr d
P I p H



Data File with Test Case for Expression Trees: binary1.dat

r +
i l +, i r *, i ll a, i lr *, i lrl b, i lrr c, i rl +, i rlr f, i rr g
i rll *, i rlll d, i rllr e
I p H C
e abc*+de*f+g*+
p



Binary Trees: binary.dat [57]

Preorder: print, visit left and right Inorder: visit left, print, visit right   
depth pointer   left  right  element    depth pointer   left  right  element   
    0  153016 153040 153064  h              2  153088      0      0      l   
    1  153040 153088 153112    e            1  153040 153088 153112    e   
    2  153088      0      0      l          3  153136      0      0        o   
    2  153112 153136 153160      l          2  153112 153136 153160      l   
    3  153136      0      0        o        3  153160      0      0        w   
    3  153160      0      0        w        0  153016 153040 153064  h   
    1  153064 153184 153232    o            2  153184      0 153208      r   
    2  153184      0 153208      r          3  153208      0      0        l   
    3  153208      0      0        l        1  153064 153184 153232    o   
    2  153232      0      0      d          2  153232      0      0      d   

postorder: visit left and right, print
depth pointer   left  right  element
    2  153072      0      0      l
    3  153120      0      0        o
    3  153144      0      0        w
    2  153096 153120 153144      l
    1  153024 153072 153096    e
    3  153192      0      0        l
    2  153168      0 153192      r
    2  153216      0      0      d
    1  153048 153168 153216    o
    0  153000 153024 153048  h



Binary Trees: Height [58]

int Height(BINARY_TREE T) {
int h, max;
  if (T == NULL)
    return -1;
  else {
    h = Height(T->left);
    max = Height(T->right);
    if (h > max) max = h;
    return(max+1);
  }
}

Definition: A leaf has a height of zero.

Height(l)=0; Height(o)=0; Height(w)=0; Height(l)=1;

Height(e)=2; Height(l)=0; Height(r)=1; Height(d)=0;

Height(o)=2; Height(h)=3;



Binary Trees: Expression Trees [59]




\begin{picture}(268,248)(26,586)
\thicklines\put(160,820){\circle{28}}
\put( 40,...
...ut(210,720){\makebox(0,0)[lb]{\raisebox{0pt}[0pt][0pt]{\twlrm +}}}
\end{picture}



Expression: (a+b*c)+((d*e+f)*g)

Inorder Traversal: a + b * c + d * e + f * g

Postorder Traversal: a b c * + d e * f + g * +

Preorder Traversal:

% binary binary1.dat



Binary Trees: binary1.dat [60]

Inorder: visit left, print, visit right  postorder: visit left and right, print   
depth pointer   left  right  element     depth pointer   left  right  element   
    2  153088      0      0      a           2  153088      0      0      a   
    1  153040 153088 153112    +             3  153136      0      0        b   
    3  153136      0      0        b         3  153160      0      0        c   
    2  153112 153136 153160      *           2  153112 153136 153160      *   
    3  153160      0      0        c         1  153040 153088 153112    +   
    0  153016 153040 153064  +               4  153280      0      0          d   
    4  153280      0      0          d       4  153304      0      0          e   
    3  153256 153280 153304        *         3  153256 153280 153304        *   
    4  153304      0      0          e       3  153208      0      0        f   
    2  153184 153256 153208      +           2  153184 153256 153208      +   
    3  153208      0      0        f         2  153232      0      0      g   
    1  153064 153184 153232    *             1  153064 153184 153232    *   
    2  153232      0      0      g           0  153016 153040 153064  +



Binary Trees: binary.c [61]

typedef struct tree_node *tree_ptr;
struct tree_node {
  element_type element;
  tree_ptr left;
  tree_ptr right;
};
typedef tree_ptr BINARY_TREE;
void preorder(BINARY_TREE T, int depth) { 
if (T != NULL) {
    show_node(T,depth);
    preorder(T->left,depth+1);
    preorder(T->right,depth+1);
} }
void postorder(BINARY_TREE T, int depth) {  
  if (T != NULL) {
    postorder(T->left,depth+1);
    postorder(T->right,depth+1);
    show_node(T,depth);
} }
void inorder(BINARY_TREE T, int depth) {  
  if (T != NULL) {
    inorder(T->left,depth+1);
    show_node(T,depth);
    inorder(T->right,depth+1);
}  }



Construct Expression Tree [62]

Solution program: binary1 binary1.dat

> e ab+cde+**

operand : create node, push 

operator: create node, node->right=pop, node->left = pop, push




\begin{picture}(653,374)(7,446)
\thicklines\put( 20,800){\framebox (200,20){}}
\...
...ut(615,455){\makebox(0,0)[lb]{\raisebox{0pt}[0pt][0pt]{\twlrm e}}}
\end{picture}


next up previous contents
Next: Binary Search Trees: ADT Up: Trees: ADT Previous: Tree Traversals: tree   Contents
Ted Billard 2001-10-25