在上一篇博客最后的笔记里面记载了二叉树的遍历三种情况:前序、中序和后序遍历
在这一篇博客里面主要是通过链表的方式来实现的
详细看代码:
1、Node.h
#ifndef NODE_H
#define NODE_H
class Node
{
public:
Node();
Node *SearchNode(int nodeIndex); //查找
void DeleteNode();
void PreorderTraversal();
void InorderTraversal();
void PostorderTraversal();
int index;
int data;
Node *pLChild;
Node *pRChild;
Node *pParent;
};
#endif
2、Node.cpp
#include "Node.h"
#include <iostream>
using namespace std;
Node::Node()
{
index = 0;
data = 0;
pLChild = NULL;
pRChild = NULL;
pParent = NULL;
}
Node *Node::SearchNode(int nodeIndex)
{
if (this->index == nodeIndex)
{
return this;
}
Node *temp = NULL;
if (this->pLChild != NULL)
{
if (this->pLChild->index == nodeIndex)
{
return this->pLChild;
}
else
{
temp = this->pLChild->SearchNode(nodeIndex);
if (temp != NULL)
{
return temp;
}
}
}
if (this->pRChild != NULL)
{
if (this->pRChild->index == nodeIndex)
{
return this->pRChild;
}
else
{
temp = this->pRChild->SearchNode(nodeIndex);
if (temp != NULL)
{
return temp;
}
}
}
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->index << " " << this->data << endl;
if (this->pLChild != NULL)
{
this->pLChild->PreorderTraversal();
}
if (this->pRChild != NULL)
{
this->pRChild->PreorderTraversal();
}
}
void Node::InorderTraversal()
{
if (this->pLChild != NULL)
{
this->pLChild->InorderTraversal();
}
cout << this->index << " " << this->data << endl;
if (this->pRChild != NULL)
{
this->pRChild->InorderTraversal();
}
}
void Node::PostorderTraversal()
{
if (this->pLChild != NULL)
{
this->pLChild->PostorderTraversal();
}
if (this->pRChild != NULL)
{
this->pRChild->PostorderTraversal();
}
cout << this->index << " " << this->data << endl;
}
3、Tree.h
#include "Node.h"
#include <stdio.h>
class Tree
{
public:
Tree(); //创建树
~Tree(); //销毁树
Node *SearchNode(int nodeIndex); //搜索结点
bool AddNode(int nodeIndex, int direction, Node *pNode); //添加结点
bool DeleteNode(int nodeIndex, Node *pNode); //删除结点
void PreorderTraversal(); //前序遍历
void InorderTraversal(); //中序遍历
void PostorderTraversal(); //后序遍历
private:
Node *m_pRoot;
};
4、Tree.cpp
#include "Tree.h"
Tree::Tree()
{
m_pRoot = new Node();
}
Tree::~Tree()
{
// DeleteNode(0, NULL);
m_pRoot->DeleteNode();
}
Node *Tree::SearchNode(int nodeIndex)
{
return m_pRoot->SearchNode(nodeIndex);
}
bool Tree::AddNode(int nodeIndex, int direction, Node *pNode)
{
Node *temp = SearchNode(nodeIndex);
if (temp == NULL)
{
return false;
}
Node *node = new Node();
if (node == NULL)
{
return false;
}
node->index = pNode->index;
node->data = pNode->data;
node->pParent = temp;
if (direction == 0)
{
temp->pLChild = node;
}
if (direction == 1)
{
temp->pRChild = node;
}
return true;
}
bool Tree::DeleteNode(int nodeIndex, Node *pNode)
{
Node *temp = SearchNode(nodeIndex);
if (temp == NULL)
{
return false;
}
if (pNode != NULL)
{
pNode->data = temp->data;
}
temp->DeleteNode();
return true;
}
void Tree::PreorderTraversal()
{
m_pRoot->PreorderTraversal();
}
void Tree::InorderTraversal()
{
m_pRoot->InorderTraversal();
}
void Tree::PostorderTraversal()
{
m_pRoot->PostorderTraversal();
}
5、nodeTreeDemo.h
#include <stdio.h>
#include <iostream>
#include "Tree.h"
using namespace std;
/*
(0)
5(1) 8(2)
2(3) 6(4) 9(5) 7(6)
索引
前序遍历: 0134256
中序遍历: 3140526
后序遍历: 3415620
*/
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char** argv)
{
Node *node1 = new Node();
node1->index = 1;
node1->data = 5;
Node *node2 = new Node();
node2->index = 2;
node2->data = 8;
Node *node3 = new Node();
node3->index = 3;
node3->data = 2;
Node *node4 = new Node();
node4->index = 4;
node4->data = 6;
Node *node5 = new Node();
node5->index = 5;
node5->data = 9;
Node *node6 = new Node();
node6->index = 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->PreorderTraversal();
cout << endl << "中序遍历是:" << endl;
tree->InorderTraversal();
tree->DeleteNode(6, NULL);
tree->DeleteNode(5, NULL);
tree->DeleteNode(2, NULL);
cout << endl << "后序遍历是:" << endl;
tree->PostorderTraversal();
return 0;
}
好啦就到这里咯~~