|
|
书中的以前序,中序和后序搜索二叉树的代码如下。
其中前序和后序的调用都是BST_Inorder_Search(),这不就没有用递归了么?遍历到后面都是用了中序的算法。
不知道大家有没有发现这个问题?而且书中写的代码和光盘上附带的源文件都是这样的,这让我感觉不像是一个印刷错误。。。。。。
- //////////////////////////////////////////////////////////////////////////
- void BST_Inorder_Search(TNODE_PTR root)
- {
- // this searches a BST using the inorder search
- // test for NULL
- if (!root)
- return;
- // traverse left tree
- BST_Inorder_Search(root->left);
- // visit the node
- printf("\nname: %s, age: %d", root->name, root->age);
- // traverse the right tree
- BST_Inorder_Search(root->right);
- } // end BST_Inorder_Search
- ///////////////////////////////////////////////////////////////////
- void BST_Preorder_Search(TNODE_PTR root)
- {
- // this searches a BST using the preorder search
- // test for NULL
- if (!root)
- return;
- // visit the node
- printf("\nname: %s, age: %d", root->name, root->age);
- // traverse left tree
- BST_Inorder_Search(root->left);
- // traverse the right tree
- BST_Inorder_Search(root->right);
- } // end BST_Preorder_Search
- ////////////////////////////////////////////////////////////////
- void BST_Postorder_Search(TNODE_PTR root)
- {
- // this searches a BST using the postorder search
- // test for NULL
- if (!root)
- return;
- // traverse left tree
- BST_Inorder_Search(root->left);
- // traverse the right tree
- BST_Inorder_Search(root->right);
- // visit the node
- printf("\nname: %s, age: %d", root->name, root->age);
- } // end BST_Postorder_Search
复制代码 |
|