数据结构——二叉树的实现

1.建立Node节点,利用Node节点来实现相关的方法:节点赋值、节点删除、节点搜索、节点遍历。利用递归来实现相关具体方法。

2.Tree类中,利用根节点指针m_pRoot来实现具体的从根节点进行的操作,例如:节点赋值、节点删除、节点搜索、节点遍历。

3.具体代码如下:

3.1Node节点代码:

#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
#include"Node.h"
#include<iostream>
using namespace std;
Node::Node()
{
	index = 0;
	data = 0;
	Node *pLchild = NULL;
	Node *pRchild = NULL;
	Node *pParent = NULL;
}
Node *Node::SearchNode(int nodeIndex)//递归,返回Node类型,重载Tree类中同类名方法
{
	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 (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.2 Tree具体代码:

#ifndef TREE_H_
#define TREE_H_
#include"Node.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;

};
#endif
#include"Tree.h"
#include<iostream>
#include"Node.h"
using namespace std;

Tree::Tree()
{
	m_pRoot = new Node();
}
Tree::~Tree()
{
	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();
	node->data = pNode->data;//指定Node各数据
	node->index = pNode->index;
	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();
}

3.3示例:

#include"Node.h"
#include"Tree.h"
#include<iostream>
int main()
{
	Tree *tree = new Tree();
	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->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);
	//tree->DeleteNode(6,NULL);
	//tree->DeleteNode(5, NULL);
	tree->DeleteNode(2, NULL);
	tree->PostorderTraversal();
	delete tree;
	system("pause");
	return 0;
}

结果:

猜你喜欢

转载自blog.csdn.net/feiyanjia/article/details/81087039