求教:给定一个数字序列,按中序(前序)遍历构造一棵完全二叉树

bucuoqude 2008-04-03 09:07:04
请问,给定一个数字序列,比如 1,2,3,4,5,6 (以逗号分开),现在要按照中序(或者前序)遍历构造一棵完全二叉树,把这棵树画出来,请问这样的树是唯一的吗?(个人觉得是唯一的吧:》)
我写个函数 createBTree 里面应该传一个什么参数呀?是不是要 string str="1,2,3,4,5,6" 然后把str传进去呢?
还有 序列里的逗号应该怎么处理呢?
请高手给点思路吧
...全文
649 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
UltraBejing 2008-05-01
  • 打赏
  • 举报
回复
支持搂主,收藏
ryfdizuo 2008-04-04
  • 打赏
  • 举报
回复 1
struct BinaryTreeNode
{
char data; //方便一点;
BinaryTreeNode* leftChild;
BinaryTreeNode* rightChild;
BinaryTreeNode():leftChild(NULL), rightChild(NULL), data(5)
{}
};

void levelOrderConstruct(BinaryTreeNode* &root, const std::string& str)
{
std::queue<BinaryTreeNode*> queue;
BinaryTreeNode *temp;
int i=0;

queue.push(root);
while (!queue.empty()) {
temp = queue.front();
queue.pop();

if (temp != 0)
{
temp->data = str[i++];
if( i==str.size() )
break;
if(temp->leftChild == 0)
{
temp->leftChild= new BinaryTreeNode;
queue.push(temp->leftChild);
}
if(temp->rightChild == NULL)
{
temp->rightChild= new BinaryTreeNode;
queue.push(temp->rightChild);
}
}
}
}

void inOrderTraverse(BinaryTreeNode *root) {
if (0 == root) {
return;
}

if (root->leftChild != 0) {
inOrderTraverse(root->leftChild);
}
std::cout<<root->data<<" ";
if (root->rightChild != 0) {
inOrderTraverse(root->rightChild);
}
}

void destroyTree(BinaryTreeNode* root)
{
std::queue<BinaryTreeNode*> queue;
BinaryTreeNode *node = 0;

queue.push(root);
while (!queue.empty())
{
node = queue.front();
queue.pop();

if (node != 0) {
if (node->leftChild != 0) {
queue.push(node->leftChild);
}

if (node->rightChild != 0) {
queue.push(node->rightChild);
}
}
delete node;
}
}

int main()
{
std::string str="123456";
BinaryTreeNode* root= new BinaryTreeNode;
levelOrderConstruct(root, str);
inOrderTraverse(root);
destroyTree(root);

return 0;
}
//输出的非数字字符表示多余节点的字符;
ryfdizuo 2008-04-04
  • 打赏
  • 举报
回复
简单的讲,将节点按层次从1-n编号:
1
/ \
2 3
/ \ /
4 5 6

缺少的节点只能是大号的,
即:如果n号节点存在,则1到n-1号节点必定存在,
同样,若n号节点不存在,则n+1号及更大号的节点也必定不存在
这就唯一的确定了一颗完全二叉树了,
ryfdizuo 2008-04-04
  • 打赏
  • 举报
回复
lz说的这里构造的是完全二叉树,这个是可以的,
zhangbin_115 2008-04-03
  • 打赏
  • 举报
回复
不是唯一的。数据结构课本写的很详细
Thorwein 2008-04-03
  • 打赏
  • 举报
回复


#include <conio.h>
#include <stdio.h>
#include <stdlib.h>

#define OK 1
#define ERROR 0
#define FALSE 0
#define TURE 1
#define OVERFLOW -2
typedef int Status;
typedef char TElemType;

typedef struct BiTNode
{
TElemType data;
struct BiTNode *lchild;
struct BiTNode *rchild;
}BiTNode,*BiTree;

