您的位置:首页 > 其它

二叉树的相关算法

2017-04-20 18:32 239 查看
二叉树的相关算法 (前序、中序、后序,递归、非递归,广度、深度遍历)

/********************************************************************
* 程序说明:二叉树的相关算法
* 1. 二叉树的前序递归遍历 preOrderRecTra()
* 2. 二叉树的前序非递归遍历 preOrderNonRecTra()
* 3. 二叉树的中序递归遍历 midOrderRecTra()
* 4. 二叉树的中序非递归遍历 midOrderNonRecTra()
* 5. 二叉树的后序递归遍历 postOrderRecTra()
* 6. 二叉树的后序非递归遍历 postOrderNonRecTra()
* 7. 广度优先非递归遍历 bfs()
* 8. 深度优先非递归遍历 dfs()
* author:Raptor_2017
********************************************************************/

#define LOCAL
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<stack>
#include<queue>
using namespace std;

const int MAXLEN = 100;

typedef struct tNode {
int have_value;
int v;
struct tNode *left, *right;
}node;

node *root;

/*
* 申请一个新的节点空间
*/
node* newNode() {
node *u = (node*)malloc(sizeof(node));
if (u == NULL)cout << "申请节点空间失败!" << endl;
else {
u->have_value = 0;
u->left = u->right = NULL;
}
return u;
}

/*
* 添加一个新的节点
* *root: 指向根节点的指针
* v: 节点值
* s: 表示了节点的位置,如LR,表示欲添加的新的节点在:根节点->左子树->右儿子
* 返回值: 0-添加新节点失败, 1-添加新节点成功
*/
int addNode(node* root, int v, char* s) {
int sLen = strlen(s);
if (0 >= sLen) return 0;
node* u = root;
for (int i = 0; i < sLen; i++) {
if ('L' == s[i]) {
if (u->left == NULL) {//若左子树(左儿子)不存在,则创建
u->left = newNode();
if (u->left == NULL) {//检查申请新节点空间是否成功
return 0;
}
}
u = u->left;
}
else if ('R' == s[i]) {
if (u->right == NULL) {
u->right = newNode();
if (u->right == NULL) {//检查申请新节点空间是否成功
return 0;
}
}
u = u->right;
}
else if (')' == s[i]);
else {
cout << "路径标识错误: " << s[i] << endl;
return 0;
}
}
if (u->have_value) {
cout << "该节点已经被赋值过: " << u->v << endl;
return 0;
}
else {
u->v = v;
u->have_value = 1;
return 1;
}
}

/*
* 删除该节点及该节点的子树
*/
void removeNode(node* u) {
if (u == NULL)return;
removeNode(u->left);
removeNode(u->right);
free(u);
return;
}

/*
* 创建一颗树
* (v,LLRR): (节点值,节点路径)
*/
node* createTree() {
node* root = newNode();
if (root == NULL)return NULL;
char s[MAXLEN];
while (scanf("%s", s) == 1) {
if (!strcmp(s, "()"))break;//结束输入节点
else {
int v;
sscanf(&s[1], "%d", &v);
int ret = addNod
4000
e(root, v, strchr(s, ',') + 1);
if (!ret) {
cout << "添加该节点失败! " << s << endl;
return root;
}
}
}
return root;
}

void preOrderRecTra(node* root) {
if (root == NULL) return;
if (root->have_value)cout << root->v << " ";
preOrderRecTra(root->left);
preOrderRecTra(root->right);
}

void preOrderNonRecTra(node* root) {
stack<node*>stk;
stk.push(root);
while (!stk.empty()) {
node* p = stk.top();
if (p) {
if (p->have_value)cout << p->v << " ";
}
stk.pop();
if (p->right) stk.push(p->right);
if (p->left) stk.push(p->left);
}
}

void midOrderRecTra(node* root) {
if (root == NULL)return;
midOrderRecTra(root->left);
if (root->have_value)cout << root->v << " ";
midOrderRecTra(root->right);
}

void midOrderNonRecTra(node* root) {
stack<node*>stk;
stk.push(root);
while (!stk.empty()) {
node* p = stk.top();
while (p) {
stk.push(p->left);
p = stk.top();
}
stk.pop();
if (!stk.empty()) {
p = stk.top();
stk.pop();
if (p->have_value)cout << p->v << " ";
stk.push(p->right);
}
}
}

void postOrderNonRecTra(node* root) {
stack<node*>stk;
stk.push(root);
node *cur, *p = NULL;
while (!stk.empty()) {
cur = stk.top();
if ((cur->left == NULL&&cur->right == NULL) ||
(p != NULL && (cur->left == p || cur->right == p))) {
if (cur->have_value)cout << cur->v << " ";
stk.pop();
p = cur;
}
else {
if (cur->right)stk.push(cur->right);
if (cur->left)stk.push(cur->left);
}
}
}

void postOrderRecTra(node* root) {
if (root == NULL)return;
postOrderRecTra(root->left);
postOrderRecTra(root->right);
if (root->have_value)cout << root->v << " ";
}

void bfs(node* root) {
if (root == NULL)return;
queue<node*>q;
q.push(root);
while (!q.empty()) {
node* tmp = q.front();
q.pop();
if (tmp) {
if (tmp->have_value)cout << tmp->v << " ";
}
if (tmp->left)q.push(tmp->left);
if (tmp->right)q.push(tmp->right);
}
}

void dfs(node* root) {
if (root == NULL)return;
stack<node*>s;
s.push(root);
while (!s.empty()) {
node* tmp = s.top();
s.pop();
if (tmp) {
if (tmp->have_value)cout << tmp->v << " ";
}
if (tmp->right)s.push(tmp->right);
if (tmp->left)s.push(tmp->left);
}
}

int main() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
node* root = createTree();
preOrderRecTra(root);
cout << endl;
midOrderNonRecTra(root);
cout << endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: