树遍历和非递归

利用随机函数产生80(不大于200且各不相同的)随机整数,用这些整数来生成一棵二树,分别对二叉树进行先序遍历,中序遍历和后序列遍历输出树中结点元素序列。注意:先序遍历输出要求采用非递归来实现。

2)程序实现的基本思想

1.建立合适的二叉树

程序是以

1.1

的形式建立的。

2.前序遍历是以stack栈和指针左右子女实现的。

3.前序遍历,中序遍历,后序遍历分别用了递归实现。

4.如建立二叉树,其中随机产生的数值是(因为80个数比较多,所以就以#define Max_size 10,若要以80个数,可#define Max_size 10改成#define Max_size 80):

106   199   95   144   102   164   26   96   87   168

由以上数值可以建立出以下的树:

1.2

由图1.2可得出他们的前序,中序和后序。

前序序列:106   95   26   87   102   96   199   144   164   168

中序序列:26    87   95   96   102   106 144   164   168   199

后序序列:87    26   96   102 95    168 164   144   199   106

3)系统流程图

程序步骤:

#include "stdio.h"

#include "conio.h"

#define Max_size 10    /*声明要产生的不同的随机数*/

struct tree    /*声明树的结构*/

{

struct tree *left;

int data;

struct tree *right;

};

typedef struct tree treenode;

typedef treenode *b_tree;        /*声明二叉树链表*/

/*插入二叉树的节点*/

b_tree insert_node(b_tree root,int node)

{

b_tree newnode;

b_tree currentnode;

b_tree parentnode;

newnode=(b_tree)malloc(sizeof(treenode));    /*建立新节点的的内存空间*/

newnode->data=node;

newnode->right=NULL;

newnode->left=NULL;

if(root==NULL)

    return newnode;

else

    {

    currentnode=root;

    while(currentnode!=NULL)

    {    parentnode=currentnode;

        if(currentnode->data>node)           /*值小于上一根节点的就插入到左孩子*/

            currentnode=currentnode->left;

        else    currentnode=currentnode->right;

    }

    if(parentnode->data>node)                /*值大于上一根节点的就插入到右孩子*/

        parentnode->left=newnode;

    else    parentnode->right=newnode;

    }

    return root;

}

/*建立二叉树*/

b_tree create_btree(int *data,int len)

{

    b_tree root=NULL;

    int i;

    for(i=0;i<len;i++)

        root=insert_node(root,data[i]);

    return root;

}

 void preorder_nonrecursive(b_tree root)

{

   b_tree stack[Max_size];

   b_tree p;

    int top=-1;

    int i;

    if (root != NULL)

    {

        top++;                        /* 根节点入栈 */

        stack[top] = root;

        while (top > -1)              /* 栈不空时循环 */

        {

            p = stack[top];           /* 出栈并访问该节点 */

            top--;

            printf("%d\t", p->data);

            if (p->right != NULL)     /* 右孩子入栈 */

            {

                top++;

                stack[top] = p->right;

            }

            if (p->left != NULL)     /* 左孩子入栈 */

            {

                top++;

                stack[top] = p->left;

            }

        }

    }

 }

/*二叉树前序遍历--递归*/

void preOrder(b_tree point)

{

    if(point!=NULL)

    {printf("%d\t",point->data);

    preOrder(point->left);

    preOrder(point->right);

    }

}

/*二叉树中序遍历--递归*/

void inOrder(b_tree point)

{    if(point!=NULL)

    {

        inOrder(point->left);

        printf("%d\t",point->data);

        inOrder(point->right);

    }

}

/*二叉树后序遍历--递归*/

void postOrder(b_tree point)

{

if(point!=NULL)

{

    postOrder(point->left);

    postOrder(point->right);

    printf("%d\t",point->data);

    }

    return;

}

/*主函数*/

main()

{

    b_tree root=NULL;

    int i,j;

    int value;

    int nodelist[Max_size];

    printf("\nThe elements of binary tree:\n");

    /*读取数值存到数组中*/

     srand((unsigned)time( NULL ));

    for( i = 0; i <Max_size;i++ )

     {

       nodelist[i]=rand()%200+1;

      for(j=0;j<i;j++)

          if(nodelist[i]==nodelist[j])

           {

           nodelist[i]=rand()%200+1;

            j=0;

          }

          printf( "%d\t", nodelist[i]);

     }  

    /*建立二叉树*/

    root=create_btree(nodelist,Max_size);

    /*前序遍历二叉树--非递归*/

    printf("\n\nThe non-inorder traversal result is :\n");

    preorder_nonrecursive(root);

    /*前序遍历二叉树--递归*/

    printf("\nThe inorder traversal result is :\n");

    preOrder(root);

    /*中序遍历二叉树--递归*/

    printf("\nThe inorder traversal result is :\n");

    inOrder(root);

    /*后序遍历二叉树--递归*/

    printf("\nThe postOrder traversal result is :\n");

    postOrder(root);

    getch();

}

4)系统运行效果图

原文地址:https://www.cnblogs.com/yongfeng/p/1666927.html