C++ 链表实现二叉树

Node.h

#pragma once
class Node
{
public:
	int index;
	int data;
	Node *pLChild;
	Node *pRChild;
	Node *pParent;
	Node();
	Node* SearchNode(int nodeIndex);
	void DeleteNode();
	void PreorderTraversal();
	void InorderTraversal();
	void PostoerderTraversal();
};

查找结点,先看这个节点是否等于目标节点,然后递归看左右

删除,先递归删除左右子数,然后修改Parent的后继指向,最后在自杀

遍历就是,递归的三种遍历方式

Node.cpp

#include "Node.h"
#include<iostream>

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

void Node::PostoerderTraversal()
{
	if (this->pLChild != NULL)
	{
		this->pLChild->PostoerderTraversal();
	}
	if (this->pRChild != NULL)
	{
		this->pRChild->PostoerderTraversal();
	}
	std::cout << this->index << " " << this->data << std::endl;
}

Tree.h

放一个根节点,以及前面的所以操作

#pragma once
#include"Node.h"
class Tree
{
private:
	Node * m_pRoot;

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 PostoerderTraversal();
};

构造函数构造一个根,析构删除根节点

Tree.cpp

#include "Tree.h"
#include<cstdio>

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->index = pNode->index;
	node->data = pNode->data;
	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 == 0)
		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::PostoerderTraversal()
{
	
	m_pRoot->PostoerderTraversal();
		
}

最后是一个测试函数

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

/*
        (0)
   5(1)           8(2)

2(3)    6(4)   9(5)     7(6)

*/
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;

	Tree *tree = new Tree();

	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->PreorderTraversal();
	//tree->InorderTraversal();
	//tree->PostoerderTraversal();

	//tree->DeleteNode(6, NULL);
	tree->DeleteNode(2, NULL);
	tree->PreorderTraversal();

    return 0;
}

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值