Description
Figure 1
Figure 1 shows a graphical representation of a binary tree of letters. People familiar with binary trees can skip over the definitions of a binary tree of letters, leaves of a binary tree, and a binary search tree of letters, and go right to The problem.
A binary tree of letters may be one of two things:
- It may be empty.
- It may have a root node. A node has a letter as data and refers to a left and a right subtree. The left and right subtrees are also binary trees of letters.
- Empty trees are omitted completely.
- Each node is indicated by
- Its letter data,
- A line segment down to the left to the left subtree, if the left subtree is nonempty,
- A line segment down to the right to the right subtree, if the right subtree is nonempty.
- Its letter data,
The preorder traversal of a tree of letters satisfies the defining properties:
- If the tree is empty, then the preorder traversal is empty.
- If the tree is not empty, then the preorder traversal consists of the following, in order
- The data from the root node,
- The preorder traversal of the root's left subtree,
- The preorder traversal of the root's right subtree.
- The data from the root node,
A tree like the one in Figure 1 is also a binary search tree of letters. A binary search tree of letters is a binary tree of letters in which each node satisfies:
The root's data comes later in the alphabet than all the data in the nodes in the left subtree.
The root's data comes earlier in the alphabet than all the data in the nodes in the right subtree.
The problem:
Consider the following sequence of operations on a binary search tree of letters
Remove the leaves and list the data removed
Repeat this procedure until the tree is empty
Starting from the tree below on the left, we produce the sequence of trees shown, and then the empty tree
by removing the leaves with data
BDHPY
CM
GQ
K
Your problem is to start with such a sequence of lines of leaves from a binary search tree of letters and output the preorder traversal of the tree.
The lines contain the leaves removed from a binary search tree in the stages described above. The letters on a line will be listed in increasing alphabetical order. Data sets are separated by a line containing only an asterisk ('*').
The last data set is followed by a line containing only a dollar sign ('$'). There are no blanks or empty lines in the input.
BDHPY CM GQ K * AC B $
Sample Output
KGCBDHQMPY BAC
---------------------------------------
#include<iostream>
#include<stdio.h>using namespace std;
struct Node //定义一个结点
{
char ch; //结点的值
Node *left; //结点的左子树
Node *right; //结点右子树
};
Node node[50000]; //结点的数组对象
char str[100][1000]={0}; //用来存储字符串的数组
int t; //设定一个变量,用来进行比较时改变元素
void insert(Node *root) //对跟进行比较,以及新的左右子树的遍历
{
if (node[t].ch<root->ch)
{
if (root->left == NULL)
root->left = &node[t];
else
insert(root->left);
}
if (node[t].ch>root->ch)
{
if (root->right == NULL)
root->right = &node[t];
else
insert(root->right);
}
}
void tre(Node *root) //用来输出结点的值
{
if (root)
{
printf("%c", root->ch);
//cout << root->ch;
tre(root->left);
tre(root->right);
}
}
int main()
{
int i, j = 0, k, p;
bool flag;
while (1)
{
flag = false;
for (i = 0; i<100; i++)
{
scanf("%c", &str[i]);
cin >> str[i]; //输入字符
if (str[i][0] == '*' || str[i][0] == '$') //判断是否已经达到输出的标准了
{
if (str[i][0] == '$')
flag = true;
p = 1;
i--;
node[0].ch = str[i][0]; //对 每个结点进行初始化
node[0].left = node[0].right = NULL;
i--;
while (i >= 0) //判断该节点的位置是否存在
{
k = 0;
while (str[i][k]) //给某一结点的值
{
node[p].ch = str[i][k];
node[p].left = node[p].right = NULL;
t = p;
insert(&node[0]); //把结点插入二叉树中
k++;
p++; //用来比较时控制元素的变化
}
i--;
}
tre(&node[0]); //打印出结点
printf("\n");
}
// if (flag)
// return 0;
}
}
return 0;
}
---------------------------------------上面的方法提交时超时 ,下面的方法我还没看懂,可以注释的大神请留言或私信我/*
题意:定义一个由字母构成的二叉树。对于二叉树内任一个结点,左子树的所有字母的字母序均在该结点字母之前,右子树的所有字母的字母序均在该结点字母之后。
现从二叉树的各叶子开始,拆除二叉树内无左右子结点的结点,最终使得该二叉树为空。现对一字母二叉树进行拆除操作,给定每一步拆除字母,问原二叉树的先序遍历序列。
思路:可根据每步拆除的字母,反过来还原该二叉树。也就是,从后往前扫描每一步拆除的字母,重新构建字母二叉树。最后一步肯定是根。
而对于其他步,遂于每一个字母,对二叉树进行有序的插入操作。将树结构还原。
每一步的每一个字母,进行位置的查找,找出该字母对应的父节点。然后在作为叶子,加至父节点的左子或者右子。
*/
#include <iostream>
#include <string>
#include <fstream>
#include <deque>
using namespace std;
template<typename T>
struct Node
{
T value;
Node<T>* lChild;
Node<T>* rChild;
Node<T>* parent;
};
template<typename T>
class FallingLeaves
{
protected:
Node<T>* root;
int count;
void Distroy(Node<T>* r);
void Insert(Node<T>*& r, T value);
void Output(Node<T>* r) const;
public:
FallingLeaves();
~FallingLeaves();
void Insert(T value);
void Output() const;
};
template<typename T>
FallingLeaves<T>::FallingLeaves()
{
root = NULL;
count = 0;
}
template<typename T>
FallingLeaves<T>::~FallingLeaves()
{
Distroy(root);
}
template<typename T>
void FallingLeaves<T>::Distroy(Node<T>* r)
{
if (!r)
{
return;
}
Distroy(r->lChild);
Distroy(r->rChild);
delete r;
}
template<typename T>
void FallingLeaves<T>::Insert(Node<T>*& r, T value)
{
if (!r)
{
r = new Node<T>();
r->value = value;
r->parent = r;
r->lChild = NULL;
r->rChild = NULL;
count++;
return;
}
if (value < r->value)
{
Insert(r->lChild, value);
}
else
{
Insert(r->rChild, value);
}
}
template<typename T>
void FallingLeaves<T>::Insert(T value)
{
Insert(root, value);
}
template<typename T>
void FallingLeaves<T>::Output(Node<T>* r) const
{
if (!r)
{
return;
}
cout << r->value;
Output(r->lChild);
Output(r->rChild);
}
template<typename T>
void FallingLeaves<T>::Output() const
{
Output(root);
cout << "\n";
}
int main()
{
string s;
deque<string> dq; //双端队列
while (cin >> s)
{
if (s == "*" || s == "$")
{
FallingLeaves<char> obj;
for (unsigned int i = 0; i < dq.size(); i++)
{
for (string::reverse_iterator iter = dq[i].rbegin(); iter != dq[i].rend(); iter++)
{
obj.Insert(*iter);
}
}
obj.Output();
dq.clear();
if (s == "$")
{
break;
}
}
else
{
dq.push_front(s);
}
}
return 0;
}