Basic operations and hierarchical traversal of Binary Trees

Source: Internet
Author: User

# 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;
}

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.