# Include <stdio. h>
# Include <stdlib. h>
# Deprecision max 100
Typedef struct btree
{
Int data;
Struct btree * left;
Struct btree * right;
} Tree;
Typedef struct queue
{
Int tail, head;
TREE * A [Max];
} Queue;
Void create (queue &);
Void enqueue (int K, queue &);
TREE * dequeue (queue &);
Int empty (Queue );
Void insert (int K, tree *&);
Void Order (TREE * t );
Int main ()
{
Int K, I, N;
Void deletekey (int K, tree * & T );
TREE * t;
Printf ("How many elements do you Want to input? \ N ");
Scanf ("% d", & N );
T = NULL;
For (I = 0; I <n; I ++)
{
Scanf ("% d", & K );
Insert (K, t );
}
Printf ("input result: \ n ");
Order (t );
Printf ("Enter the value you want to delete: \ n ");
Scanf ("% d", & K );
Deletekey (K, t );
Printf ("the deleted binary tree is as follows: \ n ");
Order (t );
Return 0;
}
Void deletekey (int K, tree * & T)
{
Void find (INT, tree * &, tree * &, tree * t, Int &);
Int found = 0;
TREE * P, * pfather, * q, * qfather;
Find (K, P, pfather, T, found );
If (found)
{
If (p-> left = NULL)
Q = p-> right;
Else
{
Q = p-> left;
If (Q-> right = NULL)
{
Q-> right = p-> right;
}
Else
{
Do
{
Qfather = Q;
Q = Q-> right;
} While (Q-> right );
Qfather-> right = Q-> left;
Q-> left = p-> left;
Q-> right = p-> right;
}
}
If (P = T)
{
T = Q;
}
Else
If (pfather-> left = P) pfather-> left = Q;
Else pfather-> right = Q;
Free (P );
}
Else printf ("failed! \ N ");
}
Void find (int K, tree * & P, tree * & pfather, tree * t, Int & found)
{
P = T;
Pfather = NULL;
While (T &&! Found)
{
If (p-> DATA = K)
{
Found = 1;
Return;
}
Pfather = P;
If (k> P-> data)
P = p-> right;
If (k <t-> data)
P = p-> left;
}
}
Void create (queue & Q)
{
Q. Head = Q. Tail = 0;
}
Void enqueue (TREE * k, queue & Q)
{
If (Q. Tail + 1) % max = Q. Head)
{
Printf ("over flow! \ N ");
Return;
}
Q. A [q. Tail] = K;
Q. Tail = (Q. Tail + 1) % Max;
}
TREE * dequeue (queue & Q)
{
TREE * K;
If (Q. Head = Q. Tail)
{
Printf ("under flow! \ N ");
Return NULL;
}
K = Q. A [q. Head];
Q. Head = (Q. Head + 1) % Max;
Return K;
}
Int empty (queue q)
{
If (Q. Head = Q. Tail) return 1;
Else return 0;
}
Void Order (TREE * t)
{
Queue Q;
Create (Q );
If (t = NULL)
{
Printf ("No! \ N ");
Return;
}
Enqueue (t, q );
While (! Empty (q ))
{
T = dequeue (Q );
Printf ("% d", T-> data );
If (t-> left) enqueue (t-> left, q );
If (t-> right) enqueue (t-> right, Q );
}
Putchar ('\ n ');
}
Void insert (int K, tree * & T)
{
TREE * P, * PF, * pfather;
PF = T;
P = (TREE *) malloc (sizeof (tree ));
P-> DATA = K;
P-> left = p-> right = NULL;
If (t = NULL)
{
T = P;
Return;
}
While (PF)
{
Pfather = PF;
If (PF-> DATA> K)
PF = PF-> left;
Else pF = PF-> right;
}
If (pfather-> DATA> K) pfather-> left = P;
Else pfather-> right = P;
}