이 진 트 리 구현 (링크 구현)

5324 단어 데이터 구조
이 진 트 리 링크 구현
node.h
#pragma once
#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 // !NODE_H_


node.cpp
#include "Node.h"
#include 
using namespace std;
Node::Node()
{
	index = 0;
	data = 0;
	pLChild = nullptr;
	pRChild = nullptr;
	pParent = nullptr;
}

Node * Node::SearchNode(int nodeIndex)
{
	if (this->index == nodeIndex)
	{
		return this;
	}
	Node *temp = NULL;
	if (this->pLChild)
	{
		if (this->pLChild->index == nodeIndex) {
			return this->pLChild;
		}
		else {
			temp=this->pLChild->SearchNode(nodeIndex);
			if (temp) //                 
			{
				return temp;
			}
		}
	}
	if (this->pRChild)
	{
		if (this->pRChild->index == nodeIndex) {
			return this->pRChild;
		}
		else {
			temp=this->pRChild->SearchNode(nodeIndex);
			return temp;
		
		}
	}
	return nullptr;
}

void Node::DeleteNode()
{
	//         
	if (this->pLChild)
	{
		this->pLChild->DeleteNode();
	}
	if (this->pRChild)
	{
		this->pRChild->DeleteNode();
	}
	if (this->pParent)
	{//              null
		if (this->pParent->pLChild == this)
		{
			this->pParent->pLChild = nullptr;
		}
		if (this->pParent->pRChild == this)
		{
			this->pParent->pRChild = nullptr;
		}
	}
	delete this;
}

void Node::PreorderTraversal()
{
	cout << this->index<data << endl;
	if (this->pLChild)
	{
		this->pLChild->PreorderTraversal();
	}
	if (this->pRChild)
	{
		this->pRChild->PreorderTraversal();
	}

}

void Node::InorderTraversal()
{
	if (this->pLChild)
	{
		this->pLChild->InorderTraversal();
	}
	cout << this->index << ":" << this->data << endl;
	if (this->pRChild)
	{
		this->pRChild->InorderTraversal();
	}
}

void Node::PostorderTraversal()
{

	if (this->pLChild)
	{
		this->pLChild->PostorderTraversal();
	}
	if (this->pRChild)
	{
		this->pRChild->PostorderTraversal();
	}
	cout << this->index << ":" << this->data << endl;
}



Tree.h
#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 // !TREE_H_
#pragma once


tree.cpp
#include "Tree.h"

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

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

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

bool Tree::AddNode(int nodeIndex, int direction, Node * pNode)
{
	Node *temp = m_pRoot->SearchNode(nodeIndex);
	if (!temp) 
	{
		return false;
	}
	Node *node = new Node();
	if (!node)
	{
		return false;
	}
	node->data = pNode->data;
	node->index = pNode->index;
	node->pParent = temp;  //     

	if (direction == 0)
	{
		temp->pLChild = node;
	}
	else 
	{
		temp->pRChild = node;
	}
	return true;
}

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

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

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

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


demo.cpp
#include 
#include 
#include "Tree.h"
#include "Node.h"
using namespace std;
/*
   (    )
        
1.       
2.       
3.          
4.       
	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 PreorderTraversal();
	void InorderTraversal();
	void PostorderTraversal();  //  
 0   5 8  2697      

    :0134256
    :3140526
    :3415620
*/
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();
	system("pause");
	return 0;
}

좋은 웹페이지 즐겨찾기