数据结构 二叉树

二叉树的递归定义:二叉树要么为空,要么由根节点,左子树(Left Subtree),右子树(Right Subtree)构成。左右两棵子树又分别是一棵二叉树。

每个结点的定义

struct TreeNode{
	ElementType data;
	TreeNode *leftChild;
	TreeNode *rightChild;
}; 

遍历

PreOrder

void PreOrder(TreeNode *root){
	if(root==NULL) return;
	visit(root->data);
	PreOrder(root->leftChild);
	PreOrder(root->rightChild);
	return;
}

InOrder

	if(root==NULL) return;
	InOrder(root->leftChild);
	visit(root->data);
	InOrder(root->rightChild);
	return;
}

PostOrder

void PosOrder(TreeNode *root){
	if(root==NULL) return;
	PosOrder(root->leftChild);
	PosOrder(root->rightChild);
	visit(root->data);
	return;
}

LeverOrder

void LevelOrder(TreeNode *root){
	queue<TreeNode*> myQueue;
	if(root!=NULL){
		myQueue.push(root);
	}
	
	while(!myQueue.empty()){
		TreeNode *current=myQueue.front();
		myQueue.pop();
		visit(current->data);
		if(current->leftChild!=NULL) myQueue.push(current->leftChild);
		if(current->rightChild!=NULL) myQueue.push(current->rightChild);
	}
	return;
}

熟悉下 建树+遍历 的程序

建树 首先结点定义要写一个默认构造函数(初始化data) ,然后Build返回值为TreeNode* ,然后根结点TreeNode *root=new TreeNode( c );

#include<cstdio>
#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;

struct TreeNode{
	char data;
	TreeNode *leftChild;
	TreeNode *rightChild;
	TreeNode(char c):data(c),leftChild(NULL),rightChild(NULL){}//构造函数 
};

TreeNode* Build(int &position,string str){//注意返回值 和 参数 
	char c=str[position++];
	if(c=='#') return NULL;//空树 
	TreeNode *root=new TreeNode(c);//new+构造函数 
	root->leftChild=Build(position,str);
	root->rightChild=Build(position,str);
	return root; 
} 

void InOrder(TreeNode* root){
	if(root==NULL) return;
	
	InOrder(root->leftChild);
	cout<<root->data<<" ";
	InOrder(root->rightChild);
	
	return;
} 

int main(){
	string str;
	while(cin>>str){
		int position=0;
		TreeNode * root=Build(position,str);
		InOrder(root);
		cout<<endl;
	}
}

由前序序列和中序序列构树

#include<cstdio>
#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;

struct TreeNode{
	char data;
	TreeNode *leftChild;
	TreeNode *rightChild;
	TreeNode(char c):data(c),leftChild(NULL),rightChild(NULL){} 
};

TreeNode *Build(string a,string b){
	if(a.size()==0) return NULL;
	char c=a[0];
	TreeNode *root=new TreeNode(c);
	int position=b.find(c);
	root->leftChild=Build(a.substr(1,position),b.substr(0,position));
	root->rightChild=Build(a.substr(position+1),b.substr(position+1));
	return root;
} 

void PostOrder(TreeNode *root){
	if(root==NULL) return;
	PostOrder(root->leftChild);
	PostOrder(root->rightChild);
	cout<<root->data;
}

int main(){
	string a,b;
	while(cin>>a>>b){
		TreeNode *root=Build(a,b);
		PostOrder(root);
		cout<<endl;
	}
}

二叉排序(搜索)树 Binary Search Tree在这里插入图片描述

Insert()二叉排序树的建树+输出【父节点】的操作:+参数father

在这里插入图片描述

#include<cstdio>
#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
#include<map>
using namespace std;

struct TreeNode{
	int a;
	TreeNode *leftChild;
	TreeNode *rightChild;
	TreeNode(int n):a(n),leftChild(NULL),rightChild(NULL){};
};

TreeNode * Insert(TreeNode* root,int x,int father){
	if(root==NULL){//插入第一个元素 
		root=new TreeNode(x);
		cout<<father<<endl; 
	}
	else{
		if(x<root->a){
			root->leftChild=Insert(root->leftChild,x,root->a);
		}
		else if(x>root->a){
			root->rightChild=Insert(root->rightChild,x,root->a);
		}
	} 
	return root;
} 

int main(){
	int n;
	while(cin>>n){
		TreeNode *root=NULL;
		for(int i=0;i<n;i++){
			int x;
			cin>>x;
			root=Insert(root,x,-1);
		}
	}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值