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

#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;
}

二叉树系列--层序遍历（java实现）

2015-09-19 19:52:02

C语言二叉树的层序遍历

2015-11-22 15:41:32

树：二叉树的层序遍历算法（超简洁实现及详细分析）

2018-02-26 00:30:45

二叉树层序遍历

2014-02-06 11:44:16

【数据结构与算法】二叉树的层序遍历

2013-10-27 14:51:06

二叉树的层序遍历详细讲解（附完整C++程序）

2017-07-03 22:23:04

二叉树的层序遍历（递归）

2016-11-08 22:44:25

层序遍历二叉树（不借助vector或deque，用C语言实现）

2016-06-02 19:36:23

二叉树层序遍历-实现代码-队列

2012年12月02日 3KB 下载