-
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
Showing
8 changed files
with
151 additions
and
3 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,10 @@ | ||
#include"binaryTree.h" | ||
|
||
int main(int argc,char** argv) | ||
{ | ||
binaryTree<int> tree; | ||
tree.create(); | ||
// cout<<tree.root->data<<endl; | ||
tree.preTraverse(tree.root); | ||
|
||
} |
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,138 @@ | ||
// https://github.com/13959582448/dataStructure-C- | ||
// https://blog.csdn.net/milanla | ||
// author:"hch" | ||
|
||
#include<iostream> | ||
using namespace std; | ||
|
||
template<typename T> //树的节点 | ||
class node | ||
{ | ||
public: | ||
T data; //数据 | ||
node<T>* lchild,*rchild; //左右孩子节点 | ||
node(const T& v):data(v),lchild(nullptr),rchild(nullptr){} | ||
node():data(0),lchild(nullptr),rchild(nullptr){} | ||
}; | ||
|
||
template<typename T> //树 | ||
class binaryTree | ||
{ | ||
public: | ||
node<T>* root; //根节点 | ||
public: | ||
binaryTree():root(nullptr){} | ||
|
||
node<T>* create(); //层次创建 | ||
inline void print(node<T>*); //方便打印 | ||
void preTraverse(node<T>*); //先序遍历 | ||
void inTraverse(node<T>*); //中序遍历 | ||
void postTraverse(node<T>*); //后序遍历 | ||
void destroyTree(node<T>*); //销毁树 | ||
|
||
~binaryTree(); | ||
}; | ||
|
||
/* 数据(序号) | ||
312(1) | ||
/ \ | ||
22(2) 33(3) | ||
/ \ / \ | ||
98(4) 43(5) 64(6) 77(7) | ||
/ \ / \ | ||
12(8) 44(9) 0(10) 2(10) | ||
->写法: 312 22 33 98 43 64 77 12 44 0 2 | ||
*/ | ||
|
||
template<typename T> | ||
node<T>* binaryTree<T>::create() | ||
{ | ||
node<T>* (nodes[100])={}; //nodes是数组 存的是每个节点指针 方便查询 | ||
|
||
int i=1; | ||
T value; | ||
while(cin>>value) //输入完成后按回车 再按ctrl+d 即可结束 有的需要按ctrl+z | ||
{ | ||
if(i==1) | ||
{ | ||
this->root=new node<T>(value); | ||
nodes[i-1]=root; | ||
i++; | ||
continue; | ||
} | ||
|
||
if(!value) //value==0 0代表为空 | ||
{ | ||
if(nodes[i/2-1]&&i%2==0) //左孩子 因为按层次遍历所有的左孩子节点的逻辑序号都是偶数 | ||
//判断是否存在该节点的双亲结点 | ||
{ | ||
nodes[i/2-1]->lchild=nullptr; | ||
} | ||
else if(nodes[i/2-1]) nodes[i/2-1]->rchild=nullptr; //双亲结点的左右孩子赋值 | ||
nodes[i-1]=nullptr; | ||
i++; | ||
continue; | ||
} | ||
|
||
node<T>* newnode=new node<T>(value); | ||
nodes[i-1]=newnode; | ||
if(nodes[i/2-1]&&i%2==0) nodes[i/2-1]->lchild=newnode; | ||
else if(nodes[i/2-1]) nodes[i/2-1]->rchild=newnode; | ||
i++; | ||
} | ||
} | ||
|
||
template<typename T> | ||
void binaryTree<T>::print(node<T> *n) //方便打印的函数 | ||
{ | ||
cout<<n->data<<" "; | ||
} | ||
|
||
template<typename T> | ||
void binaryTree<T>::preTraverse(node<T>* n) //先序遍历 | ||
{ | ||
if(n) | ||
{ | ||
print(n); //先打印本节点 | ||
preTraverse(n->lchild); //然后子树 | ||
preTraverse(n->rchild); //最后右子树 | ||
} | ||
} | ||
|
||
template<typename T> | ||
void binaryTree<T>::inTraverse(node<T>* n) //中序遍历 | ||
{ | ||
if(n) | ||
{ | ||
preTraverse(n->lchild); //先左子树 | ||
print(n); //然后本节点 | ||
preTraverse(n->rchild); //最后右子树 | ||
} | ||
} | ||
|
||
template<typename T> | ||
void binaryTree<T>::postTraverse(node<T>* n) //后序遍历 | ||
{ | ||
if(n) | ||
{ | ||
preTraverse(n->rchild); //先右子树 | ||
print(n); //然后本节点 | ||
preTraverse(n->lchild); //最后左子树 | ||
} | ||
} | ||
|
||
template<typename T> | ||
void binaryTree<T>::destroyTree(node<T>* n) //销毁树 | ||
{ | ||
if(n->lchild) destroyTree(n->lchild); //删除左子树 | ||
if(n->rchild) destroyTree(n->rchild); //删除右子树 | ||
delete n; | ||
|
||
} | ||
|
||
template<typename T> | ||
binaryTree<T>::~binaryTree() //析构函数 | ||
{ | ||
destroyTree(root); | ||
} |
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
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
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