最近做leetcode 上有好多题目是关于链表和二叉树的相关操作,于是回顾了一下数据结构,实现了链表和二叉树相应的操作.
#ifndef NODE_H
#define NODE_H
class Node
{
public:
Node();
Node *SearchNode(int nodeIndex);
void DeleteNode();
void PreorderTraversal();
void InorderTraversal();
void PostorderTracersal();
int index;
int data;
Node *pLChild;
Node *pRChild;
Node *pParent;
};
#endif
#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::PostorderTracersal(){
if(this->pLChild != NULL)
this->pLChild->PostorderTracersal();
if(this->pRChild != NULL)
this->pRChild->PostorderTracersal();
cout<<this->index<<" "<<this->data<<endl;
}
#ifndef TREE1_H
#define TREE1_H
#include "Node.h"
#include <stdlib.h>
class Tree1
{
public:
Tree1();
~Tree1();
Node *SearchNode(int nodeIndex);
bool AddNode(int nodeIndex, int direction, Node *pNode);
bool DeleteNode(int nodeIndex, Node *pNode);
void PreorderTraversal();
void InorderTraversal();
void PostorderTracersal();
private:
Node *m_pRoot;
};
#endif
#include "Tree1.h"
#include "Node.h"
#include <iostream>
Tree1::Tree1(){
m_pRoot = new Node();
}
Tree1::~Tree1(){
DeleteNode(0,NULL);
m_pRoot->DeleteNode();
}
Node *Tree1::SearchNode(int nodeIndex){
return m_pRoot->SearchNode(nodeIndex);
}
bool Tree1::AddNode(int nodeIndex, int direction, Node *pNode){
Node *temp = SearchNode(nodeIndex);
if(temp == NULL)
return false;
Node *newNode = new Node();
if(newNode == NULL)
return false;
newNode->index = pNode->index;
newNode->data = pNode->data;
newNode->pParent = temp;
if(direction == 0){
temp->pLChild = newNode;
}
if(direction == 1){
temp->pRChild = newNode;
}
return true;
}
bool Tree1::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 Tree1::PreorderTraversal(){
m_pRoot->PreorderTraversal();
}
void Tree1::InorderTraversal(){
m_pRoot->InorderTraversal();
}
void Tree1::PostorderTracersal(){
m_pRoot->PostorderTracersal();
}
#include <iostream>
#include "Tree1.h"
#include <stdlib.h>
using namespace std;
int main(){
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;
Tree1 *tree1 = new Tree1();
tree1->AddNode(0,0,node1);
tree1->AddNode(0,1,node2);
tree1->AddNode(1,0,node3);
tree1->AddNode(1,1,node4);
tree1->AddNode(2,0,node5);
tree1->AddNode(2,1,node6);
tree1->PreorderTraversal();
tree1->InorderTraversal();
tree1->PostorderTracersal();
tree1->DeleteNode(2,NULL);
tree1->PreorderTraversal();
return 0;
}