,   CyberForum.ru

++


 
:  :  - 10,   - 5.00
co6ak
407 / 500 / 29
: 12.04.2010
: 1,392
#1

. - C++

11.03.2011, 11:46. 1224. 11
( )

.
.
:
" ."
int-. - .
.
.

C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <iostream>;
#include <cstdlib>;
using namespace std;
int n1=0;
int n2=0;
 
struct TREE {
    int info;
    TREE *left;
    TREE *right;
    };
TREE *root = NULL;
int count = 0;
 
void createTree (TREE **current, int n) 
{
    if (n==0) *current=NULL;
    else 
    {   int x;
        int nl=n/2;
        int nr=n-nl-1;
        TREE *tmp = new (TREE) ;
        cout << "\n   \n" ;cin >> x ;
        tmp->info = x;
        count++;
        createTree (&tmp->left, nl);
        createTree (&tmp->right, nr);
        *current=tmp;
    }
}
void ShowSymmetric(TREE *current, int l)
{
    if(current!=NULL)
    {
    ShowSymmetric(current->left, l+1);
    for(int i=0;i<l;i++)
    cout << "\t";
    cout << current->info << endl;
    ShowSymmetric(current->right, l+1);
    }
}
void clearTree(TREE **current)
{
 
    if(*current!=NULL)
    {
    clearTree(&(*current)->left);
    clearTree(&(*current)->right);
    delete *current;
    count--;
    if(count==0)
    *current=NULL;
    }
 
}
void CheckT(TREE *current, int l) 
{
    
    if (current!=NULL)
    { 
        if(int s=current -> info<0) s=1;
        
    }
}
 
void ShowBackSymmetric(TREE *current, int l)
{
 
    if(current!=NULL)
    {
    ShowBackSymmetric(current->right, l+1);
    for(int i=0;i<l;i++)
    cout << "\t";
    cout << current->info << endl;
    ShowBackSymmetric(current->left, l+1);
    }
 
}
int main()
{
 
    setlocale(LC_ALL,"Russian");
    int num;
    char otv;
    do
    {
 
        cout << "1.  " << endl
        << "2.    " << endl
        << "3.   - " << endl
        << "4. " << endl 
        << "0. "
        << " = ";
        cin >> otv;
        switch(otv)
        {
        case '1':
 
            cout << endl << "   = ";
            cin >> num;
            clearTree(&root);
            createTree(&root, num);
            cout << endl << " " << endl;
            break;
 
 
        case '2':
 
            if(root!=NULL)
            {
            ShowSymmetric(root, 0);
            }
            else
            cout << endl << " " << endl;
            break;
 
        case '3':
 
            if(root!=NULL)
            {
             ShowBackSymmetric(root,0);
            }
            else
            cout << endl << " " << endl;
            break;
 
 
        case '4':
            if(root!=NULL)
            {
             CheckT(root,0);
            }
            else 
            cout << endl << " " << endl;
            break;
 
        case '0':
 
            break;
 
        default:
 
            cout << endl << "" << endl;
            break;
 
        }
 
    }while(otv!='0');
    cin.get();
 
}
50
PLEASE, HELP!

, >_<
Similar
41792 / 34177 / 6122
: 12.04.2006
: 57,940
11.03.2011, 11:46     .
:

C++
C++
C++
C++
C++
C++
C++
.
Artishok
277 / 276 / 32
: 27.12.2009
: 1,767
11.03.2011, 14:06     . #2
co6ak
int-. - .

:

co6ak
407 / 500 / 29
: 12.04.2010
: 1,392
11.03.2011, 14:28  []     . #3
.
C++
1
2
3
4
5
6
7
8
9
10
11
int CheckT(TREE *current, int l) 
{
        
        if (current!=NULL)
        { 
                if(int s=current -> info<0)     s=1; 
                else return {}   //          :)
                
        }
         else return 0; 
}
2 . n1 n2. - - .
fasked
C++
4932 / 2512 / 180
: 07.10.2009
: 4,306
: 1
11.03.2011, 15:23     . #4
C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
 
typedef struct tree_node {
    struct tree_node * left;
    struct tree_node * right;
    int value;
} node_t, *pnode_t;
 
typedef pnode_t tree_t, *ptree_t;
 
 
int tree_has_left (pnode_t node) {
    assert (node);
    return node->left != NULL;
}
 
int tree_has_right (pnode_t node) {
    assert (node);
    return node->right != NULL;
}
 
int tree_branch_left (pnode_t node, int value) {
    assert (node);
    return node->value <= value;
}
 
int tree_branch_right (pnode_t node, int value) {
    assert (node);
    return !(tree_branch_left(node, value));
}
 
int tree_empty (ptree_t tree) {
    assert (tree);
    return *tree == NULL;
}
 
ptree_t tree_release (ptree_t tree) {
    assert (tree);
    if (*tree) {
        tree_release (&(*tree)->left);
        tree_release (&(*tree)->right);
        free (*tree);
       //printf ("%p was deleted\n", *tree);
        *tree = NULL;
    }
    return tree;
}
 
void tree_add (ptree_t tree, int value) {
    assert (tree);
    if (*tree == NULL) {
        (*tree) = (pnode_t) malloc (sizeof (node_t));
        (*tree)->left = (*tree)->right = NULL;
        (*tree)->value = value;
        printf ("%p [%+d] was added\n", *tree, value);
    }
    else {
        if (tree_branch_left (*tree, value))
            tree_add (&(*tree)->left, value);
        else 
            tree_add (&(*tree)->right, value);
    }
}
 
size_t nnegatives (ptree_t tree, size_t init) {
    assert (tree);
    if (*tree) {
        init += (*tree)->value < 0;
        if (tree_has_left (*tree)) 
            init = nnegatives (&(*tree)->left, init);
        if (tree_has_right (*tree))
            init = nnegatives (&(*tree)->right, init);
    }
 
    return init;
}
 
size_t nnegatives_left (ptree_t tree) {
    assert (tree);
    return nnegatives(&(*tree)->left, 0);
}
 
size_t nnegatives_right (ptree_t tree) {
    assert (tree);
    return nnegatives(&(*tree)->right, 0);
}
 
int main() {
    size_t i;
    tree_t t = NULL;
 
    tree_add(&t, -5);
    for (i = 0; i < 10; ++i)
        tree_add (&t, rand() % 10 - rand() % 10);
 
    printf ("l-tree has %u negatives\n", nnegatives_left  (&t));
    printf ("r-tree has %u negatives\n", nnegatives_right (&t));
 
    tree_release (&t);
}
009D1448 [-5] was added
009D1480 [-6] was added
009D14B8 [+4] was added
009D14F0 [+5] was added
009D1528 [+0] was added
009D1560 [-2] was added
009D1598 [+0] was added
009D15D0 [-6] was added
009D1608 [+0] was added
009D1640 [+3] was added
009D1678 [+1] was added
l-tree has 1 negatives
r-tree has 2 negatives
co6ak
407 / 500 / 29
: 12.04.2010
: 1,392
12.03.2011, 00:25  []     . #5
.


3 20
.
- )

5 22
.
Artishok
277 / 276 / 32
: 27.12.2009
: 1,767
12.03.2011, 00:57     . #6
?

""
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#include <stdlib.h>
#include <iostream>
#include <stdio.h>
#include <windows.h>
 
 
//äëÿ óäîáñòâ* ðåêîìå*äóåòñÿ ââîäèòü ýëåìå*òû ò.ê. âûãëÿäèò äåðåâî òîãä* êð*ñèâøå
/*
 50->44->46->45->48->60->59->62
*/
struct uzel
{
   int key;//õð**èòñÿ â âåðøè*å êëþ÷-ç**÷å*èå
   struct uzel *left, *right;//óê*ç*òåëè ** ïð*âîå è ëåâîå ïîääåðåâî
};
 
struct stack
{
  uzel *val;
  struct stack *next;//äîá*âèë â **÷*ëî èçâëåê â **÷*ëî
};
 
 
uzel* not(int val)
{
  uzel *nod=new uzel;
  nod->key=val;
  nod->left=0;
  nod->right=0;
  return nod;
}
 
void add_tree(uzel **root, int val)//äîá*âëå*èå ýëåìå*ò*
{
  if (*root==0) *root = not(val);//åñëè 0 ñîçä*åì *îâóþ ãîëîâó
  if (val < (*root)->key)//åñëè ìå*üøå ç**÷å*èÿ òî âëåâî
  {
   if ((*root)->left==0)//åñëè 0 òî ïðîñòî ç*ïèøåì â ëåâûé óçåë ýëåìå*ò
     (*root)->left = not(val);
   else
     add_tree(&(*root)->left, val);//è**÷å **äî äîá*âòü âëåâî âëåâî
  }
  if (val > (*root)->key)//åñëè áîëüøå òî âïð*âî
  {
   if ((*root)->right==0)
     (*root)->right = not(val);//äîñò*òî÷*î ç*ïèñ*òü òóä* ýëåìå*ò
   else
     add_tree(&(*root)->right, val);//â ïð*âîå ïð*âîå ïîääåðåâî
  }
   return;
}
 
