C++二叉树的链表实现,包括递归和非递归实现
2017-07-06 09:37
351 查看
在上文的博客中已经写了二叉树的实现,那么为什么还要继续写这篇博文呢?答案是因为上文简单的写了二叉树的递归遍历,相信大家肯定刺激度不够,所以这篇博文中添加了非递归遍历,满足大家的好奇心,学习数据结构主要是蕴含在其中的思路,思路对了,什么都很简单的,不要局限在这种方式中,希望这篇博文会给大家带来一些收获。
Node.h
Tree.h
Node.cpp
Tree.cpp
Demo.cpp
在Node.cpp中我们定义了节点的非递归遍历,即树的非递归遍历,希望大家仔细查看node.cpp中的三个非递归实现的算法部分,进一步提高自己的实力,加油,向着美好出发!
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中的三个非递归实现的算法部分,进一步提高自己的实力,加油,向着美好出发!
相关文章推荐
- C++ 实现求二叉树的深度及便利(递归与非递归算法)
- 二叉树的遍历(C++非递归实现)
- 二叉树建立与遍历递归操作c++实现
- C++递归与非递归实现链表的反转
- C++实现二叉树的建立和三种递归遍历
- 二叉树的遍历:前序,中序,后序,层序--包括递归和非递归实现(转)
- C++ 实现求二叉树的深度及遍历(递归与非递归算法)
- 二叉树的遍历 递归 非递归 C++ 实现
- 二叉树的遍历:前序,中序,后序,层序--包括递归和非递归实现
- 二叉树实现(包括遍历等各种操作,递归与非递归)
- java语言实现的二叉树的各种操作(包括递归与非递归遍历二叉树,求二叉树的高度,节点总数,叶子节点等)
- (以实现)c++非递归层次二叉树
- C/C++面试题(三) 推断二叉树、快速排序递归实现、递归判断数组递增
- 二叉树的链表实现(递归)
- 二叉树的遍历:前序,中序,后序,层序--包括递归和非递归实现(转)
- 二叉树(前中后序递归非递归遍历,层次遍历,C++实现)
- 二叉树的遍历:前序,中序,后序,层序--包括递归和非递归实现
- 二叉树的遍历:前序,中序,后序,层序--包括递归和非递归实现
- C++ 递归实现反向打印一个链表(引入wrapper function的概念)
- C++实现二叉树的递归遍历与非递归遍历(先序、中序、后序、层序)