二叉树 基本运算

一、括号表示法建二叉树

   核心代码

 void make_Btree()
 {
            Btnode *st[max_size],*p;
            string str;
            int k,j=0,top=-1;
            b=NULL;
            cin>>str;
            for(int i=0;i<str.size();i++)
            {
                switch(str[i])
                {
                    case '(':st[++top]=p;k=1;break;
                    case ')':top--;break;
                    case ',':k=2;break;
                    default :
                             p=new Btnode;
                             p->data=str[i];
                             p->lchild=p->rchild=NULL;
                             if(b==NULL)
                                 b=p;
                             else
                             {
                                 switch(k)
                                 {
                                     case 1:st[top]->lchild=p;break;
                                     case 2:st[top]->rchild=p;break;
                                 }
                             }             
                }
            }
            print(b);
 }
View Code

二、查找节点

   核心代码

     Btnode *find_node(Btnode *b1,char x)//查找节点数值等于X的节点 
        {
            if(b1==NULL)
               return NULL;
            else if(b1->data==x)
               return b1;
            else
            {
                Btnode *p=find_node(b1->lchild,x);
                if(p==NULL)
                   return find_node(b1->rchild,x);
                else
                   return p;
             } 
        }
View Code

三、求树高

   核心代码

 int get_high(const Btnode *b1)
    {
        if(b1==NULL)
           return 0;
        else
        {
            int hl=get_high(b1->lchild);
            int hr=get_high(b1->rchild);
            return hl>hr?hl+1:hr+1;
        }
    }

四、范例

#include<iostream>
#include<string>
using namespace std;
const int max_size=100;
struct Btnode
{
    char data;
    Btnode *lchild;
    Btnode *rchild;
};
class Btree
{ 
    Btnode *b;
    public:
        Btree():b(NULL) {}
        ~Btree()
        {
            destroy(b);
        }
        void destroy(Btnode *&b1)
        {
            if(b1!=NULL)
            {
                destroy(b1->lchild);
                destroy(b1->rchild);
                delete b1;
            }
        }
        void print(Btnode *&b1)
        {
            if(b1!=NULL)
            {
                cout<<b1->data;
                if(b1->lchild!=NULL||b1->rchild!=NULL)
                {
                    cout<<"(";
                    print(b1->lchild);
                    if(b1->rchild!=NULL)
                    {
                        cout<<",";
                        print(b1->rchild);
                    }
                    cout<<")";
                }
            }
        }
         void make_Btree()
        {
            Btnode *st[max_size],*p;
            string str;
            int k,j=0,top=-1;
            b=NULL;
            cout<<"请输入括号表示的二叉树:";
            cin>>str;
            for(int i=0;i<str.size();i++)
            {
                switch(str[i])
                {
                    case '(':st[++top]=p;k=1;break;
                    case ')':top--;break;
                    case ',':k=2;break;
                    default :
                             p=new Btnode;
                             p->data=str[i];
                             p->lchild=p->rchild=NULL;
                             if(b==NULL)
                                 b=p;
                             else
                             {
                                 switch(k)
                                 {
                                     case 1:st[top]->lchild=p;break;
                                     case 2:st[top]->rchild=p;break;
                                 }
                             }             
                }
            }
        }
    int get_high(const Btnode *b1)
    {
        if(b1==NULL)
           return 0;
        else
        {
            int hl=get_high(b1->lchild);
            int hr=get_high(b1->rchild);
            return hl>hr?hl+1:hr+1;
        }
    }
     Btnode *find_node(Btnode *b1,char x)//查找节点数值等于X的节点 
        {
            if(b1==NULL)
               return NULL;
            else if(b1->data==x)
               return b1;
            else
            {
                Btnode *p=find_node(b1->lchild,x);
                if(p==NULL)
                   return find_node(b1->rchild,x);
                else
                   return p;
             } 
        }
    friend int main();//友元,可以使主函数有权访问该类的私有成员
};
int main()
{
    Btree t;
    t.make_Btree();
    cout<<"输入的二叉树为:";
    t.print(t.b);
    cout<<endl<<"树高:";
    cout<<t.get_high(t.b); 
    cout<<endl<<"请输入要查找的节点的值(x):";
    char x;
    cin>>x;
    Btnode *p=t.find_node(t.b,x);
    if(p!=NULL)
       cout<<"find and x="<<p->data<<endl;
    else
       cout<<"no so node\n";
    
    return 0;
}
View Code

 

转载于:https://www.cnblogs.com/shenyuling/p/10026124.html

下面是用C语言实现二叉树基本运算算法的示例代码: ```c #include <stdio.h> #include <stdlib.h> // 定义二叉树结构体 typedef struct Node { int data; // 数据域 struct Node *left; // 左子节点指针 struct Node *right; // 右子节点指针 } Node; // 创建节点 Node *createNode(int data) { Node *node = (Node *) malloc(sizeof(Node)); node->data = data; node->left = NULL; node->right = NULL; return node; } // 插入节点 Node *insertNode(Node *root, int data) { // 如果根节点为空,则新建一个节点作为根节点 if (root == NULL) { root = createNode(data); } // 如果数据小于根节点的数据,则插入到左子树 else if (data < root->data) { root->left = insertNode(root->left, data); } // 如果数据大于等于根节点的数据,则插入到右子树 else { root->right = insertNode(root->right, data); } return root; } // 查找节点 Node *findNode(Node *root, int data) { // 如果根节点为空,则返回NULL if (root == NULL) { return NULL; } // 如果数据等于根节点的数据,则返回根节点 else if (data == root->data) { return root; } // 如果数据小于根节点的数据,则在左子树中查找 else if (data < root->data) { return findNode(root->left, data); } // 如果数据大于根节点的数据,则在右子树中查找 else { return findNode(root->right, data); } } // 删除节点 Node *deleteNode(Node *root, int data) { Node *temp; if (root == NULL) { return NULL; } else if (data < root->data) { root->left = deleteNode(root->left, data); } else if (data > root->data) { root->right = deleteNode(root->right, data); } else { if (root->left == NULL) { temp = root->right; free(root); return temp; } else if (root->right == NULL) { temp = root->left; free(root); return temp; } else { temp = root->right; while (temp->left != NULL) { temp = temp->left; } root->data = temp->data; root->right = deleteNode(root->right, temp->data); } } return root; } // 中序遍历 void inorderTraversal(Node *root) { if (root != NULL) { inorderTraversal(root->left); printf("%d ", root->data); inorderTraversal(root->right); } } int main() { Node *root = NULL; // 插入节点 root = insertNode(root, 5); root = insertNode(root, 3); root = insertNode(root, 8); root = insertNode(root, 2); root = insertNode(root, 4); root = insertNode(root, 7); root = insertNode(root, 9); // 中序遍历 printf("Inorder traversal: "); inorderTraversal(root); printf("\n"); // 查找节点 Node *node = findNode(root, 4); if (node != NULL) { printf("Node found: %d\n", node->data); } else { printf("Node not found\n"); } // 删除节点 root = deleteNode(root, 5); // 中序遍历 printf("Inorder traversal: "); inorderTraversal(root); printf("\n"); return 0; } ``` 以上代码实现了二叉树基本操作,包括创建节点、插入节点、查找节点、删除节点和中序遍历等。你可以根据自己的需求进行修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值