void find_tree(uzel *root, int val)//ïîèñê ýëåìå*ò*
{
  if (root==0)//åñëè *åò *è îä*îãî ýëåìå*ò* òî âåð*óòü 0
    return;
  if (val == (root)->key)//**øëè - âîò è ñë*â*î
    cout<<"ELem exist";
  if (val < (root)->key)//åñëè ìå*üøå òî èñê*òü â ëåâîì ïîääåðåâå
    find_tree((root)->left, val);
  if (val > (root)->key)//è**÷å âïð*âîì
    find_tree((root)->right, val);
}
 
int rightmost(uzel *root)//ñ*ìûé ïð*âûé
{
  while (root->right != NULL)//ïîê* *å äîéäåò äî ïð*âîãî ñ ññûëêîé 0
    root = root->right;//ïî ïð*âûì ïîäóçë*ì ø*ã*òü
  return root->key;//âåð*ÿåì òî ÷òî òóä* ç*òåñ*ëîñü
}
 
int nnegatives (uzel *root, int init) {
    if (root==NULL) return 0;
    if (root) {
        init += (root)->key < 0;
        if (root->left) 
            init = nnegatives (root->left, init);
        if (root->right)
            init = nnegatives (root->right, init);
    }
 
    return init;
}
 
int nnegatives_left (uzel *root) {
    if (root==NULL) return 0;
    return nnegatives(root->left, 0);
}
 
int  nnegatives_right (uzel *root) {
    if (root==NULL) return 0;
    return nnegatives(root->right, 0);
}
 
uzel* del_tree(uzel *root, int val)//óä*ëå*èå
{
  if (root==0) return 0;//åñëè 0 ò*ê 0
  if (root->key == val) //åñëè **øëè òî
  {
        //ýòî ýëåìå*ò-ëèñò
     if (root->left==0 && root->right==0) 
     {
      delete root;//äîñò*òî÷*î ïðîñòî óä*ëèòü
      return 0;
     }
     if (root->right==0 && root->left != 0)//åñëè åñòü îäè* ïîòîìîê ëåâûé 
     {
      uzel *temp = root->left;//òî ïåðåìå**îé òèï* "óçåë" ç*ïèøåì ç**÷å*èå óçë*-ïîòîìê* ñëåâ*
      delete root;
      return temp;
     }
     if (root->left==0 && root->right != 0) //òî æå äëÿ ïð*âîãî
     {
      uzel *temp = root->right;
      delete root;
      return temp;
     }
     root->key = rightmost(root->left);//òåïåðü â óä*ëÿåìîé ïîçèöèè áóäåò ñòîÿòü ýëåìå*ò ñ*ìûé ïð*âûé ëåâîãî ïîääåðåâ*
     root->left = del_tree(root->left, root->key);//èùåì â ëåâîì ïîääåðåâå ýëåìå*ò ñ óä*ëÿåìûì ç**÷å*èåì è ïðîâîäèì åãî äåçè*òåãð*öèþ
     return root;
  }
  if (val < root->key)//åñëè ç**÷å*èå ìå*üøå òîãî ÷òî â êîð*å òî âûïîë*èòü âñå *åîáõîäèìûå îïåð*öèÿ äëÿ ëåâîãî ïîääåðåâ* 
  {
   root->left = del_tree(root->left, val);
   return root;
  }
  if (val > root->key)//òî æå äëÿ ïð*âîãî 
  { 
   root->right = del_tree(root->right, val);
   return root;
  }
 return root;
}
 
 
void print_tree(uzel *root)//âûâåñòè äåðåâî
{
 if (root == 0) 
    return;
   print_tree(root->left);
   cout<<root->key<<" ";
   print_tree(root->right);
}
 
      
void print_tree_2(uzel *root)//*åðåêóðñèâ*ûé îáõîä
{
  uzel *dr=root;
  struct stack
  {
    uzel *val;
    struct stack *next;
  }*st=0;
  do
  {
   if (dr != NULL) 
   {
    stack *elem=new stack;
    elem->val=dr;//óçåë ç*ïèñûâ*åì â ñòåê
    if (st==0)
    {
     st=elem;
     st->next=0;
    }
    else
    {
     elem->next=st;//áûâø*ÿ ãîëîâ* ñò**åò âòîðûì ýëåìå*òîì
     st=elem;
    }
    cout << dr->key <<" ";
    dr = dr->right;
    }  
    else 
    {                           
     if (st == NULL) break;//åñëè ñòåê ïóñò
     dr = st->val;
     //èçâëå÷å*èå èç ñòåê*
     stack *temp;
     temp=st;
     st=st->next;
     delete temp;
     dr = dr->left;
    }
   }while(true);
}
 
