1树
#pragma once
#include<iostream>
using namespace std;
class Node
{
public:
Node()
{
data = 0;
index = 0;
plChild = nullptr;
prChild = nullptr;
pParent = nullptr;
}
Node* SearchNode(int nodeindex)
{
Node* temp = nullptr;
if (this->index == nodeindex)
{
return this;
}
if (this->plChild != nullptr)
{
if (this->plChild->index == nodeindex)
return this->plChild;
else
{
temp = this->plChild->SearchNode(nodeindex);
if (temp != nullptr)
return temp;
}
}
if (this->prChild != nullptr)
{
if (this->prChild->index == nodeindex)
return this->prChild;
else
{
temp = this->prChild->SearchNode(nodeindex);
if (temp != nullptr)
return temp;
}
}
return nullptr;
}
void DeleteNode()
{
if (this->plChild != nullptr)
this->plChild->DeleteNode();
if (this->prChild != nullptr)
this->prChild->DeleteNode();
if (this->pParent != nullptr)
{
if (this->pParent->plChild == this)
this->pParent->plChild = nullptr;
if (this->pParent->prChild == this)
this->pParent->prChild = nullptr;
}
delete this;
}
void PreordTraversal()
{
cout << this->data << "#" << index<<endl;
if (this->plChild != nullptr)
{
this->plChild->PreordTraversal();
}
if (this->prChild != nullptr)
{
this->prChild->PreordTraversal();
}
}
int data;
int index;
Node* plChild;
Node* prChild;
Node* pParent;
};
class Tree
{
public:
Tree()
{
mpRoot = new Node();
}
~Tree()
{
DeleteNode(0, nullptr);
}
Node* SearchNode(int nodeindex)
{
return mpRoot->SearchNode(nodeindex);
}
bool AddNode(int nodeindex, int direction, Node* pNode)
{
Node* temp = SearchNode(nodeindex);
if (temp == nullptr)
return false;
Node* node = new Node();
if (node == nullptr)
return false;
node->data = pNode->data;
node->index = pNode->index;
if (direction == 0)
{
temp->plChild = node;
node->pParent=temp;
}
if (direction == 1)
{
temp->prChild = node;
node->pParent=temp;
}
return true;
}
bool DeleteNode(int nodeindex, Node* pNode)
{
Node* temp = SearchNode(nodeindex);
if (temp == nullptr)
return false;
if (pNode != nullptr)
{
pNode->data = temp->data;
}
temp->DeleteNode();
return true;
}
void PreordTraversal()
{
mpRoot->PreordTraversal();
}
void InordTraversal();
void PostTraversal();
private:
Node* mpRoot;
};
2环形队列
#pragma once
#ifndef MYQUEUE_H
#define MYQUEUE_H
class MyQueue
{
public:
MyQueue(int queueCapacity);
~MyQueue();
void ClearQueue();
bool QueueEmpty() const;
bool QueueFull() const;
int QueueLength() const;
bool EnQueue(int element);
bool DeQueue(int& element);
void QueueTraverse();
private:
int* m_pQqueue;
int m_iQueueLen;
int m_iQueueCapacity;
int m_iHead;
int m_iTail;
};
#endif
#include "cirqueue.h"
#include <iostream>
using namespace std;
MyQueue::MyQueue(int queueCapacity)
{
m_iQueueCapacity = queueCapacity;
ClearQueue();
m_pQqueue = new int[m_iQueueCapacity];
}
MyQueue::~MyQueue()
{
delete[]m_pQqueue;
}
void MyQueue::ClearQueue()
{
m_iHead = 0;
m_iTail = 0;
m_iQueueLen = 0;
}
bool MyQueue::QueueEmpty() const
{
if (m_iQueueLen == 0)
{
return true;
}
else
return false;
}
bool MyQueue::QueueFull() const
{
if (m_iQueueLen == m_iQueueCapacity)
{
return true;
}
else
return false;
}
int MyQueue::QueueLength() const
{
cout << m_iQueueLen << endl;
return m_iQueueLen;
}
bool MyQueue::EnQueue(int element)
{
if (!QueueFull())
{
m_pQqueue[m_iTail] = element;
m_iQueueLen++;
m_iTail++;
m_iTail = m_iTail % m_iQueueCapacity;
return true;
}
else
{
cout << "此队列已满,无法添加!" << endl;
return false;
}
}
bool MyQueue::DeQueue(int& element)
{
if (!QueueEmpty())
{
element = m_pQqueue[m_iHead];
cout << element << "退队列成功!" << endl;
m_iQueueLen--;
m_iHead++;
m_iHead = m_iHead % m_iQueueCapacity;
return true;
}
else
{
cout << "此队列已空,无法删除!" << endl;
return false;
}
}
void MyQueue::QueueTraverse()
{
if (!QueueEmpty())
{
int i;
for (i = m_iHead; i < m_iQueueLen + m_iHead; i++)
{
cout << "第" << i % m_iQueueCapacity << "元素,值为" << m_pQqueue[i % m_iQueueCapacity] << endl;
}
}
else
cout << "此队列已空,无法遍历!" << endl;
}
3图
#pragma once
class Node
{
public:
Node(char data=0)
{
mData = 0;
mIsVisited = false;
}
~Node();
char mData;
bool mIsVisited;
};
#pragma once
#include<iostream>
#include<vector>
#include"Node.h"
using namespace std;
class cMap
{
public:
cMap(int capacity)
{
mCatacity = capacity;
mNodeCount = 0;
pNodeArray = new Node[mCatacity];
pMatrix = new int[mCatacity*mCatacity];
for (int i = 0; i <= mCatacity * mCatacity; i++)
pMatrix[i] = 0;
}
~cMap()
{
delete[]pNodeArray;
delete[]pMatrix;
}
bool Addnode(Node* pnode)
{
if (pnode == nullptr)
return false;
pNodeArray[mNodeCount].mData = pnode->mData;
mNodeCount++;
return true;
}
void Resetnode()
{
for (int i = 0; i <= mNodeCount; i++)
pNodeArray[i] = false;
}
bool setVTMFDG(int row, int col, int val = 1)
{
if (row < 0 || row >= mCatacity)
return false;
if (col < 0 || col >= mCatacity)
return false;
pMatrix[row * mCatacity + col] = val;
return true;
}
bool setVTMFUDG(int row, int col, int val = 1)
{
if (row < 0 || row >= mCatacity)
return false;
if (col < 0 || col >= mCatacity)
return false;
pMatrix[row * mCatacity + col] = val;
pMatrix[col * mCatacity + row] = val;
return true;
}
void printfMatrix()
{
for(int i=0;i<=mCatacity;i++)
{
for (int j = 0; j <= mCatacity; j++)
cout << pMatrix[i * mCatacity + j] << " ";
cout << endl;
}
}
void depthTraverse(int nodeIndex = 0)
{
int value = 0;
cout << pNodeArray[nodeIndex].mData << " ";
pNodeArray[nodeIndex].mIsVisited = true;
for (int i = 0; i <= mCatacity; i++)
{
getValue(nodeIndex, i, value);
if (value == 1)
{
if (pNodeArray[i].mIsVisited)
{
continue;
}
else
{
depthTraverse(i);
}
}
else
{
continue;
}
}
}
void breadthTraverse(int nodeIndex)
{
cout << pNodeArray[nodeIndex].mData << " ";
pNodeArray[nodeIndex].mIsVisited = true;
vector<int>curVec;
curVec.push_back(nodeIndex);
breadthTI(curVec);
}
private:
bool getValue(int row,int col,int&val)
{
if (row < 0 || row >= mCatacity)
return false;
if (col < 0 || col >= mCatacity)
return false;
val = pMatrix[row * mCatacity + col];
return true;
}
void breadthTI(vector<int>preVec)
{
int value = 0;
vector<int>curVec;
for (int j = 0; j < preVec.size; j++)
{
for (int i = 0; i < mCatacity; i++)
{
getValue(preVec[j], i, value);
if (value != 0)
{
if (pNodeArray[i].mIsVisited)
continue;
else
{
cout << pNodeArray[i].mData << " ";
pNodeArray[i].mIsVisited = true;
curVec.push_back(i);
}
}
}
}
if (curVec.size() == 0)
{
return;
}
else
{
breadthTI(curVec);
}
}
private:
int mCatacity;
int mNodeCount;
Node* pNodeArray;
int* pMatrix;
};