二叉树基本操作的实现

头文件

#include<stdio.h>
/*
	链队列
	q.front指向头结点
	q.front->next指向队头
	q.rear指向队尾
	队尾进,队头出
*/

typedef struct BiTNode
{
	char elem[20];
	struct BiTNode* lchild;
	struct BiTNode* rchild;
}BiTNode, * BiTree;

//链队列的结点类型
//Qnode结点类型 QueuePtr指向结点的指针的类型
typedef struct Qnode {
	BiTree data;
	struct Qnode* next;
}Qnode, * QueuePtr;
//链队列类型
typedef struct {
	QueuePtr front;
	QueuePtr rear;
}LinkQueue;

//初始化
/* 1申请头结点,尾指针和头指针都指向头结点
* 2.置头结点指针域为空
*/
int InitQueue(LinkQueue& lq)
{
	lq.front = lq.rear = new Qnode;
	if (!lq.front) return 0;
	lq.front->next = NULL;
	return 1;
}
/*
* 1申请新结点,给新结点赋值,将指针域置空
* 2.将新结点置为尾结点,尾指针指向新的尾结点
* lq.rear指向最后一个结点
* lq.front指向头结点
*/
int EnQueue(LinkQueue& lq, BiTree e)
{
	QueuePtr p = new Qnode;
	if (!p) return 0;
	p->data = e;
	p->next = NULL;
	lq.rear->next = p;//原来最后的结点的next
	lq.rear = p;
	return 1;
}
/*
* 1.判空
*2.p指向待删除的结点
* 3.判断队列中是否只有一个元素
* 4。删
*/
int DeQueue(LinkQueue& lq, BiTree& e)
{
	if (lq.front == lq.rear) return 0;
	QueuePtr p = lq.front->next;
	e = p->data;
	if (lq.rear == p)
		lq.rear = lq.front;
	lq.front->next = p->next;
	delete(p);
	return 1;
}
void DisplayLinkQueue(LinkQueue lq)
{
	QueuePtr p = lq.front->next;
	while (p != lq.rear->next)
	{
		printf("%s ", p->data->elem);
		p = p->next;
	}
}
bool QueueEmpty(LinkQueue lq)
{
	if (lq.front == lq.rear) return true;
	return false;
}

二叉树的实现(及一些基本操作) 

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include"Queue.h"
//二叉链表定义
void CreateBitree(BiTree& t)
{
	char elem[20] = { 0 };
	gets_s(elem);
	if (strcmp(elem, "#") == 0) t = NULL;
	else {
		t = new BiTNode;
		strcpy(t->elem, elem);
		CreateBitree(t->lchild);
		CreateBitree(t->rchild);
	}
}
void PreBitree(BiTree  t)
	{
		if (t) {
			printf("%s ", t->elem);
			PreBitree(t->lchild);
			PreBitree(t->rchild);
		}
}
void Print_ao(BiTree  t, char c, int level = 0)
{
	if (t) {
		int j = 0;
		while (j++ != level) {
			printf("  ");
		}
		level++;
		printf("%s -%c\n", t->elem, c);
		Print_ao(t->lchild, 'L', level + 2);
		Print_ao(t->rchild, 'R', level + 2);
	}
}
//层序遍历
void Print_level(BiTree t)
{
	LinkQueue lq;
	BiTree p;
	InitQueue(lq);
	if (t) EnQueue(lq, t);
	while (!QueueEmpty(lq))
	{
		DeQueue(lq, p); printf("%s ",p->elem);
		if (p->lchild) EnQueue(lq, p->lchild);
		if (p->rchild) EnQueue(lq, p->rchild);
	}
}
BiTree Find_BiTree(BiTree t,char e[20]) 
{
	if (t) {
		//字符串不能直接比
		if (strcmp(e,t->elem)==0)
			return t;
		Find_BiTree(t->lchild,e);
		Find_BiTree(t->rchild,e);
	}
}
// true为L false为右
void Bitree_Insert(BiTree &t, char father[20],bool L_or_R,char e[20])
{
	if (t) {
		//字符串不能直接比
		if (strcmp(father, t->elem) == 0)
		{
			//左孩子
			if (L_or_R) {
				if (t->lchild) {
					printf("已有元素,无法插入\n");
				}
				else{
					t->lchild = (BiTree)malloc(sizeof(BiTNode));
					strcpy(t->lchild->elem, e);
					//插入结点的左右孩子都置空
					BiTree p = t->rchild;
					p->lchild = NULL;
					p->rchild = NULL;
				}
			}
			//右孩子
			else{
				if (t->rchild) {
					printf("已有元素,无法插入\n");
				}
				else {
					//?怎么给t->rchild分配空间
					//BiTree p = (BiTree)malloc(sizeof(BiTNode));
					t->rchild = (BiTree)malloc(sizeof(BiTNode));
					strcpy(t->rchild->elem, e);

					//插入结点的左右孩子都置空
					BiTree p = t->rchild;
					p->lchild = NULL;
					p->rchild = NULL;
				}

			}
			return;
		}
		Find_BiTree(t->lchild, e);
		Find_BiTree(t->rchild, e);
	}

}

int Bitree_Delete_1(BiTree &t, char e[20])
{
	if (t) {
		//字符串不能直接比
		if (strcmp(e, t->elem) == 0) {
			t = NULL;
			return 1;
		}
		Bitree_Delete_1(t->lchild, e);
		Bitree_Delete_1(t->rchild, e);
	}
	return 0;
}
//销毁二叉树,按后序遍历销毁
void Bitree_Clear(BiTree& t)
{
	if (t) {
		Bitree_Clear(t->lchild);
		Bitree_Clear(t->rchild);
		delete t;
	}
}
int Bitree_Delete_2(BiTree& t, char e[20])
{
	if (t) {
		//字符串不能直接比
		if (strcmp(e, t->elem) == 0) {
			Bitree_Clear(t);
			t = NULL;
			return 1;
		}
		Bitree_Delete_2(t->lchild, e);
		Bitree_Delete_2(t->rchild, e);
	}
	return 0;
}
/*
完全二叉树:只有右子树有缺的
*/
bool isCompleteBitree(BiTree t)
{
	if (!t) return true;
	LinkQueue lq;
	BiTree p;
	bool flag = false;//有左右孩子为0,缺某一个孩子为1
	InitQueue(lq);
	if (t) EnQueue(lq, t);
	while (!QueueEmpty(lq))
	{
		DeQueue(lq, p);
		if (!p) {
			flag = true;
			continue;
		}
		else if(flag){
			return false;
		}
		else{
			//为空的结点也有入队
			EnQueue(lq, p->lchild);
			EnQueue(lq, p->rchild);
		}
	}
	return true;
}
//test: abd###ce##f##
int main() {
	BiTree T;
	CreateBitree(T);
	printf("\n===========\n");
	printf("%d\n", isCompleteBitree(T));
	PreBitree(T);
	printf("\n===========\n");
	Print_ao(T,'D');
	printf("\n===========\n");
	Print_level(T);
	printf("\n===========\n");
	printf("%s\n", Find_BiTree(T, const_cast<char*>("c"))->elem);
	printf("\ntest delete2\n");
	Bitree_Delete_2(T, const_cast<char*>("c"));
	Print_ao(T, 'D');
	printf("%d\n", isCompleteBitree(T));
	printf("\ntest insert\n");
	Bitree_Insert(T, const_cast<char*>("a"), false, const_cast<char*>("x"));
	Print_ao(T, 'D');
	printf("%d\n", isCompleteBitree(T));
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值