CMPE242 DATA STRUCTURES AND DATA ORGANIZATION

2 Yorum

 

Labwork 11

Consider the following tree node structure:

 

struct  node {

            int data;

            struct node * left;

            struct node * right;

};

Implement the following basic functions of the tree structure:

 

  1. Initialization

struct node *tree=NULL;

 

  1. Check Empty Tree

 

int is_empty(struct mode *tree)

{

            if(tree==NULL)

return 1;

else

return 0;

}

 

  1. Create a new node

 

struct node * newNode(int d)

{

            struct node *temp;

            temp=(struct node *) malloc(sizeof(node));

            temp->data=d;

            temp->left=NULL;

            temp->right=NULL;

            return temp;

}

 

 

  1. Traverse
    1. Show elements of tree in inorder traverse

 

void inorderTraverse(struct node *tree)

{

            if(tree==NULL)

                        return;

            inorderTraverse(tree->left);

            cout<<tree->data<<" ";

            inorderTraverse(tree->right);

}

 

    1. Show elements of tree in preorder traverse

 

void preorderTraverse(struct node * tree)

{

            if(tree==NULL)

                        return;

            cout<<tree->data<<" ";

            preorderTraverse(tree->left);

            preorderTraverse(tree->right);

}

 

    1. Show elements of tree in postorder traverse

 

void postorderTraverse(struct node * tree)

{

            if(tree==NULL)

                        return;

            postorderTraverse(tree->left);

            postorderTraverse(tree->right);

            cout<<tree->data<<" ";

}

 

Yorumlar (Toplam: 2)

Sıralama:
Hangi konu ile ilgili ? Mr. Kaan
B. Kaan
örnek sorunuz var mı ?

Yorum Yaz

Onay Kodu .