-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
zhaojiedi
committed
Apr 17, 2022
1 parent
56da8c6
commit feb7af0
Showing
33 changed files
with
559 additions
and
117 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include "stack_link_base_for_tree.c" | ||
#include "queue_link_base_for_tree.c" | ||
|
||
|
||
int TreeIsEmpty(BsTree bt){ | ||
if( bt == NULL){ | ||
return 1 ; | ||
} | ||
return 0; | ||
} | ||
|
||
BsTree CreateBsTree(){ | ||
BsTree bt = (BsTree)malloc(sizeof(struct TreeNode)); | ||
return bt ; | ||
} | ||
|
||
void PreOrderTraversalByRecursive(BsTree bt){ | ||
if (bt){ | ||
printf("%d ", bt->Data); | ||
PreOrderTraversalByRecursive(bt->Left); | ||
PreOrderTraversalByRecursive(bt->Right); | ||
} | ||
} | ||
void InOrderTraversalByRecursive(BsTree bt){ | ||
if (bt){ | ||
InOrderTraversalByRecursive(bt->Left); | ||
printf("%d ", bt->Data); | ||
InOrderTraversalByRecursive(bt->Right); | ||
} | ||
} | ||
void PosOrderTraversalByRecursive(BsTree bt){ | ||
if (bt){ | ||
PosOrderTraversalByRecursive(bt->Left); | ||
PosOrderTraversalByRecursive(bt->Right); | ||
printf("%d ", bt->Data); | ||
} | ||
} | ||
|
||
void PreOrderTraversalByIterate(BsTree bt){ | ||
Stack s = CreateStack(); | ||
BsTree t = bt; | ||
while( t || !StackIsEmpty(s)){ | ||
while(t){ | ||
printf("%d ",t->Data); | ||
Push(s, t); | ||
t= t->Left; | ||
} | ||
if (!StackIsEmpty(s)){ | ||
t= (BsTree) Pop(s); | ||
t=t->Right; | ||
} | ||
} | ||
} | ||
void InOrderTraversalByIterate(BsTree bt){ | ||
Stack s = CreateStack(); | ||
BsTree t = bt; | ||
while( t || !StackIsEmpty(s)){ | ||
while(t){ | ||
Push(s, t); | ||
t= t->Left; | ||
} | ||
if (!StackIsEmpty(s)){ | ||
t= (BsTree) Pop(s); | ||
printf("%d ",t->Data); | ||
t=t->Right; | ||
} | ||
} | ||
} | ||
|
||
void PostOrderTraversalByIterate(BsTree bt){ | ||
Stack s = CreateStack(); | ||
BsTree t = bt; | ||
BsTree prev= NULL ; | ||
while( t || !StackIsEmpty(s)){ | ||
while(t){ | ||
Push(s, t); | ||
t= t->Left; | ||
} | ||
t = (BsTree)Pop(s); | ||
if (t->Right == NULL || t->Right == prev){ | ||
printf("%d ",t->Data); | ||
prev = t ; | ||
t = NULL; | ||
|
||
}else{ | ||
Push(s,t); | ||
t= t->Right; | ||
} | ||
|
||
} | ||
} | ||
void LevelOrderTraversal(BsTree bt){ | ||
|
||
Queue q = CreateQueue(); | ||
BsTree t = bt ; | ||
if (bt == NULL){ | ||
return ; | ||
} | ||
AddQ(q,t); | ||
while(!IsEmptyQ(q)){ | ||
|
||
t= DeleteQ(q); | ||
printf("%d ",t->Data); | ||
if (t->Left){ | ||
AddQ(q,t->Left); | ||
} | ||
if (t->Right){ | ||
AddQ(q,t->Right); | ||
} | ||
|
||
} | ||
|
||
} | ||
BsTree CreateDemoTree(){ | ||
/* | ||
1 | ||
2 3 | ||
5 4 6 | ||
* */ | ||
BsTree bt1 = CreateBsTree(); | ||
bt1->Data = 1 ; | ||
BsTree bt2 = CreateBsTree(); | ||
bt2->Data = 2 ; | ||
BsTree bt3 = CreateBsTree(); | ||
bt3->Data = 3 ; | ||
BsTree bt4 = CreateBsTree(); | ||
bt4->Data = 4 ; | ||
BsTree bt5 = CreateBsTree(); | ||
bt5->Data = 5 ; | ||
BsTree bt6 = CreateBsTree(); | ||
bt6->Data = 6 ; | ||
|
||
bt1->Left = bt2 ; | ||
bt1->Right =bt3 ; | ||
|
||
bt2->Left = bt5 ; | ||
bt2->Right=bt4 ; | ||
|
||
bt3->Left = bt6 ; | ||
return bt1 ; | ||
} | ||
|
||
int main(){ | ||
|
||
BsTree bt =CreateDemoTree(); | ||
printf("PreOrderTraversal\n"); | ||
PreOrderTraversalByRecursive(bt); | ||
printf("\n"); | ||
|
||
printf("InOrderTraversalByRecursive\n"); | ||
InOrderTraversalByRecursive(bt); | ||
printf("\n"); | ||
|
||
printf("PosOrderTraversalByRecursive\n"); | ||
PosOrderTraversalByRecursive(bt); | ||
printf("\n"); | ||
|
||
|
||
printf("PreOrderTraversalByIterate\n"); | ||
PreOrderTraversalByIterate(bt); | ||
printf("\n"); | ||
|
||
printf("InOrderTraversalByIterate\n"); | ||
InOrderTraversalByIterate(bt); | ||
printf("\n"); | ||
|
||
printf("PostOrderTraversalByIterate\n"); | ||
PostOrderTraversalByIterate(bt); | ||
printf("\n"); | ||
|
||
printf("LevelOrderTraversal\n"); | ||
LevelOrderTraversal(bt); | ||
printf("\n"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#pragma once | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
#define ElementType int | ||
typedef struct TreeNode *BsTree ; | ||
struct TreeNode{ | ||
ElementType Data ; | ||
BsTree Left; | ||
BsTree Right; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
|
||
|
||
int main(){ | ||
|
||
} | ||
|
||
|
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file modified
BIN
+0 Bytes
(100%)
source/code/cmake-build-debug/CMakeFiles/3.15.3/CMakeDetermineCompilerABI_C.bin
Binary file not shown.
Binary file modified
BIN
+0 Bytes
(100%)
source/code/cmake-build-debug/CMakeFiles/3.15.3/CompilerIdC/CMakeCCompilerId.o
Binary file not shown.
Oops, something went wrong.