二叉树上的各种操作

/*****BiTree.h头文件******/
#define MAXQSIZE 50
#include<iostream>
#include<cstring>
#include<sstream>
using namespace std;

typedef char ElemType;

typedef struct Node{
	ElemType data;
	struct Node *lchild, *rchild;
}BiTNode, *BiTree;

typedef struct Queue{
	BiTree Base[MAXQSIZE];
	int front, rear;
}BTQUEUE;

void InitBiTree(BiTree &BT);

void BiTreeCreate(BiTree &BT);

bool BiTreeEmpty(BiTree BT);

void TraverseBiTree(BiTree BT, int Mark);

int BiTreeDepth(BiTree BT);

int BiTreeCount(BiTree BT);

void ClearBiTree(BiTree &BT);

/********************功能实现文件BiTreeFunctions.cpp********************/

#include "BiTree.h"
#include<iostream>
#include<stdio.h>
using namespace std;

void InitBiTree(BiTree &BT)
{
	BT = NULL;
}

void BiTreeCreate(BiTree &BT)
{
	char ch;
	cin >> ch;
	getchar();
	if (ch == '#')
	{
		BT= NULL;
	}
	else
	{
		BT = (BiTree)malloc(sizeof(BiTNode));
		if (!BT)
			exit(0);
		BT->data = ch;
		BiTreeCreate(BT->lchild);
		BiTreeCreate(BT->rchild);
	}
}

bool BiTreeEmpty(BiTree BT)
{
	return BT == NULL;
}

void TraverseBiTree(BiTree BT, int Mark)
{
	if (Mark == 1)       //前序
	{
		if (BT != NULL)
		{
			cout << BT->data << " ";
			TraverseBiTree(BT->lchild, Mark);
			TraverseBiTree(BT->rchild, Mark);
		}
	}
	else if (Mark == 2)  //中序
	{
		if (BT != NULL)
		{
			TraverseBiTree(BT->lchild, Mark);
			cout << BT->data << " ";
			TraverseBiTree(BT->rchild, Mark);
		}
	}
	else if (Mark == 3)  //后序
	{
		if (BT != NULL)
		{
			TraverseBiTree(BT->lchild, Mark);
			TraverseBiTree(BT->rchild, Mark);
			cout << BT->data << " ";
		}
	}
	else if (Mark == 4)  //层序
	{
		BTQUEUE Q;
		Q.front = Q.rear = 0;
		BiTree p;
		if (BT != NULL)
		{
			Q.Base[Q.rear] = BT;
			Q.rear = (Q.rear + 1) % MAXQSIZE;
		}
		while (Q.front!=Q.rear)
		{
			p = Q.Base[Q.front];
			cout << p->data << " ";
			Q.front = (Q.front + 1) % MAXQSIZE;
			if (p->lchild != NULL)
			{
				Q.Base[Q.rear] = p->lchild;
				Q.rear = (Q.rear + 1) % MAXQSIZE;
			}
			if (p->rchild != NULL)
			{
				Q.Base[Q.rear] = p->rchild;
				Q.rear = (Q.rear + 1) % MAXQSIZE;
			}
		}
	}
	else {
		cerr << "Mark值无效!遍历失败!!" << endl;
	}
}

int BiTreeDepth(BiTree BT)
{
	if (BT == NULL)
		return 0;
	else
	{
		int depth1 = BiTreeDepth(BT->lchild);
		int depth2 = BiTreeDepth(BT->rchild);
		if (depth1 > depth2)
			return depth1 + 1;
		else return depth2 + 1;
	}
}


int BiTreeCount(BiTree BT)
{
	if (BT == NULL)
		return 0;
	else return BiTreeCount(BT->lchild) + BiTreeCount(BT->rchild) + 1;
}

void ClearBiTree(BiTree &BT)
{
	if (BT != NULL)
	{
		ClearBiTree(BT->lchild);
		ClearBiTree(BT->rchild);
		delete BT;
		BT = NULL;
	}
}

/*********************主函数文件BiTree.cpp*********************/

#include<iostream>
#include<cstring>
#include<stdlib.h>
#include "BiTree.h"
using namespace std;

int main()
{
	BiTNode *bt;

	InitBiTree(bt);
	BiTreeCreate(bt);

	cout << "前序遍,递归实现:" << endl;
	TraverseBiTree(bt, 1);
	cout << endl;

	cout << "中序遍,递归实现:" << endl;
	TraverseBiTree(bt, 2);
	cout << endl;

	cout << "后序遍,递归实现:" << endl;
	TraverseBiTree(bt, 3);
	cout << endl;

	cout << "层序遍历,递归实现:" << endl;
	TraverseBiTree(bt, 4);
	cout << endl;

	cout << "二叉树的深度为:" << endl;
	cout << BiTreeDepth(bt) << endl;

	cout << "二叉树的结点数为:" << endl;
	cout << BiTreeCount(bt) << endl;

	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是二叉树的基本操作的C语言实现: 结构体定义: ``` struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; }; ``` 1. 创建二叉树 ``` struct TreeNode* createTree(int val) { struct TreeNode* root = (struct TreeNode*)malloc(sizeof(struct TreeNode)); root->val = val; root->left = NULL; root->right = NULL; return root; } void insertLeft(struct TreeNode* root, int val) { struct TreeNode* newNode = (struct TreeNode*)malloc(sizeof(struct TreeNode)); newNode->val = val; newNode->left = NULL; newNode->right = NULL; root->left = newNode; } void insertRight(struct TreeNode* root, int val) { struct TreeNode* newNode = (struct TreeNode*)malloc(sizeof(struct TreeNode)); newNode->val = val; newNode->left = NULL; newNode->right = NULL; root->right = newNode; } ``` 2. 二叉树的遍历 ``` // 前序遍历 void preorderTraversal(struct TreeNode* root) { if (root == NULL) { return; } printf("%d ", root->val); preorderTraversal(root->left); preorderTraversal(root->right); } // 中序遍历 void inorderTraversal(struct TreeNode* root) { if (root == NULL) { return; } inorderTraversal(root->left); printf("%d ", root->val); inorderTraversal(root->right); } // 后序遍历 void postorderTraversal(struct TreeNode* root) { if (root == NULL) { return; } postorderTraversal(root->left); postorderTraversal(root->right); printf("%d ", root->val); } ``` 3. 二叉树的查找 ``` struct TreeNode* search(struct TreeNode* root, int val) { if (root == NULL) { return NULL; } if (root->val == val) { return root; } struct TreeNode* left = search(root->left, val); if (left != NULL) { return left; } struct TreeNode* right = search(root->right, val); if (right != NULL) { return right; } return NULL; } ``` 4. 二叉树的删除 ``` struct TreeNode* deleteNode(struct TreeNode* root, int key) { if (root == NULL) { return NULL; } if (root->val == key) { if (root->left == NULL && root->right == NULL) { free(root); return NULL; } if (root->left == NULL) { struct TreeNode* right = root->right; free(root); return right; } if (root->right == NULL) { struct TreeNode* left = root->left; free(root); return left; } struct TreeNode* min = root->right; while (min->left != NULL) { min = min->left; } root->val = min->val; root->right = deleteNode(root->right, min->val); } else if (root->val > key) { root->left = deleteNode(root->left, key); } else { root->right = deleteNode(root->right, key); } return root; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值