二叉树的基本运算

#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;
}

猜你喜欢

转载自www.cnblogs.com/superPG/p/12298094.html
今日推荐