您的位置:首页 > 编程语言 > C语言/C++

C++二叉树的链表实现,包括递归和非递归实现

2017-07-06 09:37 351 查看
在上文的博客中已经写了二叉树的实现,那么为什么还要继续写这篇博文呢?答案是因为上文简单的写了二叉树的递归遍历,相信大家肯定刺激度不够,所以这篇博文中添加了非递归遍历,满足大家的好奇心,学习数据结构主要是蕴含在其中的思路,思路对了,什么都很简单的,不要局限在这种方式中,希望这篇博文会给大家带来一些收获。

Node.h

#ifndef NODE_H
#define NODE_H

class Node{
public:
Node();
int pos;
int data;
Node* pLchild;
Node* pRchild;
Node* pParent;
Node* searchNode(int pos);
void deleteNode();
void PreorderTraversal();
void PreorderTraversalNonRecur();
void InorderTraversal();
void InorderTraversalNonRecur();
void PostorderTraversal();
void PostorderTraversalNonRecur();
};
#endif


Tree.h

#ifndef TREE_H
#define TREE_H
#include "Node.h"
class Tree{
public:
Tree();
~Tree();
Node* searchIndex(int nodeIndex);
bool addNode(int nodeIndex, int direction, Node* pNode);
bool delNode(int nodeIndex, Node *pNode);
void PreorderTraversalNonRecur();
void PreorderTraversal();
void InorderTraversal();
void InorderTraversalNonRecur();
void PostorderTraversal();
void PostorderTraversalNonRecur();
private:
Node* m_pRoot;
};

#endif


Node.cpp

#include "Node.h"
#include <iostream>
#include <stack>
using namespace std;
Node::Node(){
pos = 0;
data = 0;
pLchild = NULL;
pRchild = NULL;
pParent = NULL;
}
Node* Node::searchNode(int pos){
if (this->pos == pos){
return this;
}
Node*t = NULL;
if (this->pLchild != NULL){
if (this->pLchild->pos == pos){
return this->pLchild;
}
else{
t = this->pLchild->searchNode(pos);
if (t != NULL){
return t;
}
}
}
if (this->pRchild != NULL){
if (this->pRchild->pos == pos){
return this->pRchild;
}
else{
t = this->pRchild->searchNode(pos);
if (t != NULL){
return t;
}
}
}
return NULL;
}

//Ϊɭô
void Node::deleteNode(){
if (this->pLchild != NULL){
this->pLchild->deleteNode();
}
if (this->pRchild != NULL){
this->pRchild->deleteNode();
}
if (this->pParent != NULL){
if (this->pParent->pLchild == this){
this->pParent->pLchild = NULL;
}
if (this->pParent->pRchild == this){
this->pParent->pRchild = NULL;
}
}
delete this;
}
void Node::PreorderTraversal(){
/*cout << this->pos << " " << this->data << endl;
if (this->pLchild != NULL){
this->pLchild->PreorderTraversal();
}if (this->pRchild != NULL){
this->pRchild->PreorderTraversal();
}*/
if (this == NULL){
return;
}
else{
cout << this->pos << "  " << this->data << endl;
this->pLchild->PreorderTraversal();
this->pRchild->PreorderTraversal();
}
}
void Node::PreorderTraversalNonRecur(){
stack<Node*> stack;
Node *node = this;
while (node != NULL || !stack.empty()){
while (node != NULL){
cout << node->pos << " " << node->data << endl;
stack.push(node);
node = node->pLchild;
}
if (!stack.empty()){
node = stack.top();
stack.pop();
node = node->pRchild;
}
}
}
void Node::InorderTraversal(){
/*if (this->pLchild != NULL){
this->pLchild->InorderTraversal();
}
cout << this->pos << " " << this->data<<endl;
if (this->pRchild != NULL){
this->pRchild->InorderTraversal();
}*/
if (this == NULL){
return;
}
else{
this->pLchild->InorderTraversal();
cout << this->pos << "  " << this->data << endl;
this->pRchild->InorderTraversal();
}
}
void Node::InorderTraversalNonRecur(){
stack<Node*> stack;
Node* node = this;
while (node != NULL || (!stack.empty())){
while (node != NULL){
stack.push(node);
node = node->pLchild;
}
if (!stack.empty()){
node = stack.top();
cout << node->pos << " " << node->data << endl;
stack.pop();
node = node->pRchild;
}
}
}
void Node::PostorderTraversal(){
/*if (this->pLchild != NULL){
this->pLchild->PostorderTraversal();
}
if (this->pRchild != NULL){
this->pRchild->PostorderTraversal();
}
cout << this->pos << " " << this->data<<endl;*/
if (this == NULL){
return;
}
else{
this->pLchild->PostorderTraversal();
this->pRchild->PostorderTraversal();
cout << this->pos << "  " << this->data << endl;
}
}
void Node::PostorderTraversalNonRecur(){
Node *cur = this;
Node* pre = NULL;
stack<Node*> stack;
stack.push(cur);
while (!stack.empty()){
cur = stack.top();
if (cur->pLchild == NULL&&cur->pRchild == NULL || (pre != NULL&&(cur->pLchild==pre||cur->pRchild==pre))){
cout << cur->pos << " " << cur->data << endl;
stack.pop();
pre = cur;
}
else{
if (cur->pRchild != NULL){
stack.push(cur->pRchild);
}if (cur->pLchild != NULL){
stack.push(cur->pLchild);
}
}
}
}


