while ( !done ) { switch( GetOp() ) { case push: scanf(\ if (!Push(S, X, Tag)) printf(\ break; case pop: scanf(\ X = Pop(S, Tag); if ( X==ERROR ) printf(\ break; case end: PrintStack(S, 1); PrintStack(S, 2); done = 1; break; } } return 0; } /* 你的代码将被嵌在这里 */ 输入样例:
5 Push 1 1 Pop 2 Push 2 11 Push 1 2 Push 2 12 Pop 1 Push 2 13 Push 2 14 Push 1 3 Pop 2 End 输出样例:
Stack 2 Empty Stack 2 is Empty! Stack Full Stack 1 is Full! Pop from Stack 1: 1 Pop from Stack 2: 13 12 11
4-9 二叉树的遍历 (25分) 本题要求给定二叉树的4种遍历。 函数接口定义:
void InorderTraversal( BinTree BT ); void PreorderTraversal( BinTree BT ); void PostorderTraversal( BinTree BT ); void LevelorderTraversal( BinTree BT ); 其中BinTree结构定义如下: typedef struct TNode *Position; typedef Position BinTree; struct TNode{ ElementType Data; BinTree Left; BinTree Right; }; 要求4个函数分别按照访问顺序打印出结点的内容,格式为一个空格跟着一个字符。
裁判测试程序样例:
#include
Inorder: D B E F A G H C I Preorder: A B D F E C G H I Postorder: D E F B H G I C A Levelorder: A B C D F G I E H
4-12 二叉搜索树的操作集 (30分)
本题要求实现给定二叉搜索树的5种常用操作。 函数接口定义:
BinTree Insert( BinTree BST, ElementType X ); BinTree Delete( BinTree BST, ElementType X ); Position Find( BinTree BST, ElementType X ); Position FindMin( BinTree BST ); Position FindMax( BinTree BST ); 其中BinTree结构定义如下: typedef struct TNode *Position; typedef Position BinTree; struct TNode{ ElementType Data; BinTree Left; BinTree Right; }; ? ? ? ? ? 函数Insert将X插入二叉搜索树BST并返回结果树的根结点指针; 函数Delete将X从二叉搜索树BST中删除,并返回结果树的根结点指针;如果X不在树中,则打印一行Not Found并返回原树的根结点指针; 函数Find在二叉搜索树BST中找到X,返回该结点的指针;如果找不到则返回空指针; 函数FindMin返回二叉搜索树BST中最小元结点的指针; 函数FindMax返回二叉搜索树BST中最大元结点的指针。 裁判测试程序样例:
#include 10 5 8 6 2 4 1 0 10 9 7 5 6 3 10 0 5 5 5 7 0 10 3 输出样例: Preorder: 5 2 1 0 4 8 6 7 10 9 6 is found 3 is not found 10 is found 10 is the largest key 0 is found 0 is the smallest key 5 is found Not Found Inorder: 1 2 4 6 8 9