Форум программистов, компьютерный форум, киберфорум
C# для начинающих
Войти
Регистрация
Восстановить пароль
Карта форума Темы раздела Блоги Сообщество Поиск Заказать работу  
 
Рейтинг 4.71/41: Рейтинг темы: голосов - 41, средняя оценка - 4.71
0 / 0 / 0
Регистрация: 26.05.2010
Сообщений: 41
1

Составить программу, которая создает из множества бинарное дерево

15.05.2012, 14:51. Показов 7849. Ответов 4
Метки нет (Все метки)

Author24 — интернет-сервис помощи студентам
В деревьях мало чего понимаю, помогите написать прогу))

1. Составить программу, которая создает из множества (37, 89, 10, 13, 56, 16, 30, 40, 32, 31) бинарное дерево. Необходимо осуществить в прямом порядке обход бинарного дерева.
2. К полученному списку обхода применить последовательный метод поиска
0
Programming
Эксперт
94731 / 64177 / 26122
Регистрация: 12.04.2006
Сообщений: 116,782
15.05.2012, 14:51
Ответы с готовыми решениями:

Написать программу, которая создает бинарное дерево
Дан текстовый файл, содержащий текст размером не менее 10 строк. Написать программу, которая...

Разработать программу, которая создает бинарное дерево
не могу написать программу, помогите кто знает. Условие: Разработать программу, которая создает...

Разработать программу, которая создает бинарное дерево
Разработать программу, которая создает бинарное дерево T, элементами которого являются...

Написать программу, которая создает бинарное дерево
Написать программу, которая создает бинарное дерево, состоящее из целых чисел,

4
Эксперт .NET
17688 / 12873 / 3366
Регистрация: 17.09.2011
Сообщений: 21,138
15.05.2012, 14:57 2
Я в какой-то из тем выкладывал свою реализацию двоичного дерева.
Вот, если искать лень:

BinaryTree
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
using System;
using System.Collections;
using System.Collections.Generic;
 
public class BinaryTree<T> : ICollection<T>
{
    protected class Node<TValue>
    {
        public TValue Value
        {
            get;
            set;
        }
        public Node<TValue> Left
        {
            get;
            set;
        }
        public Node<TValue> Right
        {
            get;
            set;
        }
 
        public Node(TValue value)
        {
            Value = value;
        }
    }
 
    protected Node<T> root;
    protected IComparer<T> comparer;
 
    public BinaryTree() : this(Comparer<T>.Default)
    {
    }
    public BinaryTree(IComparer<T> defaultComparer)
    {
        if (defaultComparer == null)
            throw new ArgumentNullException("Default comparer is null");
        comparer = defaultComparer;
    }
    public BinaryTree(IEnumerable<T> collection) : this(collection, Comparer<T>.Default)
    {
        
    }
    public BinaryTree(IEnumerable<T> collection, IComparer<T> defaultComparer) : this(defaultComparer)
    {
        AddRange(collection);
    }
 
    public T MinValue
    {
        get
        {
            if (root == null)
                throw new InvalidOperationException("Tree is empty");
            var current = root;
            while (current.Left != null)
                current = current.Left;
            return current.Value;
        }
    }
    public T MaxValue
    {
        get
        {
            if (root == null)
                throw new InvalidOperationException("Tree is empty");
            var current = root;
            while (current.Right != null)
                current = current.Right;
            return current.Value;
        }
    }
 
    public void AddRange(IEnumerable<T> collection)
    {
        foreach (var value in collection)
            Add(value);
    }
 
    public IEnumerable<T> Inorder()
    {
        if (root == null)
            yield break;
 
        var stack = new Stack<Node<T>>();
        var node = root;
 
        while (stack.Count > 0 || node != null) {
            if (node == null) {
                node = stack.Pop();
                yield return node.Value;
                node = node.Right;
            }
            else {
                stack.Push(node);
                node = node.Left;
            }
        }
    }
    public IEnumerable<T> Preorder()
    {
        if (root == null)
            yield break;
 
        var stack = new Stack<Node<T>>();
        stack.Push(root);
 
        while (stack.Count > 0) {
            var node = stack.Pop();
            yield return node.Value;
            if (node.Right != null)
                stack.Push(node.Right);
            if (node.Left != null)
                stack.Push(node.Left);
        }
    }
    public IEnumerable<T> Postorder()
    {
        if (root == null)
            yield break;
 
        var stack = new Stack<Node<T>>();
        var node = root;
 
        while (stack.Count > 0 || node != null) {
            if (node == null) {
                node = stack.Pop();
                if (stack.Count > 0 && node.Right == stack.Peek()) {
                    stack.Pop();
                    stack.Push(node);
                    node = node.Right;
                }
                else {
                    yield return node.Value;
                    node = null;
                }
            }
            else {
                if (node.Right != null)
                    stack.Push(node.Right);
                stack.Push(node);
                node = node.Left;
            }
        }
    }
    public IEnumerable<T> Levelorder()
    {
        if (root == null)
            yield break;
 
        var queue = new Queue<Node<T>>();
        queue.Enqueue(root);
 
        while (queue.Count > 0) {
            var node = queue.Dequeue();
            yield return node.Value;
            if (node.Left != null)
                queue.Enqueue(node.Left);
            if (node.Right != null)
                queue.Enqueue(node.Right);
        }
    }
 
    #region ICollection<T> Members
    public int Count
    {
        get;
        protected set;
    }
    public virtual void Add(T item)
    {
        var node = new Node<T>(item);
 
        if (root == null)
            root = node;
        else {
            Node<T> current = root, parent = null;
 
            while (current != null) {
                parent = current;
                if (comparer.Compare(item, current.Value) < 0)
                    current = current.Left;
                else
                    current = current.Right;
            }
 
            if (comparer.Compare(item, parent.Value) < 0)
                parent.Left = node;
            else
                parent.Right = node;
        }
        ++Count;
    }
    public virtual bool Remove(T item)
    {
        if (root == null)
            return false;
 
        Node<T> current = root, parent = null;
 
        int result;
        do {
            result = comparer.Compare(item, current.Value);
            if (result < 0) {
                parent = current;
                current = current.Left;
            }
            else if (result > 0) {
                parent = current;
                current = current.Right;
            }
            if (current == null)
                return false;
        }
        while (result != 0);
 
        if (current.Right == null) {
            if (current == root)
                root = current.Left;
            else {
                result = comparer.Compare(current.Value, parent.Value);
                if (result < 0)
                    parent.Left = current.Left;
                else
                    parent.Right = current.Left;
            }
        }
        else if (current.Right.Left == null) {
            current.Right.Left = current.Left;
            if (current == root)
                root = current.Right;
            else {
                result = comparer.Compare(current.Value, parent.Value);
                if (result < 0)
                    parent.Left = current.Right;
                else
                    parent.Right = current.Right;
            }
        }
        else {
            Node<T> min = current.Right.Left, prev = current.Right;
            while (min.Left != null) {
                prev = min;
                min = min.Left;
            }
            prev.Left = min.Right;
            min.Left = current.Left;
            min.Right = current.Right;
 
            if (current == root)
                root = min;
            else {
                result = comparer.Compare(current.Value, parent.Value);
                if (result < 0)
                    parent.Left = min;
                else
                    parent.Right = min;
            }
        }
        --Count;
        return true;
    }
    public void Clear()
    {
        root = null;
        Count = 0;
    }
    public void CopyTo(T[] array, int arrayIndex)
    {
        foreach (var value in this)
            array[arrayIndex++] = value;
    }
    public virtual bool IsReadOnly
    {
        get
        {
            return false;
        }
    }
    public bool Contains(T item)
    {
        var current = root;
        while (current != null) {
            var result = comparer.Compare(item, current.Value);
            if (result == 0)
                return true;
            if (result < 0)
                current = current.Left;
            else
                current = current.Right;
        }
        return false;
    }
    #endregion
 
    #region IEnumerable<T> Members
    public IEnumerator<T> GetEnumerator()
    {
        return Inorder().GetEnumerator();
    }
    #endregion
 
    #region IEnumerable Members
    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
    #endregion
}
5
0 / 0 / 0
Регистрация: 26.05.2010
Сообщений: 41
15.05.2012, 23:10  [ТС] 3
А как осуществить обход?
0
Эксперт .NET
17688 / 12873 / 3366
Регистрация: 17.09.2011
Сообщений: 21,138
16.05.2012, 01:17 4
Обходы реализованы в методах Inorder, Preorder, Postorder и Levelorder.
0
116 / 116 / 70
Регистрация: 10.11.2013
Сообщений: 445
24.11.2014, 03:16 5
kolorotur, красавчик)) влом самому велик собирать)) подрежу твой кодик! (++++)
0
24.11.2014, 03:16
IT_Exp
Эксперт
87844 / 49110 / 22898
Регистрация: 17.06.2006
Сообщений: 92,604
24.11.2014, 03:16
Помогаю со студенческими работами здесь

Разработать программу, которая создает бинарное дерево T, элементами которого являются вещественные числа.
Помогите кто может. Разработать программу, которая создает бинарное дерево T, элементами которого...

Написать программу, которая создает бинарное дерево, состоящее из целых чисел, печатает все числа бинарного дерева
Написать программу, которая создает бинарное дерево, состоящее из целых чисел, печатает все числа...

Написать программу, которая формирует бинарное дерево
Дана последовательность чисел. Написать программу, которая формирует бинарное дерево, выводит...

Бинарное дерево. Написать программу, которая строит Т1 – копию заданного дерева Т
Написать программу, которая строит Т1 – копию заданного дерева Т.

составить программу которая создает файл
итак, сел разбираться с делфи, мне нужно создать программу, которая создает фаил RANDOM1.DAT,...

JavaScript составить программу которая создает список
Вывести результаты работы функций на страницу: 1) Составьте программу, которая создаёт...


Искать еще темы с ответами

Или воспользуйтесь поиском по форуму:
5
Ответ Создать тему
КиберФорум - форум программистов, компьютерный форум, программирование
Powered by vBulletin
Copyright ©2000 - 2024, CyberForum.ru