程序输入一个字符串(只包含小写字母),请按照字符的输入顺序建立平衡二叉排序树,并分别输出二叉树的先序序列、中序序列和后序序列,最后输出该二叉树向左旋转 90 度后的结构。
例如:向左旋转 90 度后,以每层向里缩进 4 个空格的方式输出,输出结果为:
i
g
f
a
d
c
b
输入:agxnzyimk
输出:
Preorder: xigamknzy
Inorder: agikmnxyz
Postorder: agknmiyzx
Tree:
z
y
x
n
m
k
i
g
a
| 测试输入 | 期待的输出 | 时间限制 | 内存限制 | 额外进程 | |
|---|---|---|---|---|---|
| 测试用例 1 | 以文本方式显示
| 以文本方式显示
| 1秒 | 64M | 0 |
| 测试用例 2 | 以文本方式显示
| 以文本方式显示
| 1秒 | 64M | 0 |
代码如下(上个忘了说了,以后就尽量都用C++写了)
C++写着确实输入,当然树这块一般,至少二叉树一般,最大的问题是每次交乐学都忘换编译语言。
#include <iostream>
#include <cstdlib>
#include <cstdio>
using namespace std;
struct AVLnode
{
char data;
AVLnode *left;
AVLnode *right;
};
AVLnode *root = NULL;
int depth(AVLnode *root)
{
if (root == NULL)
return 0;
else
{
if (depth(root->left) > depth(root->right))
return depth(root->left) + 1;
else
return depth(root->right) + 1;
}
}
AVLnode *LL(AVLnode *root)
{
AVLnode *son = root->left;
root->left = son->right;
son->right = root;
return son;
}
AVLnode *RR(AVLnode *root)
{
AVLnode *son = root->right;
root->right = son->left;
son->left = root;
return son;
}
AVLnode *RL(AVLnode *root)
{
AVLnode *son = root->right;
root->right = LL(son);
return RR(root);
// 嵌套
// AVLnode *son = root->right, *r = son->left;
// son->left = r->right;
// r->right = son;
// root->right = r->left;
// r->left = root;
// return r;
}
AVLnode *LR(AVLnode *root)
{
AVLnode *son = root->left;
root->left = RR(son);
return LL(root);
// AVLnode *son = root->left, *r = son->right;
// son->right = r->left;
// r->left = son;
// root->left = r->right;
// r->right = root;
// return r;
}
AVLnode *Initialize(char Data)
{
AVLnode *Node = (AVLnode *)malloc(sizeof(AVLnode));
Node->data = Data;
Node->left = NULL;
Node->right = NULL;
return Node;
}
AVLnode *insertNode(AVLnode *root, char Data)
{
if (root == NULL)
{
root = Initialize(Data);
return root;
}
else
{
if (Data < root->data)
{
root->left = insertNode(root->left, Data);
if (depth(root->left) - depth(root->right) > 1)
{
if (Data < root->left->data)
root = LL(root);
else
root = LR(root);
}
}
else
{
root->right = insertNode(root->right, Data);
if (depth(root->right) - depth(root->left) > 1)
{
if (Data < root->right->data)
root = RL(root);
else
root = RR(root);
}
}
}
return root;
}
void printPreOrder(AVLnode *root)
{
if (root != NULL)
{
cout << root->data;
printPreOrder(root->left);
printPreOrder(root->right);
}
}
void printInOrder(AVLnode *root)
{
if (root != NULL)
{
printInOrder(root->left);
cout << root->data;
printInOrder(root->right);
}
}
void printPostOrder(AVLnode *root)
{
if (root != NULL)
{
printPostOrder(root->left);
printPostOrder(root->right);
cout << root->data;
}
}
void AVLprint(AVLnode *root, int space)
{
if (root->right)
AVLprint(root->right, space + 4);
for (int i = 0; i < space; i++)
cout << " ";
cout << root->data << endl;
if (root->left)
AVLprint(root->left, space + 4);
}
int main()
{
string input;
getline(cin, input);
for (char c : input)
{
if (c == '\n')
break;
root = insertNode(root, c);
}
cout << "Preorder: ";
printPreOrder(root);
cout << "\nInorder: ";
printInOrder(root);
cout << "\nPostorder: ";
printPostOrder(root);
cout << "\nTree:\n";
AVLprint(root, 0);
return 0;
}
1464

被折叠的 条评论
为什么被折叠?



