PTA Delete Nodes in a Binary Search Tree

Given a binary search tree with no same keys, you should output the level-order traversal sequence of this tree after some nodes are deleted.

When trying to remove a node from the tree, we follow the rules listed below:

  • If the node to be deleted is a leaf node, remove it directly;
  • If the node has left subtree, set its key to be the maximum key in its left subtree and remove the node in its left subtree with maximum key;
  • If the node doesn’t have left subtree but has right subtree, set its key to be the minimum key in its right subtree and remove the node in its right subtree with minimum key.
Input Specification:

Each input file contains one test case. The first line of each test contains a number N (1

Output Specification:

Output the level-order traversal sequence of the tree. Numbers in the sequences must be separated by a space, and no extra space is allowed at the end of line.

Sample Input:
7
4 2 1 3 6 5 7
2
3 6
Sample Output:
4 2 5 1 7

代码实现:

#include
#include
typedef struct Tnode* BinTree;
struct Tnode
{
    int data;
    BinTree left;
    BinTree right;
};
BinTree createNode(int a)
{
    BinTree newnode=(BinTree)malloc(sizeof(struct Tnode));
    newnode->data=a;
    newnode->left=NULL;
    newnode->right=NULL;
    return newnode;
}
void InsertNode(BinTree,BinTree);
void DeleteNode(BinTree,BinTree);
void Process(BinTree);
void Level_order(BinTree);

int main()
{
    int n;
    scanf("%d",&n);
    BinTree root=NULL;
    for(int i=0;idata>newnode->data)
    {
        if(root->left==NULL)
        {
            root->left=newnode;
            return;
        }
        else InsertNode(newnode,root->left);
    }
    else
    {
        if(root->right==NULL)
        {
            root->right=newnode;
            return;
        }
        else InsertNode(newnode,root->right);
    }
    return;
}
void Process(BinTree root)
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int d;
        scanf("%d",&d);
        BinTree cur=root;
        BinTree pre=NULL;
        while(cur->data!=d)
        {
            pre=cur;
            if(d>cur->data)cur=cur->right;
            else cur=cur->left;
        }
        DeleteNode(cur,pre);
    }
    return;
}
void DeleteNode(BinTree target,BinTree root)
{
    if(target->left!=NULL)
    {
        BinTree cur=target->left;
        BinTree pre=target;
        while(cur->right!=NULL)
        {
            pre=cur;
            cur=cur->right;
        }
        target->data=cur->data;
        DeleteNode(cur,pre);
    }
    else if(target->right!=NULL)
    {
        BinTree cur=target->right;
        BinTree pre=target;
        while(cur->left!=NULL)
        {
            pre=cur;
            cur=cur->left;
        }
        target->data=cur->data;
        DeleteNode(cur,pre);
    }
    else
    {
        if(root->right==target)root->right=NULL;
        else root->left=NULL;
        free(target);
    }
    return;
}
void Level_order(BinTree root)
{
    BinTree queue[105];
    int head=0,tail=0;
    queue[tail++]=root;
    int flag=0;
    while(headdata);
        if(cur->left!=NULL)queue[tail++]=cur->left;
        if(cur->right!=NULL)queue[tail++]=cur->right;
    }
    return;
}

你可能感兴趣的:(前端,开发语言,数据结构)