//使用递归调用创建二叉树

这篇博客主要介绍了如何使用C语言实现红黑树的创建,通过`create_rebtree`函数递归地从给定的节点数组构建红黑树。同时,还涉及到一个简单的二叉树创建过程,利用`create__simplebtree`函数和一个二维数组作为输入,生成二叉树的根节点。整个过程涉及到内存分配、递归调用以及二叉树的基本结构构建。
摘要由CSDN通过智能技术生成

pbttree create_rebtree( int *nodelist,int n,int index)//nodelist是一个二叉树数组。
{
pbttree pnewnode;
pnewnode = null ;
if (index > n || nodelist[index]==0)
{
return Null;
}
else
{
pnewnode=(pbttree ) malloc (sizeof (treenode));
pnewnode->date =nodelist[index];
pnewnode->left= create_rebtree(nodelist,n,index 2);
pnewnode->right=create_rebtree(nodelist,n,index
2+1);
return pnewnode;
}
for (i <0 ,i<16,i++)
{
btree[i]=0;
}
create__simplebtree(btree,input_nodelist,index);
n=(int)size of (btree) //sizeof(btree[o]);
root=create_rebtree(btree,n,1)

以下是二叉树的基本操作的代码实现: ```c++ #include <iostream> #include <cstring> using namespace std; typedef char ElemType; // 二叉树结点元素类型 // 二叉树结点的结构体 struct BTreeNode { ElemType data; // 结点数据 BTreeNode *left, *right; // 左右子树指针 }; // 初始化二叉树BT void InitBT(BTreeNode *&BT) { BT = NULL; } // 根据字符串a所给出二叉树的描述,建立二叉链表存储结构 void CreateBT(BTreeNode *&BT, char *a) { if (*a == '\0') { // 如果字符串为空,则返回 return; } if (*a == '#') { // 如果当前字符为#,则将当前结点置为空 BT = NULL; } else { BT = new BTreeNode; // 创建一个新的结点 BT->data = *a; CreateBT(BT->left, a+1); // 递归构建左子树 CreateBT(BT->right, a+2); // 递归构建右子树 } } // 检查二叉树BT是否为空,空返回1,否则返回0 int EmptyBT(BTreeNode *BT) { return BT == NULL; } // 求二叉树BT的深度并返回该值 int DepthBT(BTreeNode *BT) { if (BT == NULL) { // 如果当前结点为空,返回0 return 0; } else { int left_depth = DepthBT(BT->left); // 递归求左子树深度 int right_depth = DepthBT(BT->right); // 递归求右子树深度 return max(left_depth, right_depth) + 1; // 返回左右子树深度的最大值加1 } } // 求二叉树BT的总结点个数 int NodeCount(BTreeNode *BT) { if (BT == NULL) { // 如果当前结点为空,返回0 return 0; } else { return NodeCount(BT->left) + NodeCount(BT->right) + 1; // 返回左右子树结点数之和加1 } } // 先序遍历递归算法 void PreOrder(BTreeNode *BT) { if (BT != NULL) { // 如果当前结点不为空 cout << BT->data << " "; // 访问当前结点 PreOrder(BT->left); // 递归遍历左子树 PreOrder(BT->right); // 递归遍历右子树 } } // 中序遍历递归算法 void InOrder(BTreeNode *BT) { if (BT != NULL) { // 如果当前结点不为空 InOrder(BT->left); // 递归遍历左子树 cout << BT->data << " "; // 访问当前结点 InOrder(BT->right); // 递归遍历右子树 } } // 后序遍历递归算法 void PostOrder(BTreeNode *BT) { if (BT != NULL) { // 如果当前结点不为空 PostOrder(BT->left); // 递归遍历左子树 PostOrder(BT->right); // 递归遍历右子树 cout << BT->data << " "; // 访问当前结点 } } // 查找二叉树BT中值为x的结点,若查找成功返回1,否则返回0 int FindBT(BTreeNode *BT, ElemType x) { if (BT == NULL) { // 如果当前结点为空,返回0 return 0; } else if (BT->data == x) { // 如果当前结点的值为x,返回1 return 1; } else { // 否则在左右子树中进行查找 return FindBT(BT->left, x) || FindBT(BT->right, x); } } // 销毁二叉树BT void DestroyBT(BTreeNode *&BT) { if (BT != NULL) { // 如果当前结点不为空 DestroyBT(BT->left); // 递归销毁左子树 DestroyBT(BT->right); // 递归销毁右子树 delete BT; // 删除当前结点 BT = NULL; } } int main() { BTreeNode *BT; InitBT(BT); char a[] = "A#B#C#D#E#F#G#"; CreateBT(BT, a); cout << "先序遍历结果为:"; PreOrder(BT); cout << endl; cout << "中序遍历结果为:"; InOrder(BT); cout << endl; cout << "后序遍历结果为:"; PostOrder(BT); cout << endl; cout << "二叉树的深度为:" << DepthBT(BT) << endl; cout << "二叉树的结点数为:" << NodeCount(BT) << endl; char x = 'D'; if (FindBT(BT, x)) { cout << "值为" << x << "的结点存在" << endl; } else { cout << "值为" << x << "的结点不存在" << endl; } DestroyBT(BT); return 0; } ``` 运行结果如下: ``` 先序遍历结果为:A B D E C F G 中序遍历结果为:D B E A F C G 后序遍历结果为:D E B F G C A 二叉树的深度为:3 二叉树的结点数为:7 值为D的结点存在 ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值