Delete the node of the binary tree __null

Source: Internet
Author: User
General idea: Discuss in various situations

1. Deleted nodes do not have subtree, delete directly, and modify the corresponding parent node of the pointer is null.

2. For the case of only one subtree, consider the subtree of its parent node, whether left or right, based on the node being deleted.

3. The most complex is the case of two children, two methods can be considered, the same idea: replace a node with the leftmost node of the left subtree of the deleted node A or the left of the right subtree of a, and modify the pointer to the parent node of the leftmost or most right node, and modify the method like 2 without careful discussion.

/************* the deletion of the node of the two-fork tree ***********/

/********* This program describes the creation of a two-fork tree from recursive to lookup *********/
/********* as well as the whole process of deletion, is also considered on the front several *********/
/********* and summarization of the program of the ***************************/
#include <stdio.h>
#include <stdlib.h>

#define MAXSIZE 16

Defining a binary tree node
struct TreeNode
{
int data;
struct treenode* left;
struct treenode* right;
};
typedef TreeNode Node;
typedef node* Btree;

//Recursive establishment of two-fork tree
Btree create_btree (int* nodelist,int postion)
{
 btree newnode;
 
 if ( nodelist[postion]==0| | postion>=maxsize)
     return NULL;
 else
 {
  newnode= ( btree) malloc (sizeof (Node));
  newnode->data=nodelist[postion];
  newnode->left=create_btree (nodelist,postion*2);
  newnode->right=create_btree (nodelist,postion*2+1);
  return NewNode;
 }
}

/******** Find the node of the binary tree ********/
Btree Search (btree root,int node)
{
Btree Point;
Point=root;
if (point==null)
return root;
Else
if (Point->data==node)
return point;
Else
if (Point->data>node)
Search (Point->left,node);
Else
Search (Point->right,node);
}

/******** the second lookup method to record the value of its parent node ********/
Btree Binary_search (btree point,int node,int *postion)
{
Btree parent;

Parent=point;
*postion=0;

while (Point!=null)
{
if (Point->data==node)
return to parent;
Else
{
Parent=point;
if (Point->data>node)
{
point=point->left;
*postion=-1;

}
Else
{
point=point->right;
*postion=1;
}
}

}
return NULL;

}

/********** Delete binary tree node operation ***********/
Btree deletenode (btree root,int node)
{
Btree parent;
Btree Point;
Btree Child;
int postion;


Parent=binary_search (root,node,&postion);
The case that the binary tree is empty
if (parent==null)
return root;
Else
{
Switch (postion)
{case-1:point=parent->left;break;

Case 1:p oint=parent->right;break;

Case 0:p Oint=parent;break;
}

if (point->left==null&&point->right==null)
{
Switch (postion)
{
case-1:parent->left=null;break;
Case 1:parent->right=null;break;
Case 0:parent=null;break;
}

Free (point);
return root;
}

if (point->left==null&&point->right!=null)
{
if (postion==-1)
parent->left=point->right;
Else
if (postion==1)
parent->right=point->right;
Else
root=root->right;

Free (point);
return root;
}

if (point->left!=null&&point->right==null)
{
if (postion==-1)
parent->left=point->left;
Else
if (postion==1)
parent->right=point->left;
Else
root=root->left;
return root;
}

if (point->left!=null&& point->right!=null)
{
Parent=point;
child=point->left;
while (Child->right!=null)
{
Parent=child;
child=child->right;
}
point->data=child->data;
if (parent->left=child)
parent->left=child->left;
Else
parent->right=child->left;

Free (child);
return root;
}
}

}

/*********-sequence traversing binary tree *************/
void Inorder (Btree point)
{
if (point!=null)
{
Inorder (Point->left);
printf ("[%2d]", point->data);
Inorder (Point->right);
}
}

/********* Main program test function ***********/
void Main ()
{
Btree Root=null;
int deletetree;

int nodelist[16]={0,5,4,6,2,0,0,8,1,3,0,0,0,0,7,9};

Root=create_btree (nodelist,1);

printf ("/n the original");
Inorder (root);
printf ("n");

printf ("Input The value of the number/n");
int test;
scanf ("%d", &test);

Root=deletenode (root,test);
printf ("N ' deleted tree is/n");
Inorder (root);
printf ("n");


}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.