zoukankan      html  css  js  c++  java
  • 二叉查找树

    二叉查找树的插入节点/打印

    #include<iostream>
    using namespace std;
    struct node
    {
        int data;
        node *left;
        node *right;
    };
    //(1)通过插入节点构建二叉查找树
    node *insert_node(node *root,int num)
    {
    
        if(root==NULL)
        {
            root=new node;
            root->left=NULL;
            root->right=NULL;
            root->data=num;
        }
        else
        {
            if(num<root->data)
            {
                root->left=insert_node(root->left,num);
            }
            else
                root->right=insert_node(root->right,num);
        }
        return root;
    }
    //(2) 打印二叉查找树
    void print_tree(node *root)
    {
    
        if(root->left==NULL)
        {
            if(root->right==NULL)
                cout<<root->data<<endl;
            else
            {
                cout<<root->data<<endl;
                print_tree(root->right);
            }
        }
        else
        {
            print_tree(root->left);
            cout<<root->data<<endl;
            if(root->right!=NULL) print_tree(root->right);
        }
    }
    
    int main()
    {
        node *tree;
        tree=NULL;
        cout<<"input datas:"<<endl;
        int x;char c;
        while(cin>>x)
        {
            tree=insert_node(tree,x);
            cin.get(c);
            if(c=='
    ')break;
        }
        print_tree(tree);
        return 0;
    }
  • 相关阅读:
    eclipse技巧总结
    java中的全等和相似
    curl命令
    tr命令
    Ubuntu下安装支付宝安全控件
    Firefox about
    Ubuntu Terminal Shortcut
    ulimit
    ajax post(copy part)
    getopt
  • 原文地址:https://www.cnblogs.com/riden/p/4564456.html
Copyright © 2011-2022 走看看