# 二叉树------层序遍历

#include <iostream>
#include <deque>
#include <stdio.h>
using namespace std;
#define N 100
struct Node
{
char data;
struct Node *left, *right;
};

struct SeQueue
{
int front;
int end;
};

Node *MakeTree();
void DisplayTree(Node *tree);
void PrintTreeByFloor(Node *tree);
void DeleteTree(Node **tree);

int main()
{
Node *myTree = MakeTree();

cout << "MidOrder: " << endl;
DisplayTree(myTree);
cout << endl;

cout << "print tree by floor: " << endl;
PrintTreeByFloor(myTree);
cout << endl;

DeleteTree(&myTree);
return 0;
}

Node *MakeTree()
{
char ch;
SeQueue Q;
SeQueue *q = &Q;
q->front = 1;
q->end = 0;
Node *tmp = NULL;
Node *root = NULL;

cout << "Input some chars to make a tree(less than 99 and end by '@'):" << endl;
ch = getchar();
while(ch != '@' && ch != '\n')
{
tmp = new Node();
tmp->data = ch;
tmp->left = tem->right = NULL;
q->end += 1;
if(root = NULL)
{
root = tmp;
}
else
{
{
if(q->end % 2 == 0)
else
}
if(q->end % 2 != 0)
q->front += 1;
}
ch = getchar();
}
return root;
}

void DisplayTree(Node *tree)
{
Node *p = tree;
if(p)
{
DisplayTree(p->left);
cout << p->data << " ";
DisplayTree(p->right);
}
}

void PrintTreeByFloor(Node *tree)
{
if(tree == NULL)
return ;
deque<Node *> treeNode;
treeNode.push_back(tree);

while(treeNode.size())
{
Node *tmp = treeNode.front();
treeNode.pop_front();
cout << tmp->data << " ";
if(tmp->left)
treeNode.push_back(tmp->left);
if(tmp->right)
treeNode.push_back(tmp->right);
}
}

void DeleteTree(Node **tree)
{
if(tree == NULL)
return ;
Node *p = *tree;
if(p)
{
DeleteTree(&p->left);
DeleteTree(&p->right);
delete [] p;
}
*tree = NULL;
}

• 本文已收录于以下专栏：

举报原因： 您举报文章：二叉树------层序遍历 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)