学习C++数据结构(树的实现)

在学习数据结构的时候自己纯手打的实现代码,放到平台上来方便自己日后回顾,也可以给其他想学习的朋友一点小小的参考。
(代码思路来源于慕课网的c++数据结构课程)

  • 结点Node.h
#pragma once
#include<iostream>
#include<string>
using namespace std;

//结点
class Node
{
public:
	Node();
	int data;
	int index;
	Node* pLChild;
	Node* pRChild;
	Node* pParent;
	Node* SearchNode(int nodeIndex);
	void DeleteNode();
	void PreorderTaverse();
	void InorderTaverse();			//中序遍历
	void PostorderTaverse();			//后序遍历

};

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::PreorderTaverse()
{
	cout << this->index << "    " << this->data << endl;
	if (this->pLChild != NULL)
	{
		this->pLChild->PreorderTaverse();
	}
	if (this->pRChild != NULL)
	{
		this->pRChild->PreorderTaverse();
	}
}

void Node::InorderTaverse()
{
	if (this->pLChild != NULL)
	{
		this->pLChild->InorderTaverse();
	}

	cout << this->index << "    " << this->data << endl;
	
	if (this->pRChild != NULL)
	{
		this->pRChild->InorderTaverse();
	}
}

void Node::PostorderTaverse()
{
	cout << this->index << "    " << this->data << endl;

	if (this->pLChild != NULL)
	{
		this->pLChild->PostorderTaverse();
	}

	if (this->pRChild != NULL)
	{
		this->pRChild->PostorderTaverse();
	}
}
  • 树的链表实现 Tree_LinkedList.h
#pragma once
#include<iostream>
#include<stdlib.h>
#include "Node.h"
using namespace std;

//链表实现
class Tree_LinkedList
{
public:
	Tree_LinkedList();
	~Tree_LinkedList();
	Node* SearchNode(int nodeIndex);
	bool AddNode(int nodeIndex, int direction, Node* pNode);
	bool DeleteNode(int nodeIndex, Node* pNode);
	void PreorderTaverse();			//先序遍历
	void InorderTaverse();			//中序遍历
	void PostorderTaverse();			//后序遍历
private:
	Node* m_pRoot;
};

Tree_LinkedList::Tree_LinkedList()
{
	m_pRoot = new Node();
}

Tree_LinkedList::~Tree_LinkedList()
{
	m_pRoot->DeleteNode();
}

Node* Tree_LinkedList::SearchNode(int nodeIndex)
{
	return m_pRoot->SearchNode(nodeIndex);
}

bool Tree_LinkedList::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->data = pNode->data;
	node->index = pNode->index;
	node->pParent = temp;
	if (direction == 0)
	{
		temp->pLChild = node;
	}
	if (direction == 1)
	{
		temp->pRChild = node;
	}
	return true;
}

bool Tree_LinkedList::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_LinkedList::PreorderTaverse()
{
	m_pRoot->PreorderTaverse();
}

void Tree_LinkedList::InorderTaverse()
{
	m_pRoot->InorderTaverse();
}

void Tree_LinkedList::PostorderTaverse()
{
	m_pRoot->PostorderTaverse();
}
  • 树的数组实现 Tree.h
#pragma once
#include<string>
#include<iostream>
using namespace std;

//数组实现
class Tree
{
public:
	Tree(int size, int *pRoot);			//创建树
	~Tree();			//销毁树
	int *SearchNode(int nodeIndex);			//根据索引寻找结点
	bool AddNode(int nodeIndex, int direction, int* pNode);			//添加结点
	bool DeleteNode(int nodeIndex, int* pNode);			//删除结点
	void TreeTaverse();			//遍历树
private:
	int* m_pTree;
	int m_iSize;
};

Tree::Tree(int size, int* pRoot)
{
	m_pTree = new int[size];
	m_iSize = size;
	for (int i = 0; i < size; i++)
	{
		m_pTree[i] = 0;
	}
	m_pTree[0] = *pRoot;
}

Tree::~Tree()
{
	delete[]m_pTree;
	m_pTree = NULL;
}

int* Tree::SearchNode(int nodeIndex)
{
	if (nodeIndex < 0 || nodeIndex >= m_iSize)
	{
		return false;
	}
	if (0 == m_pTree[nodeIndex])
	{
		return false;
	}
	return &m_pTree[nodeIndex];
}

bool Tree::AddNode(int nodeIndex, int direction, int* pNode)			//direction==0时插入左结点;direction==1时插入右结点
{
	if (nodeIndex < 0 || nodeIndex >= m_iSize)
	{
		return false;
	}
	if (0 == m_pTree[nodeIndex])
	{
		return false;
	}
	//direction = 0 ==左;direction = 1 ==有右
	if (0 == direction)
	{
		if (nodeIndex * 2 + 1 >= m_iSize)
		{
			return false;
		}
		if (0 != m_pTree[nodeIndex * 2 + 1])
		{
			return false;
		}
		m_pTree[nodeIndex * 2 + 1] = *pNode;
	}
	if (1 == direction)
	{
		if (nodeIndex * 2 + 2 >= m_iSize)
		{
			return false;
		}
		if (0 != m_pTree[nodeIndex * 2 + 2])
		{
			return false;
		}
		m_pTree[nodeIndex * 2 + 2] = *pNode;
	}
	return true;
}

bool Tree::DeleteNode(int nodeIndex, int* pNode)
{
	if (nodeIndex < 0 || nodeIndex >= m_iSize)
	{
		return false;
	}
	if (0 == m_pTree[nodeIndex])
	{
		return false;
	}
	*pNode = m_pTree[nodeIndex];
	m_pTree[nodeIndex] = 0;
	return true;
}

void Tree::TreeTaverse()
{
	for (int i = 0; i < m_iSize; i++)
	{
		cout << m_pTree[i] << " ";
	}
}
  • 主函数demo.cpp
#include<stdlib.h>
#include<string>
#include<iostream>
#include "Tree.h"
#include "Tree-LinkedList.h"
using namespace std;

int main()
{
	/*int root = 1;
	Tree* pTree = new Tree(10, &root);
	int node1 = 2;
	int node2 = 3;
	pTree->AddNode(0, 0, &node1);
	pTree->AddNode(0, 1, &node2);

	int node3 = 4;
	int node4 = 5;
	pTree->AddNode(1, 0, &node3);
	pTree->AddNode(1, 1, &node4);

	int node5 = 6;
	int node6 = 7;
	pTree->AddNode(2, 0, &node5);
	pTree->AddNode(2, 1, &node6);

	int node = 0;
	pTree->DeleteNode(6, &node);
	pTree->TreeTaverse();
	int *p = pTree->SearchNode(2);
	cout << endl << *p << endl;
	delete pTree;
	pTree = NULL;*/

	Node* node0 = new Node();
	node0->index = 0;
	node0->data = 0;

	Node *node1 = new Node();
	node1->index = 1;
	node1->data = 1;

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

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

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

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

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

	Tree_LinkedList* tree = new Tree_LinkedList;
	tree->AddNode(0, 0, node0);
	tree->AddNode(0, 1, node1);

	tree->AddNode(1, 0, node2);
	tree->AddNode(1, 1, node3);

	tree->AddNode(2, 0, node4);
	tree->AddNode(2, 1, node5);

	tree->AddNode(3, 0, node6);

	tree->PreorderTaverse();

	delete tree;
	tree = NULL;

	return 0;
}
发布了36 篇原创文章 · 获赞 43 · 访问量 1825

猜你喜欢

转载自blog.csdn.net/weixin_45224869/article/details/104517579