二叉树的相关(自用)

#include <iostream>

// 二叉树节点结构体定义
struct TreeNode {
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

// 统计以当前节点为根节点的孩子节点个数
int countChildren(TreeNode* node) {
    if (node == nullptr) {
        return 0;
    }
    int count = 0;
    if (node->left!= nullptr) {
        count++;
    }
    if (node->right!= nullptr) {
        count++;
    }
    return count;
}

// 遍历二叉树统计每个节点的孩子节点个数(前序遍历示例,可按需换成其他遍历方式)
void countChildrenForEachNode(TreeNode* root) {
    if (root == nullptr) {
        return;
    }
    int childrenCount = countChildren(root);
    std::cout << "节点 " << root->val << " 的孩子节点个数为: " << childrenCount << std::endl;
    countChildrenForEachNode(root->left);
    countChildrenForEachNode(root->right);
}

int main() {
    // 构建一个简单的二叉树示例
    TreeNode* root = new TreeNode(1);
    root->left = new TreeNode(2);
    root->right = new TreeNode(3);
    root->left->left = new TreeNode(4);
    root->left->right = new TreeNode(5);

    countChildrenForEachNode(root);

    return 0;
}
#include <iostream>

// 二叉树节点结构体定义
struct TreeNode {
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

// 交换二叉树的左右子树
void swap(TreeNode* root) {
    if (root == NULL) {
        return;
    }
    TreeNode* temp = root->left;
    root->left = root->right;
    root->right = temp;
    swap(root->left);
    swap(root->right);
}

// 前序遍历二叉树
void preorderTraversal(TreeNode* root) {
    if (root == NULL) {
        return;
    }
    std::cout << root->val << " ";
    preorderTraversal(root->left);
    preorderTraversal(root->right);
}


int main() {
    // 手动构建一个简单的二叉树示例
    TreeNode* root = new TreeNode(1);
    root->left = new TreeNode(2);
    root->right = new TreeNode(3);
    root->left->left = new TreeNode(4);
    root->left->right = new TreeNode(5);

    std::cout << "交换前前序遍历结果: ";
    preorderTraversal(root);
    std::cout << std::endl;

    // 交换二叉树的左右子树
    swap(root);

    std::cout << "交换后前序遍历结果: ";
    preorderTraversal(root);
    std::cout << std::endl;

    return 0;
}
#include<iostream>
using namespace std;

struct BiNode
{
	char data;
	BiNode* lchild, * rchild;
};

class BiTree
{
public:
	BiTree() { root = Creat(); }
	~BiTree() { Release(root); }
	void PreOrder() { PreOrder(root); }
	void InOrder() { InOrder(root); }
	void PostOrder() { PostOrder(root); }
	void LevelOrder();


private:
	BiNode* Creat();
	void Release(BiNode* bt);
	void PreOrder(BiNode* bt);
	void InOrder(BiNode* bt);
	void PostOrder(BiNode* bt);
	BiNode* root;
};

BiNode* BiTree::Creat()
{
	BiNode* bt;
	char ch;
	cin >> ch;
	if (ch == '#')
		bt = nullptr;
	else {
		bt = new BiNode;
		bt->data = ch;
		bt->lchild = Creat();
		bt->rchild = Creat();
	}
	return bt;
}

void BiTree::Release(BiNode* bt)
{
	if (bt == nullptr)
		return;
	else {
		Release(bt->lchild);
		Release(bt->rchild);
		delete bt;
	}
}


void BiTree::PreOrder(BiNode* bt)
{
	if (bt == nullptr)
		return;
	else {
		cout << bt->data;
		PreOrder(bt->lchild);
		PreOrder(bt->rchild);
	}
}

void BiTree::InOrder(BiNode* bt)
{
	if (bt == nullptr)
		return;
	else {
		InOrder(bt->lchild);
		cout << bt->data;
		InOrder(bt->rchild);
	}
}

void BiTree::PostOrder(BiNode* bt)
{
	if (bt == nullptr)
		return;
	else {
		PostOrder(bt->lchild);
		PostOrder(bt->rchild);
		cout << bt->data;
	}
}

void BiTree::LevelOrder()
{
	BiNode* Q[100], * q = nullptr;
	int front = -1, rear = -1;
	if (root == nullptr)
		return;
	Q[++rear] = root;
	while (front != rear)
	{
		q = Q[++front];
		cout << q->data;
		if (q->lchild != nullptr)
			Q[++rear] = q->lchild;
		if (q->rchild != nullptr)
			Q[++rear] = q->rchild;
	}
}


int main()
{
	BiTree T;
	T.PreOrder();
}
#include<iostream>
#include<cmath>
using namespace std;

struct BiNode
{
	char data;
	BiNode* lchild, * rchild;
};

class BiTree
{
public:
	BiTree(){root = Creat();}
	BiNode* Creat();
	void Release(BiNode* bt);
	int SumNode(BiNode* bt);//节点个数
	void LevelOrder();
	int CountLeaf(BiNode* bt, int* count);//叶子节点个数
	int GetTreeDepth(BiNode* bt);//统计层数
	void SwapLR(BiNode* bt);
	BiNode* root;
};

BiNode* BiTree::Creat()
{
	BiNode* bt;
	char ch;
	cin >> ch;
	if (ch == '#')
		bt = nullptr;
	else {
		bt = new BiNode;
		bt->data = ch;
		bt->lchild = Creat();
		bt->rchild = Creat();
	}
	return bt;
}

void BiTree::Release(BiNode* bt)
{
	if (bt == nullptr)
		return;
	else {
		Release(bt->lchild);
		Release(bt->rchild);
		delete bt;
	}
}


int BiTree::SumNode(BiNode* bt)
{
	
	if (bt == nullptr)
		return 0;
	
	int leftCount = SumNode(bt->lchild);
	int rightCount = SumNode(bt->rchild);
	
	
	return leftCount+rightCount+1;
}

int BiTree::CountLeaf(BiNode* bt, int* count)
{
	if (bt != nullptr && bt->lchild == nullptr && bt->rchild == nullptr)
	{
		(*count)++;
	}
	if (bt != nullptr)
	{
		CountLeaf(bt->lchild,count);
		CountLeaf(bt->rchild,count);
	}
	return *count;
}


int BiTree::GetTreeDepth(BiNode* bt)
{
	if (bt == nullptr)
		return 0;
	int leftDepth = GetTreeDepth(bt->lchild);
	int rightDepth = GetTreeDepth(bt->rchild);
	return max(leftDepth, rightDepth) + 1;
}

void BiTree::LevelOrder()
{
	BiNode* Q[100], * q = nullptr;
	int front = -1, rear = -1;
	if (root == nullptr)
		return;
	Q[++rear] = root;
	while (front != rear)
	{
		q = Q[++front];
		cout << q->data;
		if (q->lchild != nullptr)
			Q[++rear] = q->lchild;
		if (q->rchild != nullptr)
			Q[++rear] = q->rchild;
	}
}


void BiTree::SwapLR(BiNode* bt)
{
	if (bt == nullptr)
		return;
	BiNode* temp = bt->lchild;//交换左右子树的指针
	bt->lchild = bt->rchild;
	bt->rchild = temp;
	SwapLR(bt->lchild);
	SwapLR(bt->rchild);
}

int main()
{
	BiTree T{};
	cout<<"节点个数:" << T.SumNode(T.root) << endl;
	int count = 0;
	cout <<"叶子节点个数:" << T.CountLeaf(T.root, &count)<<endl;
	cout << "层数:" << T.GetTreeDepth(T.root);
	cout << "交换LR前层序:" << endl;
	T.LevelOrder();
	T.SwapLR(T.root);
	cout << "交换LR后层序:" << endl;
	T.LevelOrder();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值