user-0 提问: 二叉查找树的中序遍历

回复:
user: user-1
content: /* Fig. 12.19: fig12_19.c    Create a binary tree and traverse it     preorder, inorder, and postorder */ #include <stdio.h> #include <stdlib.h> #include <time.h>  /* self-referential structure */ struct treeNode {     struct treeNode *leftPtr; /* pointer to left subtree */    int data; /* node value */    struct treeNode *rightPtr; /* pointer to right subtree */ }; /* end structure treeNode */  typedef struct treeNode TreeNode; /* synonym for struct treeNode */ typedef TreeNode *TreeNodePtr; /* synonym for TreeNode* */  /* prototypes */ void insertNode( TreeNodePtr *treePtr, int value ); void inOrder( TreeNodePtr treePtr ); void preOrder( TreeNodePtr treePtr ); void postOrder( TreeNodePtr treePtr );  /* function main begins program execution */ int main( void ) {     int i; /* counter to loop from 1-10 */    int item; /* variable to hold random values */    TreeNodePtr rootPtr = NULL; /* tree initially empty */     srand( time( NULL ) );     printf( "The numbers being placed in the tree are:\n" );     /* insert random values between 0 and 14 in the tree */    for ( i = 1; i <= 10; i++ ) {        item = rand() % 15;       printf( "%3d", item );       insertNode( &rootPtr, item );    } /* end for */     /* traverse the tree preOrder */    printf( "\n\nThe preOrder traversal is:\n" );    preOrder( rootPtr );     /* traverse the tree inOrder */    printf( "\n\nThe inOrder traversal is:\n" );    inOrder( rootPtr );     /* traverse the tree postOrder */    printf( "\n\nThe postOrder traversal is:\n" );    postOrder( rootPtr );    return 0; /* indicates successful termination */ } /* end main */  /* insert node into tree */ void insertNode( TreeNodePtr *treePtr, int value ) {     /* if tree is empty */    if ( *treePtr == NULL ) {          *treePtr = (TreeNodePtr)malloc( sizeof( TreeNode ) );        /* if memory was allocated then assign data */       if ( *treePtr != NULL ) {           ( *treePtr )->data = value;          ( *treePtr )->leftPtr = NULL;          ( *treePtr )->rightPtr = NULL;       } /* end if */       else {          printf( "%d not inserted. No memory available.\n", value );       } /* end else */    } /* end if */    else { /* tree is not empty */       /* data to insert is less than data in current node */       if ( value < ( *treePtr )->data ) {          insertNode( &( ( *treePtr )->leftPtr ), value );       } /* end if */        /* data to insert is greater than data in current node */       else if ( value > ( *treePtr )->data ) {          insertNode( &( ( *treePtr )->rightPtr ), value );       } /* end else if */       else { /* duplicate data value ignored */          printf( "dup" );       } /* end else */    } /* end else */ } /* end function insertNode */  /* begin inorder traversal of tree */ void inOrder( TreeNodePtr treePtr ) {     /* if tree is not empty then traverse */    if ( treePtr != NULL ) {        inOrder( treePtr->leftPtr );       printf( "%3d", treePtr->data );       inOrder( treePtr->rightPtr );    } /* end if */ } /* end function inOrder */  /* begin preorder traversal of tree */ void preOrder( TreeNodePtr treePtr ) {     /* if tree is not empty then traverse */    if ( treePtr != NULL ) {        printf( "%3d", treePtr->data );       preOrder( treePtr->leftPtr );       preOrder( treePtr->rightPtr );    } /* end if */ } /* end function preOrder */  /* begin postorder traversal of tree */ void postOrder( TreeNodePtr treePtr ) {     /* if tree is not empty then traverse */    if ( treePtr != NULL ) {        postOrder( treePtr->leftPtr );       postOrder( treePtr->rightPtr );       printf( "%3d", treePtr->data );    } /* end if */ } /* end function postOrder */    /**************************************************************************  * (C) Copyright 1992-2010 by Deitel & Associates, Inc. and               *  * Pearson Education, Inc. All Rights Reserved.                           *  *                                                                        *  * DISCLAIMER: The authors and publisher of this book have used their     *  * best efforts in preparing the book. These efforts include the          *  * development, research, and testing of the theories and programs        *  * to determine their effectiveness. The authors and publisher make       *  * no warranty of any kind, expressed or implied, with regard to these    *  * programs or to the documentation contained in these books. The authors *  * and publisher shall not be liable in any event for incidental or       *  * consequential damages in connection with, or arising out of, the       *  * furnishing, performance, or use of these programs.                     *  *************************************************************************/

