《数据结构之树篇》--二叉树的链表实现

--Node的头文件:

#pragma once
#ifndef NODE_H_
#define NODE_H_

#include<iostream>
using namespace std;

class Node
{
public:
	Node();
	Node *SearchNode(int nodeIndex);
	void DeleteNode();
	void PreorderTraverse();
	void InorderTraverse();
	void PostorderTraverse();
	int index;
	int data;
	Node *pLChild;
	Node *pRChild;
	Node *pParent;
};



#endif // !NODE_H_

--Node的CPP文件:

#include "Node.h"

Node::Node()
{
	m_idata = 0;
	m_iIndex = 0;
	m_pLChild = NULL;
	m_pRChild = NULL;
	m_pParent = NULL;
}


Node * Node::SearchNode(int index)
{
	if (this->m_iIndex == index)
		return this;
	
	if (this->m_pLChild != NULL)
	{
		if (this->m_pLChild->m_iIndex==index)
		{
			return this->m_pLChild;
		}
		Node *temp = m_pLChild->SearchNode(index);
		if (temp != NULL)
			return temp;
	}
	if (this->m_pRChild != NULL)
	{
		if (this->m_pRChild->m_iIndex == index)
		{
			return this->m_pRChild;
		}
		Node *temp = m_pRChild->SearchNode(index);
		if (temp != NULL)
			return temp;
	}
	return NULL;
}

void Node::DeleteNode()
{
	if (this->m_pLChild != NULL)
	{
		this->m_pLChild->DeleteNode();
	}
	if (this->m_pRChild != NULL)
	{
		this->m_pRChild->DeleteNode();
	}
	if (this->m_pParent != NULL)
	{
		if (this->m_pParent->m_pLChild == this)
			this->m_pParent->m_pLChild = NULL;
		if (this->m_pParent->m_pRChild == this)
			this->m_pParent->m_pRChild = NULL;
	}
	delete this;
}

void Node::PreTraverse()
{
	cout << this->m_idata << "(" << this->m_iIndex << ")" << endl;
	if (this->m_pLChild != NULL)
	{
		this->m_pLChild->PreTraverse();
	}
	if (this->m_pRChild != NULL)
	{
		this->m_pRChild->PreTraverse();
	}
}

void Node::InTraverse()
{
	if (this->m_pLChild != NULL)
	{
		this->m_pLChild->PreTraverse();
	}

	cout << this->m_idata << "(" << this->m_iIndex << ")" << endl;

	if (this->m_pRChild != NULL)
	{
		this->m_pRChild->PreTraverse();
	}
}

void Node::ProTraverse()
{
	if (this->m_pLChild != NULL)
	{
		this->m_pLChild->PreTraverse();
	}
	if (this->m_pRChild != NULL)
	{
		this->m_pRChild->PreTraverse();
	}
	cout << this->m_idata << "(" << this->m_iIndex << ")" << endl;
}


--Tree的头文件:

#pragma once
#ifndef TREE_H_
#define TREE_H_

#include"Node.h"
#include<stdlib.h>
using namespace std;

class Tree
{
public:
	Tree();
	~Tree();
	Node* SearchNode(int index);
	bool AddNode(int index, bool direction, Node *pNode);
	bool DeleteNode(int index, Node *pNode);
	void PreTraverse();
	void InTraverse();
	void ProTraverse();
private:
	Node* m_pRoot;
};


#endif // !TREE_H_

--Tree的CPP文件:


#include "Tree.h"

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

Tree::~Tree()
{
	DeleteNode(0, NULL);
}

Node * Tree::SearchNode(int index)
{
	return m_pRoot->SearchNode(index);
}

bool Tree::AddNode(int index, bool direction, Node * pNode)
{
	Node *temp = SearchNode(index);
	if (temp == NULL)
		return false;

	Node *node = new Node;
	node->m_idata = pNode->m_idata;
	node->m_iIndex = pNode->m_iIndex;
	node->m_pParent = temp;

	if (direction == true)
	{
		if (temp->m_pLChild != NULL)
			return false;
		temp->m_pLChild = node;
	}
	else
	{
		if (temp->m_pRChild != NULL)
			return false;
		temp->m_pRChild = node;
	}
	return true;
}

bool Tree::DeleteNode(int index, Node * pNode)
{
	Node *temp = SearchNode(index);
	if (temp == NULL)
	{
		return false;
	}
	if (pNode != NULL)
	{
		pNode->m_idata = temp->m_idata;
	}
	temp->DeleteNode();
	return true;
}

void Tree::PreTraverse()
{
	m_pRoot->PreTraverse();
}

void Tree::InTraverse()
{
	m_pRoot->InTraverse();
}

void Tree::ProTraverse()
{
	m_pRoot->ProTraverse();
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值