-
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
1 parent
db7e909
commit 22b8006
Showing
3 changed files
with
163 additions
and
4 deletions.
There are no files selected for viewing
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,142 @@ | ||
#include <iostream> | ||
#include <string> | ||
#include <queue> | ||
|
||
using namespace std; | ||
|
||
struct Node | ||
{ | ||
Node(string name): name(name) {} | ||
string name; | ||
Node *left; | ||
Node *right; | ||
}; | ||
|
||
|
||
void preOrderPrint(Node *node) | ||
{ | ||
if( node == NULL ) | ||
return; | ||
|
||
std::cout << node->name << " "; | ||
|
||
preOrderPrint(node->left); | ||
preOrderPrint(node->right); | ||
} | ||
|
||
void inOrderPrint(Node *node) | ||
{ | ||
if ( node == NULL) | ||
return; | ||
|
||
inOrderPrint(node->left); | ||
std::cout << node->name << " "; | ||
inOrderPrint(node->right); | ||
} | ||
|
||
|
||
void postOrderPrint(Node *node) | ||
{ | ||
if ( node == NULL) | ||
return; | ||
|
||
postOrderPrint(node->left); | ||
postOrderPrint(node->right); | ||
std::cout << node->name << " "; | ||
} | ||
|
||
void levelOrderPrint1(Node *node) | ||
{ | ||
if( node == NULL ) | ||
return; | ||
|
||
std::queue<Node *> q; | ||
q.push(node); | ||
|
||
Node *current = NULL; | ||
while( !q.empty() ) | ||
{ | ||
current = q.front(); | ||
std::cout << current->name << " "; | ||
if( current->left != NULL ) | ||
q.push(current->left); | ||
if( current->right != NULL ) | ||
q.push(current->right); | ||
|
||
q.pop(); | ||
} | ||
} | ||
|
||
void levelOrderPrint2(Node *node) | ||
{ | ||
if( node == NULL ) | ||
return; | ||
|
||
std::vector<Node *>v1; | ||
v1.push_back(node); | ||
|
||
Node *current = NULL; | ||
while( v1.size() != 0) | ||
{ | ||
current = v1.front(); | ||
v1.erase(v1.begin()); | ||
|
||
std::cout << current->name << " "; | ||
if( current->left != NULL ) | ||
v1.push_back(current->left); | ||
if( current->right != NULL ) | ||
v1.push_back(current->right); | ||
} | ||
|
||
// int start = 0; | ||
// while( start < v1.size() ) | ||
// { | ||
// current = v1[start++]; | ||
// std::cout << current->name << " "; | ||
// if( current->left != NULL ) | ||
// v1.push_back(current->left); | ||
// if( current->right != NULL ) | ||
// v1.push_back(current->right); | ||
// } | ||
|
||
// for( auto iter = v1.begin(); iter != v1.end(); iter++) | ||
// { | ||
// std::cout << (*iter)->name << " "; | ||
// } | ||
} | ||
|
||
|
||
|
||
int main(int argc, char const *argv[]) | ||
{ | ||
Node A("A"); | ||
Node B("B"); | ||
Node C("C"); | ||
Node D("D"); | ||
Node E("E"); | ||
Node F("F"); | ||
Node G("G"); | ||
|
||
A.left = &B; | ||
A.right = &C; | ||
B.left = &D; | ||
B.right = &E; | ||
C.left = &F; | ||
C.right = &G; | ||
|
||
|
||
preOrderPrint(&A); | ||
std::cout << std::endl; | ||
inOrderPrint(&A); | ||
std::cout << std::endl; | ||
postOrderPrint(&A); | ||
std::cout << std::endl; | ||
levelOrderPrint1(&A); | ||
std::cout << std::endl; | ||
levelOrderPrint2(&A); | ||
|
||
|
||
|
||
|
||
return 0; | ||
} |
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,15 @@ | ||
# 基本内容 | ||
|
||
树是一种非线性结构,树主要由几点构成 | ||
|
||
1. 树上的每个元素叫”节点“ | ||
2. 节点之间的连接关系,表示为父子节点关系,节点之间的连接叫边 | ||
3. 根节点没有父节点,没有子节点的节点叫为叶子节点 | ||
4. 同一个父节点的子节点称为兄弟节点 | ||
|
||
树的概念: | ||
+ 节点的层:从根节点到该节点的层数,根节点为 1 层 | ||
+ 节点的高度:节点到最长叶子节点边的个数 | ||
+ 节点的深度:根节点到该节点经过边的个数 | ||
+ 节点的度:子节点的个数 | ||
+ 树的高度: 顶点到最长叶子节点经过的边的个数 |
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