// printBTTree.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
#include <Windows.h>
using namespace std;
typedef struct _BTNode
{
int data;
struct _BTNode *left;
struct _BTNode *right;
}BTNode;
#define LENGTH 20
struct _BTNode *array[LENGTH];
int sum = 1;
int count = 0;
void print(BTNode *head) {
if (head == NULL)
{
return;
}
array[0] = head;
while (sum != count)
{
for (int i = count; i < sum; i++)
{
printf("%d ", array[i]->data);
count = (count > LENGTH - 1?0:++count);
if (array[i]->left)
{
array[(sum > LENGTH - 1?0:sum++)] = head->left;
}
if (array[i]->right)
{
array[(sum > LENGTH - 1?0:sum++)] = head->right;
}
}
}
}
void buildBTTree(BTNode *head) {
int i = 1;
while(i > 0) {
cin>>i;
head->left = new BTNode;
head->left->left = NULL;
head->left->right = NULL;
head->left->data = i;
cin>>i;
head->right = new BTNode;
head->right->left = NULL;
head->right->right = NULL;
head->right->data = i;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
BTNode head;
head.data = 1;
head.left = NULL;
head.right = NULL;
BTNode *p_node = &head;
buildBTTree(p_node);
print(p_node);
getchar();
system("pause");
return 0;
}
//
#include "stdafx.h"
#include <iostream>
#include <Windows.h>
using namespace std;
typedef struct _BTNode
{
int data;
struct _BTNode *left;
struct _BTNode *right;
}BTNode;
#define LENGTH 20
struct _BTNode *array[LENGTH];
int sum = 1;
int count = 0;
void print(BTNode *head) {
if (head == NULL)
{
return;
}
array[0] = head;
while (sum != count)
{
for (int i = count; i < sum; i++)
{
printf("%d ", array[i]->data);
count = (count > LENGTH - 1?0:++count);
if (array[i]->left)
{
array[(sum > LENGTH - 1?0:sum++)] = head->left;
}
if (array[i]->right)
{
array[(sum > LENGTH - 1?0:sum++)] = head->right;
}
}
}
}
void buildBTTree(BTNode *head) {
int i = 1;
while(i > 0) {
cin>>i;
head->left = new BTNode;
head->left->left = NULL;
head->left->right = NULL;
head->left->data = i;
cin>>i;
head->right = new BTNode;
head->right->left = NULL;
head->right->right = NULL;
head->right->data = i;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
BTNode head;
head.data = 1;
head.left = NULL;
head.right = NULL;
BTNode *p_node = &head;
buildBTTree(p_node);
print(p_node);
getchar();
system("pause");
return 0;
}