C++ 二叉树(创建,遍历,查找,插入,删除)『菜鸟版』

//二叉树操作

#include "stdafx.h"
#include<iostream>
using namespace std; 
int num_visit=0;//记录输出元素个数
struct tnode{   
    public: int data;
    public: tnode *left,*right;
	
    tnode(){}
	tnode(int item,tnode *p,tnode *q):data(item),left(p),right(q){}
 };
typedef tnode *Tnode;
//生明函数
int menu();//菜单
void main_menu(Tnode root,int n);//主菜单
Tnode createTree();//创建二叉树
int visit(int t);//访问节点
void showTree(Tnode , int );//遍历
int level_showTree(Tnode root);//先序便遍历二叉树
int inorder_showTree(Tnode root);//中序遍历二叉树
int postorder_showTree(Tnode root);//后序遍历二叉树
bool search(Tnode root,int search_num);//查找二叉树
Tnode insert(Tnode root,int insert_num);//插入
void d_delete(Tnode,int);//删除
void free(Tnode root);//free
//主函数
void main()
{
	Tnode root = createTree();	
	//菜单
	int n = menu();
	//退出
	if(n ==0 ){
	  return;
	}
	//遍历
	if(n<=3)
	{
	  showTree(root,n);
      main_menu(root,n);
	}
	//查找
	 if(n == 4){
	     cout<<"请输入要查找的结点的元素值:";
		 cin>>n;
		 search(root,n);
		 main_menu(root,n);
	  }
	//插入
    if(n == 5){
	    cout<<"请输入要插入的节点的元素值:";
		cin>>n;
		insert(root,n);
		main_menu(root,n);
	  }
	//删除
	if(n == 6){
	   cout<<"请输入要删除的结点元素值:";
	   cin>>n;
	   d_delete(root,n);
	   n = menu();
	   main_menu(root,n);
	}
}
//菜单
int menu(){
	  cout<<"        菜单:输入0退出."<<endl<<endl;
	  cout<<"一 遍历:"<<endl;
	  cout<<"1.先序遍历 请输入1.";
	  cout<<endl<<"2.中序遍历 请输入2.";
	  cout<<endl<<"3.后序遍历 请输入3.";
	  cout<<endl<<endl<<"二 查找:"<<endl;
	  cout<<"4.查找 请输入4."<<endl;
	  cout<<endl<<"三 插入:"<<endl;
	  cout<<"5.插入 请输入5."<<endl;
	  cout<<endl<<"四 删除:"<<endl;
	  cout<<"6.删除 请输入6."<<endl<<endl;
	  cout<<"请输入:";
	  int n;
	  cin>>n;
	  return n;
}
//主菜单
void main_menu(Tnode root,int n){
	while(n>=0){
		n = menu();

		if(n==0){
		return;
		}
		if(n<=3)
		{
		  showTree(root,n);
		}
		//查找
		 if(n == 4){
			 cout<<"请输入要查找的结点的元素值:";
			 cin>>n;
			 cout<<search(root,n);
		  }
		//插入
		if(n == 5){
			cout<<"请输入要插入的节点的元素值:";
			cin>>n;
			insert(root,n);
		  }
		if(n == 6){
		   cout<<"请输入要删除的结点元素值:";
		   cin>>n;
		   d_delete(root,n);
		}
	}
}
//创建二叉树
Tnode createTree(){
   Tnode root,p,q;
   
   //左子树
   p = new tnode(96,NULL,NULL);
   //右子树
   q = new tnode(104,NULL,NULL);
   //根节点
   root = new tnode(100,p,q);
   return root;
}
//访问节点
int visit(int t){
	   cout<<t<<" ";
	   if(++num_visit%5 == 0)
		   cout<<endl;
	 return 1;
}
//遍历
void showTree(Tnode root,int n){
  
  switch(n){
      case 1: 
		   cout<<"先序遍历:"<<endl;
		   level_showTree(root);
	    break;
	  case 2: 
		  cout<<"中序遍历:"<<endl;
		  inorder_showTree(root);
		break;
	  case 3:
		  cout<<"后序遍历:"<<endl;
		  postorder_showTree(root);
		break;
	  default:
		  return; 
  }
  cout<<endl;
}
//先序遍历二叉树
int level_showTree(Tnode root){
	if(root){
	    visit(root->data);
		level_showTree(root->left);
		level_showTree(root->right);
	}else
		return 0;
}
//中序遍历
int inorder_showTree(Tnode root){
	if(root){
		inorder_showTree(root->left);
		visit(root->data);
		inorder_showTree(root->right);
	}else
		return 0;
}
//后序遍历
int postorder_showTree(Tnode root){
	if(root){
		postorder_showTree(root->left);
		postorder_showTree(root->right);
		visit(root->data);
	}else 
		return 0;
}
//查找
bool search(Tnode root,int search_num){
	if(root){
	   if(root->data == search_num){
			cout<<root->data <<"=="<< search_num<<endl;
		    return true;
	   }else{
		   search(root->left,search_num);
		   search(root->right,search_num);
		 }  
	}else{
		return false;
	}
}
//插入
Tnode insert(Tnode root,int insert_num){
	if(search(root,insert_num)){
    	cout<<"此节点存在!"<<endl;
		return NULL;
	}else
		if(root == NULL){
			root = new tnode(insert_num,NULL,NULL);
		}else{
		  if(root->data>insert_num){
			  root->left = insert(root->left,insert_num);	
		  }else{
			  root->right = insert(root->right,insert_num);
		  }
		}
		return root;
}
//删除
void d_delete(Tnode root,int d_num){
	if(root){
		if(root->left->data == d_num){
			root->left = NULL;
			free(root->left);
		}else if(root->right->data == d_num){
			root->right = NULL;
			free(root->right);
		}else{
			d_delete(root->left,d_num);
			d_delete(root->right,d_num);
		}
	}else
		return;
}
//free
void free(Tnode root){
	if(root){
		free(root->left);
		free(root->right);
		delete root;
	}
}

    原文作者:二叉查找树
    原文地址: https://blog.csdn.net/smallearth/article/details/8245219
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