这是输入=》 (1,L) (2,R) (3,LL) (4,RR) (5,LR) (6,RL) (7,) ()
然后从上到下,从左至右,遍历二叉树。
题目就是就是这么简单,竟写了一个晚上, 顺便把二叉树的前, 中, 后 遍历也写了, 判断是否为完全二叉树还没有完成。
其实用指针来写的话,很形象生动,在写完之后觉得代码原来这么简单,第一次写之前什么都下不了手,此题关键在于你对指针的理解是否到位,运用是否熟练, 能够熟练运用指针就能很快地写完这道题。
在写判断是否为完全完二叉树的思想是: 将所有节点都放入队列中,无论是否为空节点,当队首出现空节点时,如果后面还有非空节点则判断他是非完全二叉树。这很好理解,生动形象。
因为定义是这样的:
完全二叉树: 前n-1层都是满的,第n层如有空缺,则是缺在右边,即第n层的最右边的节点,它的左边是满的,右边是空的。
以下源代码:
#include <iostream>
#include <sstream>
#include <queue>
using namespace std;
struct node {
int value;
bool have_value;
node * left, * right;
node() : value(0), have_value(0), left(NULL), right(NULL) { } //默认构造函数
};
bool flag = 0;
node* root; //创建根节点
void getin(int& n, string& s) {
node* temp = root; //建立temp 节点
for(int i = 0; i < s.size(); ++i)
if(s[i] == 'L') { // 两个if把) 忽略掉
if(temp->left == NULL) temp->left = new node; //先指向 所需指向的指针
temp = temp->left;
} else if(s[i] == 'R') {
if(temp->right == NULL) temp->right = new node; //如果所需指向指针还没有空间就创建空间
temp = temp->right;
}
if(temp->have_value == 1) flag = 1; //判断 指向的指针是否有 数值 有的话就重复输入了
temp->value = n;
temp->have_value = 1; //输入后记住这个已经输入了
}
void turn(string& s) {
string n, ss;
int n1;
for(int i = 0; i < s.size(); i++)
if(s[i] == ',') {
for(int j = 1; j < i; j++) n+=s[j];
for(int j = i + 1; j < s.size(); j++) ss+=s[j];
break;
}
stringstream str; //输入输出流的使用,唉o(︶︿︶)o 不会c语言的输入
str << n, str >> n1; //输出整数
getin(n1,ss); //分成 数字 和 字符串 包含 ) 比如 LLLLRR)
}
void bfs(node* p) { //广度优先遍历
queue<node* > q;
q.push(p);
while(!q.empty()) {
node* temp = q.front();
q.pop();
cout << temp->value << " ";
if(temp->left != NULL) q.push(temp->left);
if(temp->right != NULL) q.push(temp->right);
}
}
void qian(node* temp) { //前序遍历
cout << temp->value << " ";
if(temp->left != NULL) qian(temp->left);
if(temp->right != NULL) qian(temp->right);
}
void zhong(node* temp) { // 中序遍历
if(temp->left != NULL) qian(temp->left);
cout << temp->value << " ";
if(temp->right != NULL) qian(temp->right);
}
void hou(node* temp) { // 后序遍历
if(temp->left != NULL) qian(temp->left);
if(temp->right != NULL) qian(temp->right);
cout << temp->value << " ";
}
void prime1(node* temp) {
queue<node* > q;
q.push(temp);
flag = 1;
while(flag) {
temp = q.front();
q.pop();
if(temp == NULL) {
flag = 0; //如果 队首空了 跳出循环
while(!q.empty()) {
temp = q.front();
q.pop();
if(temp != NULL) {
cout << "prime1 : It is not a 完全二叉树!\n";
return ;
}
}
cout << "prime1 : It is a 完全二叉树!\n";
return ;
}
q.push(temp->left);
q.push(temp->right);
}
}
void prime2(node* temp) {
if(temp->left == NULL && temp->right == NULL)
return;
if(temp->left != NULL && temp->right == NULL) {
temp = temp->left;
if(temp->left != NULL || temp->right != NULL) {
cout << "prime1 : It is not a 完全二叉树!\n";
return;
}
}
prime2(temp->left);
prime2(temp->right);
}
int main() {
string s;
root = new node; //创建空间才能储存信息
while(cin >> s && s != "()") turn(s);
if(flag == 1) cout << "Your input was wrong!\n"; //如果输入重复提示
bfs(root);
cout << endl;
qian(root);
cout << endl;
zhong(root);
cout << endl;
hou(root);
//(1,L) (2,R) (3,LL) (4,RR) (5,LR) (6,RL) (7,) ()
cout << endl;
prime1(root);
prime2(root);
return 0;
}