void creator(uzel *root,int x,int y,int c)//âûâîä äåðåâ* ââèäå äåðåâ*
{
    if (root == 0) 
    return; 
    COORD coord={x,y}; //êîîðäè**òû x y
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);
    y=y+8;
    cout<<root->key;
    creator(root->left,x-c,y,(c/2));
    creator(root->right,x+c,y,(c/2));
}
 
int main()
{
    uzel *trie=0;
    int ch;
    while(0<1)
    {
        system("cls");
        cout<<"Insert into tree----------1"<<endl;
        cout<<"View left-root-right------2"<<endl;
        cout<<"Delete elem---------------3"<<endl;
        cout<<"Find element--------------4"<<endl;
        cout<<"Unrec view----------------5"<<endl;
        cout<<"View tree-----------------6"<<endl;
        cout<<"Left negatives------------7"<<endl;
        cout<<"Right negatives-----------8"<<endl;
        cin>>ch;
        switch(ch)
        {
            case 1:
            {
                system("cls");
                int ins=0;
                cout<<"Put element in tree"<<endl;
                cin>>ins;
                add_tree(&trie,ins);
                break;
            }
            case 2:
            {
                system("cls");
                print_tree(trie);
                getchar();
                break;
            }
            case 3:
            {
                system("cls");
                int del=0;
                cout<<"Elem fo delete"<<endl;
                cin>>del;
                trie=del_tree(trie,del);
                cout<<"If element exist- it deleted"<<endl;
                getchar();
                break;
            }
            case 4:
            {
                system("cls");
                int fin=0;
                cout<<"Insert element to find"<<endl;
                cin>>fin;
                find_tree(trie,fin);
                getchar();
                break;
            }
            case 5:
            {
                system("cls");
                print_tree_2(trie);
                getchar();
                break;
            }
            case 6:
            {
                system("cls");
                creator(trie,40,1,18);
                getchar();
                break;
            }
            case 7:
            {
                system("cls");
                cout<<"Left negatives---"<<nnegatives_left(trie)<<endl;
                getchar();
                break;
            }
            case 8:
            {
                system("cls");
                cout<<"Right negatives---"<<nnegatives_right(trie)<<endl;
                getchar();
                break;
            }
        }
    }
                
    return 0;
}
.
co6ak
407 / 500 / 29
: 12.04.2010
: 1,392
12.03.2011, 01:22  []     . #7
)
.
assert ))
fasked
12.03.2011, 13:58
  #8

:

co6ak
assert
co6ak

co6ak
407 / 500 / 29
: 12.04.2010
: 1,392
12.03.2011, 14:02  []     . #9
fasked

:



.
)
ForEveR
 ++
7958 / 4720 / 319
: 24.06.2010
: 10,525
: 3
12.03.2011, 14:04     . #10
co6ak, , assert...
fasked
C++
4932 / 2512 / 180
: 07.10.2009
: 4,306
: 1
12.03.2011, 14:06     . #11
co6ak
assert , assert , terminate, , , , , assert , terminate assert, .
MoreAnswers
37091 / 29110 / 5898
: 17.06.2006
: 43,301
12.03.2011, 14:39     .
:

C++
C++
C++
C++
C++



:
co6ak
407 / 500 / 29
: 12.04.2010
: 1,392
12.03.2011, 14:39  []     . #12
fasked
assert , assert , terminate, , , , , assert , terminate assert, .
.
try .
Yandex
12.03.2011, 14:39     .

: 10:10. GMT +3.
- , ,
-
Powered by vBulletin® Version 3.8.9
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
@Mail.ru