Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
slaid_5_portal_2011.doc
Скачиваний:
0
Добавлен:
01.05.2025
Размер:
1.41 Mб
Скачать

Моделювання дерева. Приклад 2

struct Tree {

int data;

Tree *next_left,

Tree *next_right;

}

Tree *tree_struct;

Tree *new_el;

Tree *cur_el;

int data;

int i;

int mas[5] = {5, 4, 8, 6, 3};

main(){

tree_struct = 0;

for (i=0; i<5; i++)

{

data = mas[i];

new_el = new Tree;

new_el->data = data;

new_el->next_left = 0;

new_el->next_right = 0;

if (tree_struct == 0)

tree_struct = new_el;

else

cur_el = tree_struct;

do

{

if (cur_el->data <= data)

if (cur_el->next_right == 0)

{

cur_el->next_right = new_el;

cur_el = 0;

}

else

cur_el = cur_el->next_right;

else

if (cur_el->next_left == 0) then

{

cur_el->next_left = new_el;

cur_el = 0;

}

else

cur_el = cur_el->next_left;

}

while (cur_el == 0);

}

}

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]