nextupprevious
Next:4.3 An Application of Binary Trees: Huffman Code ConstructionUp:4. Binary TreesPrevious:4.1.4 Data Structures for Tree Representation

4.2 Binary Trees

Definition: A binary tree is either empty or consists of a node called the root together with two binary trees called the left subtree and the right subtree.
Figure 4.6: Examples of complete, incomplete binary trees
\begin{figure}\centerline{\psfig{figure=figures/Fcompbintree.ps,width=15cm}}\end{figure}

Binary Tree Traversal:

1.
Preorder


Visit root, visit left subtree in preorder, visit right subtree in preorder

2.
Postorder


Visit left subtree in postorder, right subtree in postorder, then the root

3.
Inorder


Visit left subtree in inorder, then the root, then the right subtree in inorder

Figure 4.7: Binary tree traversals
\begin{figure}\centerline{\psfig{figure=figures/Ftraversals.ps}}\end{figure}

Data Structures for Binary Trees:

1.
Arrays, especially suited for complete and full binary trees
2.
Pointer-based
Pointer-based Implementation:


typedef struct node-tag {

            item-type info ;

         struct node-tag * left ;

         struct node-tag * right ;

    } node-type ;

node-type * root ; /* pointer to root */

node-type * p ; /* temporary pointer */

void preorder(node-type * root)

    {

            if (root) {

            visit (root) ;

        preorder (root $ \rightarrow$ left) ;

        preorder (root $ \rightarrow$ right) ;

    }

}

void inorder (node-type * root)

{

            if (root) {

            inorder (root $ \rightarrow$ left) ;

                visit (root) ;

            inorder (root $ \rightarrow$ right) ;

       }
}

void postorder (node-type * root)

{

        if (root) {

                postorder (root $ \rightarrow$ left) ;

                postorder (root $ \rightarrow$ right) ;

                        visit (root) ;

        }

}
 


nextupprevious
Next:4.3 An Application of Binary Trees: Huffman Code ConstructionUp:4. Binary TreesPrevious:4.1.4 Data Structures for Tree Representation
eEL,CSA_Dept,IISc,Bangalore