当前位置:编程学习 > C/C++ >>

一步一步写算法(之排序二叉树删除-3)

 

【 声明:版权所有,欢迎转载,请勿用于商业用途。  联系信箱:feixiaoxing @163.com】

 

    3 普通节点的删除

 

    3.1 删除的节点没有左子树,也没有右子树

 

     测试用例1: 删除节点6

 

 

/*

*               

*         10          ======>     10

*        /  \                      \

*      6     15                     15

*                                                          

*/ 

 

static void test8() 

    TREE_NODE* pTreeNode = NULL; 

    assert(TRUE == insert_node_into_tree(&pTreeNode, 10)); 

    assert(TRUE == insert_node_into_tree(&pTreeNode, 6)); 

    assert(6 == pTreeNode->left_child->data); 

    assert(TRUE == insert_node_into_tree(&pTreeNode, 15)); 

    assert(TRUE == delete_node_from_tree(&pTreeNode, 6)); 

    assert(NULL == pTreeNode->left_child); 

    free(pTreeNode->right_child); 

    free(pTreeNode); 

/*

*              

*         10          ======>     10

*        /  \                      \

*      6     15                     15

*                                                        

*/

 

static void test8()

{

       TREE_NODE* pTreeNode = NULL;

       assert(TRUE == insert_node_into_tree(&pTreeNode, 10));

       assert(TRUE == insert_node_into_tree(&pTreeNode, 6));

       assert(6 == pTreeNode->left_child->data);

       assert(TRUE == insert_node_into_tree(&pTreeNode, 15));

       assert(TRUE == delete_node_from_tree(&pTreeNode, 6));

       assert(NULL == pTreeNode->left_child);

       free(pTreeNode->right_child);

       free(pTreeNode);

}    测试用例2: 删除节点15

 

 

/*

*               

*         10          ======>     10

*        /  \                    / 

*      6     15                 6   

*                                                         

*/ 

 

static void test9() 

    TREE_NODE* pTreeNode = NULL; 

    assert(TRUE == insert_node_into_tree(&pTreeNode, 10)); 

    assert(TRUE == insert_node_into_tree(&pTreeNode, 6)); 

    assert(TRUE == insert_node_into_tree(&pTreeNode, 15)); 

    assert(15 == pTreeNode->right_child->data); 

    assert(TRUE == delete_node_from_tree(&pTreeNode, 15)); 

    assert(NULL == pTreeNode->right_child); 

    free(pTreeNode->right_child); 

    free(pTreeNode); 

/*

*              

*         10          ======>     10

*        /  \                    /

*      6     15                 6  

*                                                         

*/

 

static void test9()

{

       TREE_NODE* pTreeNode = NULL;

       assert(TRUE == insert_node_into_tree(&pTreeNode, 10));

       assert(TRUE == insert_node_into_tree(&pTreeNode, 6));

       assert(TRUE == insert_node_into_tree(&pTreeNode, 15));

       assert(15 == pTreeNode->right_child->data);

       assert(TRUE == delete_node_from_tree(&pTreeNode, 15));

       assert(NULL == pTreeNode->right_child);

       free(pTreeNode->right_child);

       free(pTreeNode);

}&

补充:软件开发 , C语言 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,