数据结构之二叉树

本文介绍了一种二叉树的数据结构实现方法,并提供了链表和二叉树的基本操作,包括节点搜索、删除及三种遍历方式:前序、中序和后序遍历。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近做leetcode 上有好多题目是关于链表和二叉树的相关操作,于是回顾了一下数据结构,实现了链表和二叉树相应的操作.

#ifndef NODE_H
#define NODE_H

class Node
{
public:
	Node();
	Node *SearchNode(int nodeIndex);
	void DeleteNode();
	void PreorderTraversal();
	void InorderTraversal();
	void PostorderTracersal();
	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;
	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::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::PostorderTracersal(){
	if(this->pLChild != NULL)
		this->pLChild->PostorderTracersal();
	if(this->pRChild != NULL)
		this->pRChild->PostorderTracersal();
	cout<<this->index<<" "<<this->data<<endl;


}
#ifndef TREE1_H
#define TREE1_H
#include "Node.h"
#include <stdlib.h>
class Tree1
{
public:
	Tree1();
	~Tree1();
	Node *SearchNode(int nodeIndex);
	bool AddNode(int nodeIndex, int direction, Node *pNode);
	bool DeleteNode(int nodeIndex, Node *pNode);
	void PreorderTraversal();
	void InorderTraversal();
	void PostorderTracersal();
private:
	Node *m_pRoot;
};




#endif

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

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


Tree1::~Tree1(){
	DeleteNode(0,NULL);
	m_pRoot->DeleteNode();

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

}
bool Tree1::AddNode(int nodeIndex, int direction, Node *pNode){
	Node *temp = SearchNode(nodeIndex);
	if(temp == NULL)
		return false;	
	Node *newNode = new Node();
	if(newNode == NULL)
		return false;
	newNode->index = pNode->index;
	newNode->data = pNode->data;
	newNode->pParent = temp;
	if(direction == 0){
		temp->pLChild = newNode;

	}
	if(direction == 1){
		temp->pRChild = newNode;
	}
	return true;

}
bool Tree1::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 Tree1::PreorderTraversal(){
	m_pRoot->PreorderTraversal();

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

}
void Tree1::PostorderTracersal(){
	m_pRoot->PostorderTracersal();

}

#include <iostream>
#include "Tree1.h"
#include <stdlib.h>

using namespace std;

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;

	Tree1 *tree1 = new Tree1();
	tree1->AddNode(0,0,node1);
	tree1->AddNode(0,1,node2);
	tree1->AddNode(1,0,node3);
	tree1->AddNode(1,1,node4);
	tree1->AddNode(2,0,node5);
	tree1->AddNode(2,1,node6);
	tree1->PreorderTraversal();
	tree1->InorderTraversal();
 	tree1->PostorderTracersal();
	tree1->DeleteNode(2,NULL);
	tree1->PreorderTraversal();


	return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值