어떻게 두 갈래 나무가 만 두 갈래 나무인지 판단합니까?

2254 단어
#include<iostream>
#define N 15

using namespace std;

char str[] = "ab#d##c#e##";
int i = -1;

typedef struct node
{
	struct node *leftChild;
	struct node *rightChild;
	char data;
}BiTreeNode, *BiTree;

// 
BiTreeNode *createNode(int i)
{
	BiTreeNode * q = new BiTreeNode;
	q->leftChild = NULL;
	q->rightChild = NULL;
	q->data = i;

	return q;
}

BiTree createBiTree1()
{
	BiTreeNode *p[N] = {NULL};
	int i;
	for(i = 0; i < N; i++)
		p[i] = createNode(i + 1);

	//  
	for(i = 0; i < N/2; i++)
	{
		p[i]->leftChild = p[i * 2 + 1];
		p[i]->rightChild = p[i * 2 + 2];
	}

	return p[0];
}

void createBiTree2(BiTree &T)
{
	i++;
	char c;
	if(str[i] && '#' == (c = str[i]))
		T = NULL;
	else
	{
		T = new BiTreeNode;
		T->data = c;
		createBiTree2(T->leftChild);
		createBiTree2(T->rightChild);
	}
}

int max(int x, int y)
{
	return x > y ? x : y;
}

int getDepth(BiTree T)
{
	if(NULL == T)
		return 0;

	return 1 + max(getDepth(T->leftChild), getDepth(T->rightChild));
}

int getAllNode(BiTree T)
{
	if(NULL == T)
		return 0;

	return 1 + getAllNode(T->leftChild) + getAllNode(T->rightChild);
}

bool isFullBinaryTree(BiTree T)
{
	int all = getAllNode(T);
	int depth = getDepth(T);
	int n = all + 1;

	if(0 == ( n & (n - 1)) ) // 
		return true;

	return false;
}

void print(bool b)
{
	if(b)
		cout << "yes" << endl;
	else
		cout << "no" << endl;
}

int main()
{
	BiTree T1;
	T1 = createBiTree1();
    print(isFullBinaryTree(T1));

	BiTree T2;
	createBiTree2(T2);
    print(isFullBinaryTree(T2));

	return 0;
}

결과는 다음과 같습니다.
yes no

좋은 웹페이지 즐겨찾기