二叉树构建(查找,增加,删除,前、中、后序遍历)

//链式节点为基本单元,辅助后续二叉树类实现,“Node.h”为节点类的实现代码

#include "Node.h"
#include<iostream>
using namespace std;
Node::Node():data(0),index(0),pfather(NULL),pLchild(NULL),pRchild(NULL){}
Node::Node(int _index,int _data):data(_data),index(_index),pfather(NULL),pLchild(NULL),pRchild(NULL){}
Node* Node::SearchNode(int N_index){
	Node *temp=NULL;
	if(this->index==N_index){
		return this;
	}
	if(this->pLchild!=NULL){
	  if(this->pLchild->index==N_index){
		return this->pLchild;}
	  else{ 
		temp=this->pLchild->SearchNode(N_index);
		if(temp!=NULL) return temp; 
	  }
	}
	if(this->pRchild!=NULL){
	  if(this->pRchild->index==N_index){
	 	return this->pRchild;}
	  else{ 
		temp=this->pRchild->SearchNode(N_index);
		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->pfather!=NULL) {
		if(this->pfather->pLchild==this) this->pfather->pLchild=NULL;
		if(this->pfather->pRchild==this) this->pfather->pRchild=NULL;
	}
	delete this;
}
void Node::Pre_traversal(){
	cout<<this->index<<"  "<<this->data<<endl;
	if(this->pLchild!=NULL) 
		this->pLchild->Pre_traversal();
	if(this->pRchild!=NULL) 
		this->pRchild->Pre_traversal();
}
void Node::In_traversal(){
	if(this->pLchild!=NULL) 
		this->pLchild->In_traversal();
	cout<<this->index<<"  "<<this->data<<endl;	
	if(this->pRchild!=NULL) 
		this->pRchild->In_traversal();
}
void Node::Lat_traversal(){
	if(this->pLchild!=NULL) 
		this->pLchild->Lat_traversal();
	if(this->pRchild!=NULL) 
		this->pRchild->Lat_traversal();
	cout<<this->index<<"  "<<this->data<<endl;	
}

//二叉树类的实现


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

Tree::Tree(){
	m_pNode=new Node();
}
Tree::~Tree(){
	DeleteNode(0,NULL);//m_pNode->DeleteNode();
}
Node* Tree::SearchNode(int s_index){
	return m_pNode->SearchNode(s_index);
}
bool Tree::AddNode(int node_index,int direction,Node* pNode){
	Node* temp=SearchNode(node_index);
	if(temp==NULL) {return false;}
	Node* node=new Node();
	if(node==NULL) {return false;}
	node->data=pNode->data;
	node->index=pNode->index;
	node->pfather=temp;
	if(direction==0) {temp->pLchild=node;}
	if(direction==1) {temp->pRchild=node;}
	return true;
}
bool Tree::DeleteNode(int node_index,Node* pNode){
	Node* temp=SearchNode(node_index);
	if(temp==NULL) {return false;}
	if(pNode!=NULL) {pNode->data=temp->data;}
	temp->DeleteNode();
	return true;
}
void Tree::Pre_traversal(){
	m_pNode->Pre_traversal();//前序遍历,头,左,右

}
void Tree::In_traversal(){
	m_pNode->In_traversal();//中序遍历,左,头,右
}
void Tree::Lat_traversal(){
	m_pNode->Lat_traversal();//后序遍历,左,右,头
}

//主函数调用

#include "Node.h"
#include "Tree.h"
#include<iostream>
#include<stdlib.h>
using namespace std;
int main(){
	Tree * N_0=new Tree();
	Node* N_1=new Node(1,1);
	Node* N_2=new Node(2,2);
	Node* N_3=new Node(3,7);
	Node* N_4=new Node(4,5);
	Node* N_5=new Node(5,8);
	Node* N_6=new Node(6,9);
	Node* N_7=new Node(7,8);
	Node* N_8=new Node(8,9);
	Node* N_9=new Node(9,8);
	Node* N_10=new Node(10,9);


	N_0->AddNode(0,0,N_1);
	N_0->AddNode(0,1,N_2);
	N_0->AddNode(1,0,N_3);
	N_0->AddNode(1,1,N_4);
	N_0->AddNode(2,0,N_5);
	N_0->AddNode(2,1,N_6);
	N_0->AddNode(3,0,N_7);
	N_0->AddNode(3,1,N_8);
	N_0->AddNode(4,0,N_9);
	N_0->AddNode(4,1,N_10);
	N_0->Pre_traversal();
	N_0->In_traversal();
	N_0->Lat_traversal();	
	system("pause");
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值