Skip to content

Commit

Permalink
second
Browse files Browse the repository at this point in the history
  • Loading branch information
hch001 committed Jan 18, 2020
1 parent 113f3ad commit 9ee76ce
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 3 deletions.
Binary file added 二叉树/a.out
Binary file not shown.
10 changes: 10 additions & 0 deletions 二叉树/binaryTree.cpp
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);

}
138 changes: 138 additions & 0 deletions 二叉树/binaryTree.h
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);
}
2 changes: 1 addition & 1 deletion 栈/stack.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// https://github.com/13959582448
// https://github.com/13959582448/dataStructure-C-
// https://blog.csdn.net/milanla
// author:"hch"

Expand Down
2 changes: 1 addition & 1 deletion 链表/LinkList.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// https://github.com/13959582448
// https://github.com/13959582448/dataStructure-C-
// https://blog.csdn.net/milanla
// author:"hch"

Expand Down
Binary file removed 链表/a.out
Binary file not shown.
Binary file removed 队列/a.out
Binary file not shown.
2 changes: 1 addition & 1 deletion 队列/queue.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// https://github.com/13959582448
// https://github.com/13959582448/dataStructure-C-
// https://blog.csdn.net/milanla
// author:"hch"

Expand Down

0 comments on commit 9ee76ce

Please sign in to comment.