二叉树的创建
时间: 2023-11-11 18:02:13 浏览: 25
二叉树可以通过递归方式来创建。具体步骤如下:
1. 定义二叉树结构体或类,结构体或类中包含左子树指针、右子树指针和节点值等属性。
2. 通过递归方式,先创建根节点,然后再递归创建左子树和右子树。
3. 确定终止条件,当节点为空时,返回空指针。
具体的代码实现可以参考以下示例:
```
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
TreeNode* createTree(vector<int>& nums, int index) {
if (index >= nums.size() || nums[index] == -1) {
return NULL;
}
TreeNode* root = new TreeNode(nums[index]);
root->left = createTree(nums, 2 * index + 1);
root->right = createTree(nums, 2 * index + 2);
return root;
}
```
其中,vector<int>& nums 表示按照二叉树的顺序遍历得到的节点值,index 表示当前节点在 vector 中的下标。在实际应用中,也可以根据实际情况进行修改。
相关问题
二叉树建立
二叉树是一种广泛应用于计算机科学中的数据结构,用于存储有层次关系的数据。下面介绍两种建立二叉树的方法。
1. 递归建树
递归建树是一种比较简单的方法,其基本思路是:
- 如果当前节点为空,则创建一个新节点并将数据赋值给它;
- 如果当前节点不为空,则比较待插入数据和当前节点的大小,如果待插入数据比当前节点小,则递归调用左子树,否则递归调用右子树。
下面是递归建树的示例代码:
```python
class TreeNode:
def __init__(self, val):
self.val = val
self.left = None
self.right = None
def insert(root, val):
if root is None:
return TreeNode(val)
if val < root.val:
root.left = insert(root.left, val)
else:
root.right = insert(root.right, val)
return root
```
2. 迭代建树
迭代建树是一种较为复杂的方法,需要利用栈来辅助建立二叉树。其基本思路是:
- 创建一个空根节点,将待插入数据赋值给根节点;
- 从第二个数据开始遍历,对于每个数据,依次与根节点比较大小,如果比根节点小,则作为左子树的根节点,否则作为右子树的根节点;
- 使用一个栈来保存访问过的节点,每次访问节点时将其入栈;
- 如果当前节点为空,则从栈中弹出一个节点作为当前节点。
下面是迭代建树的示例代码:
```python
class TreeNode:
def __init__(self, val):
self.val = val
self.left = None
self.right = None
def insert(root, val):
if root is None:
return TreeNode(val)
stack = [root]
while stack:
node = stack.pop()
if val < node.val:
if node.left is None:
node.left = TreeNode(val)
break
else:
stack.append(node.left)
else:
if node.right is None:
node.right = TreeNode(val)
break
else:
stack.append(node.right)
return root
```
以上两种方法都可以用于建立二叉搜索树,其中递归建树是更为常见的方法。
c++ 完全二叉树创建
完全二叉树是一种特殊的二叉树,它的每一层都被完全填满,除了最后一层可能不满。在创建完全二叉树时,可以按照层次遍历的顺序依次插入节点。
创建完全二叉树的步骤如下:
1. 创建一个空的二叉树。
2. 创建根节点,并将其插入到二叉树中。
3. 按层次顺序遍历二叉树,从根节点开始,依次遍历每个节点。
4. 对于当前遍历的节点,如果它的左子节点为空,则创建一个新的节点,并将其作为当前节点的左子节点;如果它的右子节点为空,则创建一个新的节点,并将其作为当前节点的右子节点。
5. 重复步骤4,直到所有节点都被插入到二叉树中。
下面是用C语言实现创建完全二叉树的示例代码:
```
// 树节点的定义
struct bstree_node {
int data;
struct bstree_node* left;
struct bstree_node* right;
};
// 创建完全二叉树
struct bstree_node* create_complete_binary_tree(int arr[], int n) {
if (n == 0) {
return NULL;
}
// 创建根节点
struct bstree_node* root = (struct bstree_node*)malloc(sizeof(struct bstree_node));
root->data = arr[0];
root->left = NULL;
root->right = NULL;
// 创建一个队列,用于存储待插入节点的父节点
struct bstree_node* queue[n];
int front = 0;
int rear = 0;
queue[rear++] = root;
// 遍历数组,依次插入节点
for (int i = 1; i < n; i++) {
// 取出队列中的父节点
struct bstree_node* parent = queue[front++];
// 创建左子节点
struct bstree_node* left_child = (struct bstree_node*)malloc(sizeof(struct bstree_node));
left_child->data = arr[i];
left_child->left = NULL;
left_child->right = NULL;
parent->left = left_child;
// 将左子节点入队
queue[rear++] = left_child;
// 判断是否还有剩余节点待插入
if (i + 1 < n) {
// 创建右子节点
struct bstree_node* right_child = (struct bstree_node*)malloc(sizeof(struct bstree_node));
right_child->data = arr[i + 1];
right_child->left = NULL;
right_child->right = NULL;
parent->right = right_child;
// 将右子节点入队
queue[rear++] = right_child;
// 跳过右子节点,继续遍历下一个节点
i++;
}
}
return root;
}
```
请注意,上述代码中的`arr`是一个整型数组,包含了待插入二叉树中的节点值。在函数调用时,需要提供该数组以及数组的长度`n`。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)