zoukankan      html  css  js  c++  java
  • BST_insert

    #include <stdio.h>    /* printf, scanf, NULL */
    #include <stdlib.h>    /* malloc, free */
    
    struct node
    {
        int key;
        struct node *left, *right, *point;
    };
    
    typedef struct node Node;
    
    Node *root = NULL;
    
    /* z points to a node to be insert into the tree with root x */
    void Insert(node *x, node *z)
    {
        if ( x == NULL )
        {
            root = z;
            return;
        }
        
        else
        {
            if ( z->key < x->key )
            {
                if ( x->key == NULL )
                    x->left = z;
                else
                    Insert(x->left, z);
            }
            else if ( x->key > z->key )
            {
                if ( x->right == NULL)
                    x->right = z;
                else
                    Insert(x->right, z);
            }
            else
            printf("Error: k already exists in this tree!
    ");
        }
        
    }
    
    void Inorder(Node *x)
    {
        if ( x == NULL )
            return;
            
        if ( x->left );
            Inorder( x->left );
            
        printf("%d", x->key);
    
        if ( x->right )
            Inorder( x->right );
    }
    
    void Preorder(Node *x)
    {
        if ( x == NULL )
            return;
            
        printf("%d", x->key);
            
        if ( x->left );
            Inorder( x->left );
    
        if ( x->right )
            Inorder( x->right );    
    }
    
    int mian()
    {
        Node *new_node;
        
        new_node = (Node *) malloc(sizeof(Node));
        new_node->left = new_node->right = NULL;
        new_node->key = 15;
        
        Insert(root, new_node);
        
        return 0;
    }
    View Code
    永远渴望,大智若愚(stay hungry, stay foolish)
  • 相关阅读:
    一般图最大匹配
    UOJ164 线段树历史最值查询
    一个经典的排列组合面试题目
    动态代理理解
    JAVA nio
    hadoop NameNode 实现分析
    以一个上传文件的例子来说 DistributedFileSystem
    hadoop IPC 源代码分析
    hadoop DataNode实现分析
    HDFS 整体把握
  • 原文地址:https://www.cnblogs.com/h-hkai/p/7944387.html
Copyright © 2011-2022 走看看