, (, ), . T :
, ;
T, T ( ).
, , . , , , , . Y, X , X, ( X, , Y, ). X K, , K + 1. , , . ( ) . , , . . . . , .
.
. T :
, ;
T, T ( ).
1
, . , 1 . - , .
(, , ) , , . . ( ), - ( ), - ( ).
|
|
2 : a b c d e f g
3 : d b a c f e g
4 : a c b e g f d
,
#include "stdafx.h"
#include "conio.h"
#include "windows.h"
struct BTree *stree(struct BTree *root, struct BTree *r, char info);
struct BTree *dtree(struct BTree *root, char key);
struct BTree
{
char info;
struct BTree *left;
struct BTree *right;
};
int _tmain(int argc, _TCHAR* argv[])
{
struct BTree *root = NULL;
SetConsoleOutputCP(1251);
SetConsoleCP(1251);
root = stree(root,root,'d');
root = stree(root,root,'b');
root = stree(root,root,'a');
root = stree(root,root,'c');
root = stree(root,root,'f');
root = stree(root,root,'g');
root = stree(root,root,'e');
root = dtree(root, 'b');
root = dtree(root, 'd');
root = dtree(root, 'a');
root = dtree(root, 'c');
root = dtree(root, 'e');
root = dtree(root, 'f');
root = dtree(root, 'g');
_getch();
return 0;
}
struct BTree *stree(struct BTree *root, struct BTree *r, char info)
{
if(!r)
{
r = new struct BTree;
r->left = NULL;
r->right = NULL;
r->info = info;
if(!root)
return r;
if(info<root->info)
root->left = r;
else
root->right = r;
return r;
}
if(info<r->info)
stree(r,r->left,info);
else
stree(r,r->right,info);
return root;
}
struct BTree *dtree(struct BTree *root, char key)
{
struct BTree *p,*p2;
if(!root) return root;
if(root->info == key) {
if(root->left == root->right){
free(root);
return 0;
}
else if(root->left == NULL) {
p = root->right;
free(root);
return p;
}
else if(root->right == NULL) {
p = root->left;
free(root);
return p;
}
else {
p2 = root->right;
p = root->right;
while(p->left) p = p->left;
p->left = root->left;
free(root);
return p2;
}
}
if(root->info < key) root->right = dtree(root->right, key);
else root->left = dtree(root->left, key);
return root;
}
1
1
1.) N (>0) N . ( , , , ), N .
2.) ( , , ).
3.) , , .
4.) , , .
2
1.) N (>0) N . ( , , , ), N .
2.) ( , , ).
|
|
3.) .
4.) , , .
3
1.) N (>0) N . ( , , , ), N .
2.) ( , , ).
3.) ( , ).
4.) , , .
4
1.) N (>0) N . ( , , , ), N .
2.) ( , , ).
3.) ( , ).
4.) , , .
5
1.) N (>0) N . ( , , , ), N .
2.) ( , , ).
3.) , .. . (, , .. , , , 0).
4.) , , .
6
1.) N (>0) N . ( , , , ), N .
2.) ( , , ).
3.) , , , .
4.) , , .
7
1.) N (>0) N . ( , , , ), N .
2.) ( , , ).
3.) , .
4.) , , .
8
1.) N (>0) N . ( , , , ), N .
|
|
2.) ( , , ).
3.) , .
4.) , , .
9
1.) N (>0) N . ( , , , ), N .
2.) ( , , ).
3.) , .
4.) , , .
10
1.) N (>0) N . ( , , , ), N .
2.) ( , , ).
3.) (.. , ).
4.) , , .
11
1.) N (>0) N . ( , , , ), N .
2.) ( , , ).
3.) , .
4.) , , .
12
1.) N (>0) N . ( , , , ), N .
2.) ( , , ).
3.) , .
4.) , , .