二叉树的基本运算
#include<cstdio>
using namespace std;
using namespace std;
struct node {
typename data; //数据域
node* lchild;
node* rchild;
};
typename data; //数据域
node* lchild;
node* rchild;
};
node* root = NULL;
//新建节点
node*newNode(int v) {
node* Node = nwe node;
Node->data=v; //v为权值
Node->lchild=Node->rchild=NULL;
return Node;
}
node*newNode(int v) {
node* Node = nwe node;
Node->data=v; //v为权值
Node->lchild=Node->rchild=NULL;
return Node;
}
//二叉树的查找、修改
void search(node *root,int x,int newdata) {
if(root == NULL) {
return ;
}
if(root->data == x) {
root->data = newdata;
}
search(root->lchild,x,newdata);
search(root->rchild,x,newdata);
}
void search(node *root,int x,int newdata) {
if(root == NULL) {
return ;
}
if(root->data == x) {
root->data = newdata;
}
search(root->lchild,x,newdata);
search(root->rchild,x,newdata);
}
//二叉树节点的插入
void insert(node* &root,int x) { //&root为引用
if(root == NULL) {
root = newNode(x);
return ;
}
if(/*介于二叉树的性质,插在左子树*/)
{
insert(root->lchild,x);
}
else
{
insert(root->rchild,x);
}
}
void insert(node* &root,int x) { //&root为引用
if(root == NULL) {
root = newNode(x);
return ;
}
if(/*介于二叉树的性质,插在左子树*/)
{
insert(root->lchild,x);
}
else
{
insert(root->rchild,x);
}
}
//二叉树的建立
node* Creat(int data[],int n) {
node* root = NULL;
for(int i=0;i<n;i++)
{
insert (root,data[i]);
}
return root;
}
node* Creat(int data[],int n) {
node* root = NULL;
for(int i=0;i<n;i++)
{
insert (root,data[i]);
}
return root;
}
本文作者:superPG
本文链接:https://www.cnblogs.com/superPG/p/12298094.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步