二叉树的基本运算

谁说我不能喝 提交于 2020-02-12 11:46:36
#include<cstdio>
using namespace std;
struct node {
 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;
 
}
//二叉树的查找、修改
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);
 }
}
//二叉树的建立
node* Creat(int data[],int n) {
 node* root = NULL;
 for(int i=0;i<n;i++)
 {
  insert (root,data[i]);
 }
 return root;
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!