Status CreatBiTree(BiTNode **p)
{
char ch;
scanf("%c",&ch);/* input ab**e**或者abc**d**ef**g** */
if('*'==ch)
{
*p=NULL;
}

else
{
*p=(BiTNode *)malloc(sizeof(BiTNode));
if(!(*p))
exit(OVERFLOW);
(*p)->data=ch;
CreatBiTree(&((*p)->lchild));
CreatBiTree(&((*p)->rchild));
}
return OK;
}

Status visit(char data)
{
printf("%c",data);
return OK;
}

Status PreOrderTraverse(BiTNode *Bp)/*先序遍历*/
{
if(Bp)
{
if(visit(Bp->data))
{
if(PreOrderTraverse(Bp->lchild))
{
if(PreOrderTraverse(Bp->rchild))
{
return OK;
}
}
}
return ERROR;
}

else
{
return OK;
}
}

Status InOrderTraverse(BiTNode *Bp)/*中序遍历*/
{
if(Bp)
{
if(InOrderTraverse(Bp->lchild))
{
if(visit(Bp->data))
{
if(InOrderTraverse(Bp->rchild))
{
return OK;
}
}
return ERROR;
}
return ERROR;
}

else
{
return OK;
}
}

Status PostOrderTraverse(BiTNode *Bp)/*后序遍历*/
{
if(Bp)
{
if(InOrderTraverse(Bp->lchild))
{
if(InOrderTraverse(Bp->rchild))
{
if(visit(Bp->data))
{
return OK;
}
return ERROR;
}
}
return ERROR;
}
else
{
return OK;
}
}

int main()
{

BiTNode *Bp=NULL;

CreatBiTree(&Bp);
printf("\nPreOrderTraverse:");
PreOrderTraverse(Bp);
printf("\n");

printf("\nInOrderTraverse:");
InOrderTraverse(Bp);
printf("\n");

printf("\nPostOrderTraverse:");
PostOrderTraverse(Bp);
printf("\n");

getchar();
getchar();

return OK;
}


以前学数据结构的时候写的。 在读入一个元素的时候。你定义数组就可以了。这个不难。
Leejun527 2008-04-03
  • 打赏
  • 举报
回复
我记得仅凭中序是不能确定一个二叉树的,需要前序加中序,楼主看下数据结构书。
函数参数传一个字符串和串长度。
Inhibitory 2008-04-03
  • 打赏
  • 举报
回复
应该是唯一的,正好这两天复习树,你可以参考一下这个程序中的createTree()函数,还有就是levelOrderTraverse()是层次遍历,这样可以看到树的每一层和树是怎么构造的,inOrderTraverse()是用来中序遍历树:
至于其他的代码,可以不用看.

#include <iostream>
#include <fstream>
#include <string>
#include <stack>
#include <queue>
#include <ctime>
#include <cmath>

class BinaryTreeNode {
public:
BinaryTreeNode(int data = 0) {
leftChild = 0;
rightChild = 0;
parent = 0;
this->data = data;
}

~BinaryTreeNode() {
}

void visit() {
std::cout << data << " ";
}

BinaryTreeNode *leftChild;
BinaryTreeNode *rightChild;
BinaryTreeNode *parent;
int data;
};

class BinaryTree {
public:
BinaryTree();
~BinaryTree();

void add(int value);
bool isEmpty();
void createTree(int *array, int length);
void preOrderTraverse();
void inOrderTraverse();
void postOrderTraverse();
void levelOrderTraverse();

private:
void preOrderTraverse(BinaryTreeNode *root);
void inOrderTraverse(BinaryTreeNode *root);
void postOrderTraverse(BinaryTreeNode *root);
void createTree(BinaryTreeNode *&node, int *array, int first, int last);

private:
BinaryTreeNode *root;
};

BinaryTree::BinaryTree() {
root = 0;
}

BinaryTree::~BinaryTree() {
std::queue<BinaryTreeNode*> queue;
BinaryTreeNode *node = 0;

queue.push(root);
while (!queue.empty()) {
node = queue.front();
queue.pop();

if (node != 0) {
if (node->leftChild != 0) {
queue.push(node->leftChild);
}

if (node->rightChild != 0) {
queue.push(node->rightChild);
}
}

delete node;

#ifdef DEBUG
std::cout << "Destructor" << std::endl;
#endif
}
}

