//二叉树的二叉链表存储表示 typedef struct BiNode { /**************begin************/ /**************end************/ }BiTNode,*BiTree;
时间: 2024-05-15 16:10:16 浏览: 206
二叉树的二叉链表存储表示是指使用链表来表示二叉树的结构,其中每个节点包含三个部分:数据域、左孩子指针和右孩子指针。BiNode结构体中的数据域可以根据实际情况进行定义,一般来说是存储该节点的数据信息。BiTNode代表二叉树的一个节点,BiTree则代表整棵二叉树。以下是BiNode结构体的代码实现:
typedef struct BiNode
{
int data; // 数据域
struct BiNode *lchild; // 左孩子指针
struct BiNode *rchild; // 右孩子指针
}BiTNode,*BiTree;
相关问题
//算法5.3 先序遍历的的顺序建立二叉链表 #include<iostream> using namespace std; //二叉树的二叉链表存储表示 typedef struct BiNode { /**************begin************/ /**************end************/ }BiTNode,*BiTree; //1. 以二叉链表表示二叉树,建立一棵二叉树(算法5.3); void CreateBiTree(BiTree &T) { /**************begin************/ /**************end************/ } //CreateBiTree //2.输出二叉树的中序遍历结果(算法5.1); void InOrderTraverse(BiTree T) { /**************begin************/ /**************end************/ } //3.输出二叉树的前序遍历结果(见讲稿); void PreOrderTraverse(BiTree T){ /**************begin************/ /**************end************/ } // 4.输出二叉树的后序遍历结果(见讲稿); void PostOrderTraverse(BiTree T){ /**************begin************/ /**************end************/ } //5.计算二叉树的深度(算法5.5); int Depth(BiTree T) { /**************begin************/ /**************end************/ } //6.统计二叉树的结点个数(算法5.6); int NodeCount(BiTree T){ /**************begin************/ /**************end************/ } //7.统计二叉树的叶结点个数; int LeafCount(BiTree T){ /**************begin************/ /**************end************/ } //8.统计二叉树的度为1的结点个数; int n1Count(BiTree T,int n,int n0){ /**************begin************/ /**************end************/ } int main() { BiTree tree; //cout<<"请输入建立二叉链表的序列:\n"; //cout<<"请输入根节点:"; CreateBiTree(tree); cout<<"所建立的二叉链表先序序列:\n"; PreOrderTraverse(tree); cout<<"\n"; cout<<"所建立的二叉链表中序序列:\n"; InOrderTraverse(tree); cout<<"\n"; cout<<"所建立的二叉链表后序序列:\n"; PostOrderTraverse(tree); cout<<"\n"; int depth = Depth(tree); cout<<"所建立的二叉链表深度是:\n"; cout << "depth = " << depth; cout<<"\n"; cout<<"所建立的二叉链表结点个数是:\n"; int nodeCount = NodeCount(tree); cout << "nodeCount(n) = " << nodeCount; cout<<"\n"; cout<<"所建立的二叉链表叶结点个数是:\n"; int leafCount = LeafCount(tree); cout << "leafCount(n0) = " << leafCount; cout<<"\n"; cout<<"所建立的二叉链表度为1的结点个数是:\n"; int n1 = n1Count(tree,nodeCount,leafCount); cout << "n1 = " <<n1 ; cout<<"\n"; cout<<endl; return 0; }
### C++ 二叉树 遍历与统计实现
以下是基于C++语言的二叉树先序遍历、中序遍历、后序遍历、深度计算以及节点统计的相关算法实现。
#### 定义二叉树结构
首先定义一个简单的二叉树节点类 `TreeNode` 和操作该树的方法:
```cpp
#include <iostream>
#include <stack>
using namespace std;
// 定义二叉树节点结构
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int data) : val(data), left(nullptr), right(nullptr) {}
};
```
---
#### 先序遍历 (Preorder Traversal)
先序遍历遵循 **根 -> 左子树 -> 右子树** 的顺序。可以采用递归或迭代两种方式实现。
##### 递归方法
```cpp
void preorderRecursive(TreeNode* root) {
if (!root) return; // 如果当前节点为空,返回
cout << root->val << " "; // 访问根节点
preorderRecursive(root->left); // 遍历左子树
preorderRecursive(root->right); // 遍历右子树
}
```
##### 迭代方法
```cpp
void preorderIterative(TreeNode* root) {
if (!root) return; // 如果树为空,直接退出
stack<TreeNode*> s;
s.push(root);
while (!s.empty()) { // 当栈不为空时循环
TreeNode* node = s.top(); s.pop();
cout << node->val << " "; // 输出当前节点
if (node->right) s.push(node->right); // 先压入右孩子(后处理)
if (node->left) s.push(node->left); // 再压入左孩子(先处理)
}
}
```
---
#### 中序遍历 (Inorder Traversal)
中序遍历遵循 **左子树 -> 根 -> 右子树** 的顺序。
##### 递归方法
```cpp
void inorderRecursive(TreeNode* root) {
if (!root) return; // 如果当前节点为空,返回
inorderRecursive(root->left); // 遍历左子树
cout << root->val << " "; // 访问根节点
inorderRecursive(root->right); // 遍历右子树
}
```
##### 迭代方法
```cpp
void inorderIterative(TreeNode* root) {
if (!root) return; // 如果树为空,直接退出
stack<TreeNode*> s;
TreeNode* curr = root;
while (curr || !s.empty()) { // 当指针非空或者栈中有待处理节点时循环
while (curr) { // 将所有左子树压入栈
s.push(curr);
curr = curr->left;
}
curr = s.top(); s.pop(); // 处理栈顶节点
cout << curr->val << " "; // 输出当前节点
curr = curr->right; // 移动到右子树
}
}
```
---
#### 后序遍历 (Postorder Traversal)
后序遍历遵循 **左子树 -> 右子树 -> 根** 的顺序。
##### 递归方法
```cpp
void postorderRecursive(TreeNode* root) {
if (!root) return; // 如果当前节点为空,返回
postorderRecursive(root->left); // 遍历左子树
postorderRecursive(root->right); // 遍历右子树
cout << root->val << " "; // 访问根节点
}
```
##### 迭代方法
由于后序遍历需要两次访问同一节点,因此可以通过辅助栈来模拟这种行为。
```cpp
void postorderIterative(TreeNode* root) {
if (!root) return; // 如果树为空,直接退出
stack<TreeNode*> s1, s2;
s1.push(root);
while (!s1.empty()) { // 使用两个栈完成后序遍历
TreeNode* node = s1.top(); s1.pop();
s2.push(node); // 倒置输出顺序
if (node->left) s1.push(node->left); // 先压入左孩子
if (node->right) s1.push(node->right); // 再压入右孩子
}
while (!s2.empty()) { // 最终从第二个栈依次弹出并打印
cout << s2.top()->val << " ";
s2.pop();
}
}
```
---
#### 深度计算 (Tree Depth Calculation)
通过递归求解树的最大深度。
```cpp
int maxDepth(TreeNode* root) {
if (!root) return 0; // 空树高度为0
int leftHeight = maxDepth(root->left); // 获取左子树的高度
int rightHeight = maxDepth(root->right); // 获取右子树的高度
return max(leftHeight, rightHeight) + 1; // 返回较大者加1
}
```
---
#### 节点统计 (Node Counting)
同样可通过递归统计整个树中的节点数。
```cpp
int countNodes(TreeNode* root) {
if (!root) return 0; // 空树节点数为0
return countNodes(root->left) + countNodes(root->right) + 1; // 统计左右子树和当前节点
}
```
---
### 总结
以上代码实现了二叉树的主要功能,包括三种遍历方式及其对应的递归与迭代版本,同时还提供了深度计算和节点统计的功能[^1]。
//算法5.6 统计二叉树中结点的个数 #include<iostream> using namespace std; //二叉树的二叉链表存储表示 typedef struct BiNode 1 char data; //结点数据域 struct BiNode *lchild,*rchild; //左右孩子指针 ] BiTNode,*BiTree; //用算法5.3建立二叉链表 void CreateBiTree(BiTree &T) //用算法5.3建立二叉链表 void CreateBiTree(BiTree &T) //按先序次序输入二叉树中结点的值(一个字符),创建二叉链表表示的二叉树T char ch; cin >> ch; if(ch=='#') T=NULL; //递归结束,建空树 else T=new BiTNode; T->data=ch; //生成根结占 CreateBiTree(T->lchild). /递归创建左子树 CreateBiTree(T->rchild); /递归创建右子树 //else ) //CreateBiTree //else ) //CreateBiTree int NodeCount(BiTree T) if(T==NULL) return 0; 如果是空树,则结点个数为0,递归结束 else return NodeCount(T->lchild)+ NodeCount(T->rchild) +1; //否则结点个数为左子树的结点个数+右子树的结点个数+1 void main() void main() BiTree tree; cout<<"请输入建立二叉链表的序列:\n"; CreateBiTree(tree); cout<<"结点个数为: "<<NodeCount(tree)<<endl; )
这段代码是用来统计二叉树中结点个数的,使用的是二叉链表存储结构。其中CreateBiTree函数用来建立二叉树,按照先序遍历的顺序输入二叉树中结点的值,如果输入的是'#'代表该节点为空。NodeCount函数用来递归计算二叉树中结点的个数,如果是空树,返回0;否则返回左子树的结点个数+右子树的结点个数+1。最后在主函数中调用CreateBiTree和NodeCount函数,并输出结果。
阅读全文
相关推荐













