Skip to content

Commit 70600eb

Browse files
author
lazylzg
committed
更新
1 parent fe464a8 commit 70600eb

File tree

3 files changed

+15
-16
lines changed

3 files changed

+15
-16
lines changed

第10章数据结构二/10.1二叉树/10-1二叉树遍历.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@ struct TreeNode {
88
TreeNode(char c):data(c),leftChild(NULL),rightChild(NULL) {};
99
};
1010

11-
TreeNode* Build(int &position,string str) {
11+
TreeNode *Build(int &position,string str) {
1212
char c=str[position++];
1313
if(c=='#') {
1414
return NULL;
1515
}
16+
//创建新节点
1617
TreeNode *root=new TreeNode(c);
18+
//创建左子树
1719
root->leftChild=Build(position,str);
20+
//创建右子树
1821
root->rightChild=Build(position,str);
1922
return root;
2023
}
@@ -36,7 +39,6 @@ int main() {
3639
InOrder(root);
3740
printf("\n");
3841
}
39-
4042
return 0;
4143
}
4244

第10章数据结构二/10.1二叉树/10-2二叉树遍历.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@ struct TreeNode {
88
TreeNode(char c):data(c),leftChild(NULL),rightChild(NULL) {};
99
};
1010

11-
TreeNode* Build(string str1,string str2) {
11+
TreeNode *Build(string str1,string str2) {
1212
if(str1.size()==0) {
1313
return NULL;
1414
}
15-
//µ±Ç°×Ö·û
1615
char c=str1[0];
17-
TreeNode *root=new TreeNode(c);
18-
//ѰÕÒÇзֵã
19-
int position=str2.find(c);
20-
root->leftChild=Build(str1.substr(1,position),str2.substr(0,position));
21-
root->rightChild=Build(str1.substr(position+1),str2.substr(position+1));
16+
//创建新节点
17+
TreeNode *root = new TreeNode(c);
18+
//寻找切分点
19+
int pos=str2.find(c);
20+
root->leftChild=Build(str1.substr(1,pos),str2.substr(0,pos));
21+
root->rightChild=Build(str1.substr(pos+1),str2.substr(pos+1));
2222
return root;
2323
}
2424

第10章数据结构二/10.2二叉排序树/10-3二叉排序树.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,11 @@ struct TreeNode {
1111

1212
TreeNode* Insert(TreeNode*root,int data,int father) {
1313
if(root==NULL) {
14-
//创建新节点
1514
root=new TreeNode(data);
1615
printf("%d\n",father);
1716
} else if(data<root->data) {
18-
//插入左子树
1917
root->leftChild=Insert(root->leftChild,data,root->data);
2018
} else {
21-
//插入右子树
2219
root->rightChild=Insert(root->rightChild,data,root->data);
2320
}
2421
return root;
@@ -27,11 +24,11 @@ TreeNode* Insert(TreeNode*root,int data,int father) {
2724
int main() {
2825
int n;
2926
while(scanf("%d",&n)!=EOF) {
30-
TreeNode* root=NULL;
31-
int data;
27+
TreeNode *root=NULL;
3228
for(int i=0; i<n; i++) {
33-
scanf("%d",&data);
34-
root=Insert(root,data,-1);
29+
int x;
30+
scanf("%d",&x);
31+
root=Insert(root,x,-1);
3532
}
3633
}
3734
return 0;

0 commit comments

Comments
 (0)