void BinaryTree::add(int value) {
if (isEmpty()) {
root = new BinaryTreeNode();
root->data = value;

return;
}

BinaryTreeNode *node = root;
while (true) {
if (value < node->data) {
if (0 == node->leftChild) {
node->leftChild = new BinaryTreeNode();
node->leftChild->data = value;
break;
} else {
node = node->leftChild;
}
} else {
if (0 == node->rightChild) {
node->rightChild = new BinaryTreeNode();
node->rightChild->data = value;
break;
} else {
node = node->rightChild;
}
}
}
}

bool BinaryTree::isEmpty() {
return (0 == root) ? true : false;
}

void BinaryTree::createTree(int *array, int length) {
createTree(root, array, 0, length - 1);
}

void BinaryTree::createTree(BinaryTreeNode *&node, int *array, int first, int last) {
if (first > last) {
return;
}

int mid = (first + last + 1) / 2;
node = new BinaryTreeNode(*(array + mid));

createTree(node->leftChild, array, first, mid - 1);
createTree(node->rightChild, array, mid + 1, last);
}

void BinaryTree::preOrderTraverse() {
//preOrderTraverse(root);

std::stack<BinaryTreeNode*> stack;
BinaryTreeNode *temp = 0;

stack.push(root);
while (!stack.empty()) {
// Go to the most left end.
while ((temp = stack.top()) != 0) {
temp->visit();
stack.push(temp->leftChild);
}
stack.pop();

if (!stack.empty()) {
temp = stack.top();
stack.pop();

stack.push(temp->rightChild);
}
}
}

void BinaryTree::preOrderTraverse(BinaryTreeNode *node) {
// DLR
if (0 == node) {
return;
}

node->visit();

if (0 != node->leftChild) {
preOrderTraverse(node->leftChild);
}

if (0 != node->rightChild) {
preOrderTraverse(node->rightChild);
}
}

void BinaryTree::inOrderTraverse() {
//inOrderTraverse(root);

std::stack<BinaryTreeNode*> stack;
BinaryTreeNode *temp = 0;

stack.push(root);
while (!stack.empty()) {
while ((temp = stack.top()) != 0) {
stack.push(temp->leftChild);
}
stack.pop();

if (!stack.empty()) {
temp = stack.top();
stack.pop();

temp->visit();
stack.push(temp->rightChild);
}
}
}

void BinaryTree::inOrderTraverse(BinaryTreeNode *root) {
if (0 == root) {
return;
}

if (root->leftChild != 0) {
inOrderTraverse(root->leftChild);
}

root->visit();

if (root->rightChild != 0) {
inOrderTraverse(root->rightChild);
}
}

void BinaryTree::postOrderTraverse() {
postOrderTraverse(root);
}

void BinaryTree::postOrderTraverse(BinaryTreeNode *root) {
if (0 == root) {
return;
}

if (root->leftChild != 0) {
inOrderTraverse(root->leftChild);
}

if (root->rightChild != 0) {
inOrderTraverse(root->rightChild);
}

root->visit();
}

void BinaryTree::levelOrderTraverse() {
std::queue<BinaryTreeNode*> queue;
BinaryTreeNode *temp;

queue.push(root);
while (!queue.empty()) {
temp = queue.front();
queue.pop();

if (temp != 0) {
temp->visit();
if (temp->leftChild != 0) {
queue.push(temp->leftChild);
}
if (temp->rightChild != 0) {
queue.push(temp->rightChild);
}
}
}
}

int main(int argc, char *argv[]) {
BinaryTree *bTree = new BinaryTree();

const int length = 7;
int array[length] = {1, 2, 3, 4, 5, 6, 7};
bTree->createTree(array, length);

bTree->levelOrderTraverse();
std::cout << std::endl;
bTree->inOrderTraverse();
std::cout << std::endl;

delete bTree;

return EXIT_SUCCESS;
}

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