user: user-1
content: /* Fig. 12.19: fig12_19.c    Create a binary tree and traverse it     preorder, inorder, and postorder */ #include <stdio.h> #include <stdlib.h> #include <time.h>  /* self-referential structure */ struct treeNode {     struct treeNode *leftPtr; /* pointer to left subtree */    int data; /* node value */    struct treeNode *rightPtr; /* pointer to right subtree */ }; /* end structure treeNode */  typedef struct treeNode TreeNode; /* synonym for struct treeNode */ typedef TreeNode *TreeNodePtr; /* synonym for TreeNode* */  /* prototypes */ void insertNode( TreeNodePtr *treePtr, int value ); void inOrder( TreeNodePtr treePtr ); void preOrder( TreeNodePtr treePtr ); void postOrder( TreeNodePtr treePtr );  /* function main begins program execution */ int main( void ) {     int i; /* counter to loop from 1-10 */    int item; /* variable to hold random values */    TreeNodePtr rootPtr = NULL; /* tree initially empty */     srand( time( NULL ) );     printf( "The numbers being placed in the tree are:\n" );     /* insert random values between 0 and 14 in the tree */    for ( i = 1; i <= 10; i++ ) {        item = rand() % 15;       printf( "%3d", item );       insertNode( &rootPtr, item );    } /* end for */     /* traverse the tree preOrder */    printf( "\n\nThe preOrder traversal is:\n" );    preOrder( rootPtr );     /* traverse the tree inOrder */    printf( "\n\nThe inOrder traversal is:\n" );    inOrder( rootPtr );     /* traverse the tree postOrder */    printf( "\n\nThe postOrder traversal is:\n" );    postOrder( rootPtr );    return 0; /* indicates successful termination */ } /* end main */  /* insert node into tree */ void insertNode( TreeNodePtr *treePtr, int value ) {     /* if tree is empty */    if ( *treePtr == NULL ) {          *treePtr = (TreeNodePtr)malloc( sizeof( TreeNode ) );        /* if memory was allocated then assign data */       if ( *treePtr != NULL ) {           ( *treePtr )->data = value;          ( *treePtr )->leftPtr = NULL;          ( *treePtr )->rightPtr = NULL;       } /* end if */       else {          printf( "%d not inserted. No memory available.\n", value );       } /* end else */    } /* end if */    else { /* tree is not empty */       /* data to insert is less than data in current node */       if ( value < ( *treePtr )->data ) {          insertNode( &( ( *treePtr )->leftPtr ), value );       } /* end if */        /* data to insert is greater than data in current node */       else if ( value > ( *treePtr )->data ) {          insertNode( &( ( *treePtr )->rightPtr ), value );       } /* end else if */       else { /* duplicate data value ignored */          printf( "dup" );       } /* end else */    } /* end else */ } /* end function insertNode */  /* begin inorder traversal of tree */ void inOrder( TreeNodePtr treePtr ) {     /* if tree is not empty then traverse */    if ( treePtr != NULL ) {        inOrder( treePtr->leftPtr );       printf( "%3d", treePtr->data );       inOrder( treePtr->rightPtr );    } /* end if */ } /* end function inOrder */  /* begin preorder traversal of tree */ void preOrder( TreeNodePtr treePtr ) {     /* if tree is not empty then traverse */    if ( treePtr != NULL ) {        printf( "%3d", treePtr->data );       preOrder( treePtr->leftPtr );       preOrder( treePtr->rightPtr );    } /* end if */ } /* end function preOrder */  /* begin postorder traversal of tree */ void postOrder( TreeNodePtr treePtr ) {     /* if tree is not empty then traverse */    if ( treePtr != NULL ) {        postOrder( treePtr->leftPtr );       postOrder( treePtr->rightPtr );       printf( "%3d", treePtr->data );    } /* end if */ } /* end function postOrder */    /**************************************************************************  * (C) Copyright 1992-2010 by Deitel & Associates, Inc. and               *  * Pearson Education, Inc. All Rights Reserved.                           *  *                                                                        *  * DISCLAIMER: The authors and publisher of this book have used their     *  * best efforts in preparing the book. These efforts include the          *  * development, research, and testing of the theories and programs        *  * to determine their effectiveness. The authors and publisher make       *  * no warranty of any kind, expressed or implied, with regard to these    *  * programs or to the documentation contained in these books. The authors *  * and publisher shall not be liable in any event for incidental or       *  * consequential damages in connection with, or arising out of, the       *  * furnishing, performance, or use of these programs.                     *  *************************************************************************/ 这是《C语言大学教程》第六版中第374页的关于二叉树遍历的程序（递归实现），可供参考。

user: user-2
content: 引用 2 楼 my_live_123 的回复:可以递归写就消失一大段代码+1...

user: user-3
content: 可以递归写就消失一大段代码

user: user-0
content: 在代码中有一个break 可不可以优化代码使得break消失