Tree.cpp

#include "Tree.h"
#inc
14711
lude <iostream>
using namespace std;
Tree::Tree(){
m_pRoot = new Node();
}
Tree::~Tree(){
//delNode(0, NULL);
m_pRoot->deleteNode();
}
Node* Tree::searchIndex(int nodeIndex){
return m_pRoot->searchNode(nodeIndex);
}
bool Tree::delNode(int nodeIndex, Node *pNode){
Node* temp = searchIndex(nodeIndex);
if (temp == NULL){
return false;
}
pNode->pos = temp->pos;
pNode->data = temp->data;

temp->deleteNode();
return true;
}
bool Tree::addNode(int nodeIndex, int direction, Node* pNode){
Node* node = searchIndex(nodeIndex);
if (node == NULL){
cout << "这里出错了" << endl;
return false;
}
Node *temp = new Node();
if (temp == NULL){
return false;
}
temp->data = pNode->data;
//temp->pos = pNode->pos;
temp->pParent = node;
if (direction == 0){
if (node->pLchild != NULL){
return false;
}
cout << "第" << nodeIndex * 2 + 1 << "个节点  " <<temp->data<< endl;
temp->pos = nodeIndex * 2 + 1;
node->pLchild = temp;
return true;
}
if (direction == 1){
if (node->pRchild != NULL){
return false;
}
cout << "第" << nodeIndex * 2 + 2 << "个节点  " <<temp->data<< endl;
temp->pos = nodeIndex * 2 + 2;
node->pRchild = temp;
return true;
}
return true;
}
void Tree::PreorderTraversal(){
m_pRoot->PreorderTraversal();
}
void Tree::PreorderTraversalNonRecur(){
m_pRoot->PreorderTraversalNonRecur();
}
void Tree::InorderTraversal(){
m_pRoot->InorderTraversal();
}
void Tree::InorderTraversalNonRecur(){
m_pRoot->InorderTraversalNonRecur();
}
void Tree::PostorderTraversal(){
m_pRoot->PostorderTraversal();
}
void Tree::PostorderTraversalNonRecur(){
m_pRoot->PostorderTraversalNonRecur();
}


Demo.cpp

#include <iostream>
#include "Tree.h"
using namespace std;
int main(){
Node* node1 = new Node();
node1->pos = 1;
node1->data = 5;

Node* node2 = new Node();
node2->pos = 2;
node2->data = 8;

Node* node3 = new Node();
node3->pos = 3;
node3->data = 2;

Node* node4 = new Node();
node4->pos = 4;
node4->data = 6;

Node* node5 = new Node();
node5->pos = 5;
node5->data = 9;

Node* node6 = new Node();
node6->pos = 6;
node6->data = 7;

Tree* tree = new Tree();
tree->addNode(0, 0, node1);
tree->addNode(0, 1, node2);

tree->addNode(1, 0, node3);
tree->addNode(1, 1, node4);

tree->addNode(2, 0, node5);
tree->addNode(2, 1, node6);
cout << "前序遍历:" << endl;
tree->PreorderTraversalNonRecur();
cout << "中序遍历:" << endl;
tree->InorderTraversalNonRecur();
cout << "后序遍历:" << endl;
tree->PostorderTraversalNonRecur();
Node *temp = new Node();
tree->delNode(5, temp);
cout << "结果:"<<temp->pos << " " << temp->data << endl;
tree->InorderTraversal();
return 0;
}


在Node.cpp中我们定义了节点的非递归遍历,即树的非递归遍历,希望大家仔细查看node.cpp中的三个非递归实现的算法部分,进一步提高自己的实力,加油,向着美好出发!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: