二叉树的构建

本文详细介绍了如何使用C++语言构建一个简单的二叉树,包括节点定义、构造函数实现及树的构建过程。

二叉树(Binary Tree)是将数据按一定的分支关系组织起来的结构,保存数据的基本节点(Node)最多有两个子树,通常子树的根被称之为“左子树”(Left subtree)和“右子树”(Right subtree)。 

图1 一个简单的二叉树

由二叉树的性质可知,一个节点最基本的三要素为:保存节点的值以及指向左右子树的指针。

1 typedef struct binTree{
2     int data;
3     struct binTree* left;
4     struct binTree* right;
5 }BinNode, *BinTree;
View Code

构造如图1所示的二叉树,如下代码。

 1 /*
 2  *                     root
 3  *            left1               right1
 4  *     left2       right2                right3
 5  *            left3     right4     left4        right5
 6  */
 7 
 8 void createBinTree(BinTree& root){
 9     BinTree left1 = new BinNode;
10     BinTree right1 = new BinNode;
11     left1->data = 2;
12     right1->data = 5;
13     root->left = left1;
14     root->right = right1;    //Construct the first layer.
15 
16     BinTree left2 = new BinNode;
17     BinTree right2 = new BinNode;
18     left2->data = 1;
19     right2->data = 8;
20     left1->left = left2;
21     left1->right = right2;
22     BinTree right3 = new BinNode;
23     right3->data = 9;
24     right1->right = right3;        //Construct the second layer
25 
26     BinTree left3 = new BinNode;
27     left3->data = 6;
28     right2->left = left3;
29     BinTree right4 = new BinNode;
30     right4->data = 11;
31     right2->right = right4;
32 
33     BinTree left4 = new BinNode;
34     left4->data = 7;
35     right3->left = left4;
36     BinTree right5 = new BinNode;
37     right5->data = 10;
38     right3->right = right5;
39 }
View Code
 

 完整代码:

 1 #include<iostream>
 2 using namespace std;
 3 
 4 typedef struct binTree{
 5     int data;
 6     struct binTree* left;
 7     struct binTree* right;
 8 }BinNode, *BinTree;
 9 
10 /*
11  *                     root
12  *            left1               right1
13  *     left2       right2                right3
14  *            left3     right4     left4        right5
15  */
16 
17 void createBinTree(BinTree& root){
18     BinTree left1 = new BinNode;
19     BinTree right1 = new BinNode;
20     left1->data = 2;
21     right1->data = 5;
22     root->left = left1;
23     root->right = right1;    //Construct the first layer.
24 
25     BinTree left2 = new BinNode;
26     BinTree right2 = new BinNode;
27     left2->data = 1;
28     right2->data = 8;
29     left1->left = left2;
30     left1->right = right2;
31     BinTree right3 = new BinNode;
32     right3->data = 9;
33     right1->right = right3;        //Construct the second layer
34 
35     BinTree left3 = new BinNode;
36     left3->data = 6;
37     right2->left = left3;
38     BinTree right4 = new BinNode;
39     right4->data = 11;
40     right2->right = right4;
41 
42     BinTree left4 = new BinNode;
43     left4->data = 7;
44     right3->left = left4;
45     BinTree right5 = new BinNode;
46     right5->data = 10;
47     right3->right = right5;
48 }
49 
50 int main(void){
51     BinTree  root = new BinNode;
52     root->data = 0;
53     createBinTree(root);
54         return 0;
55 }
View Code

 

 

转载于:https://www.cnblogs.com/nigang/p/3564877.html

### 二叉树构建方法 二叉树是一种每个节点最多有两个子节点的树结构,通常称为“左子节点”和“右子节点”。构建二叉树时,需要定义节点类和树类,以便管理节点的插入和树的结构。常见的实现方式是通过层次遍历序列来构建二叉树,因为该序列可以唯一地确定一棵二叉树[^1]。 #### 节点类的实现 首先定义一个节点类 `Node`,用于表示树的节点。每个节点包含一个值以及指向左子节点和右子节点的引用。 ```python class Node: def __init__(self, value): self.value = value self.left = None self.right = None ``` #### 树类的实现 接下来定义一个树类 `Tree`,用于管理二叉树构建和节点插入操作。可以使用队列来辅助层次遍历插入节点。 ```python from collections import deque class Tree: def __init__(self): self.root = None def add(self, value): node = Node(value) if not self.root: self.root = node return queue = deque([self.root]) while queue: current = queue.popleft() if not current.left: current.left = node return else: queue.append(current.left) if not current.right: current.right = node return else: queue.append(current.right) ``` #### 构建示例 使用上述类,可以按照层次遍历的方式依次插入节点来构建一棵二叉树。 ```python # 创建树并插入节点 tree = Tree() values = [1, 2, 3, 4, 5, 6, 7] for val in values: tree.add(val) ``` 在这个例子中,树的根节点为 `1`,其左子节点为 `2`,右子节点为 `3`,依此类推,形成一个完整的二叉树结构。 #### 遍历二叉树 为了验证树的构建是否正确,可以通过层次遍历打印树的节点顺序。 ```python def level_order_traversal(root): if not root: return [] result = [] queue = deque([root]) while queue: current = queue.popleft() result.append(current.value) if current.left: queue.append(current.left) if current.right: queue.append(current.right) return result # 打印层次遍历结果 print(level_order_traversal(tree.root)) # 输出 [1, 2, 3, 4, 5, 6, 7] ``` ### 二叉树的其他构建方式 除了层次遍历外,也可以通过先序、中序、后序遍历的结果来构建二叉树,但层次遍历是最直观且最容易实现的方法[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值