Форум программистов
 

Восстановите пароль или Зарегистрируйтесь на форуме, о проблемах и с заказом рекламы пишите сюда - alarforum@yandex.ru, проверяйте папку спам!

Вернуться   Форум программистов > IT форум > Помощь студентам
Регистрация

Восстановить пароль

Купить рекламу на форуме - 42 тыс руб за месяц

Ответ
 
Опции темы Поиск в этой теме
Старый 07.05.2013, 21:22   #1
Татьяна20
Новичок
Джуниор
 
Регистрация: 07.05.2013
Сообщений: 1
По умолчанию Удалить поддерево(С++)

Здравствуйте, подскажите, пожалуйста, как можно обойти дерево и посчитать минимальное отношение число листьев/число не листьев, а затем еще и удалить поддерево с этим отношением.
Мой код для построения дерева:
Код:
#include <stdlib.h>
#ifndef tree_h
#define tree_h
static long count_nodes=0;
class  Tree{
 
public:
    Tree():count(1){};
    int value, key, count;
    Tree *brother, *children;
    
    Tree* CreateRoot(int);
    void Add(Tree *, int, int);
    Tree* FindKey(Tree*, int);
    void Print(Tree*);
    Tree* DeleteKey(Tree*, int);
    void DeleteTree(Tree*);
    void Delete(Tree*, int);
};
 
#endif
 
#include <iostream>
#include "tree.h"
#include <algorithm> 
using namespace std;
 
Tree* Tree::CreateRoot(int value){
    Tree* root = new Tree;
    root->value = value;
    root->brother = NULL;
    root->children = NULL;
    root->key = 1;
    return root;
}
 
Tree * Tree::FindKey(Tree* root, int k){
    Tree* temp = root;
    Tree* found = NULL;
    if(temp != NULL){
        while((found == NULL) && (temp != NULL)){
            if(temp->key == k){
 
                found = temp;
 
            }
            else{
 
                found = FindKey(temp->children, k);
            }
            temp = temp->brother;
        }
    }
    return found;
}
 
void Tree::Add(Tree* root, int key, int value){
    Tree* node = new Tree;
    Tree* child = new Tree;
    Tree* parent = FindKey(root, key);
    node->brother = NULL;
    node->children = NULL;
    node->key = ++count;
    node->value = value;
    if(parent != NULL){
        child = parent->children;
        if(child == NULL){
            parent->children = node;
 
        }
        else{
            while(child->brother != NULL){
    
                child = child->brother;
    
                
            }
            
            child->brother = node;
    
        }
    }
}
 
Tree* Tree::DeleteKey(Tree* root, int k){
    Tree * temp = new Tree;
    Tree * found = new Tree;
    found = NULL;
    if(root != NULL){
        if((root->brother != NULL) && (root->brother->key == k)){
            found = root->brother;
            root->brother = found->brother;
            found->brother = NULL;
            return found;
        }
        if((root->children != NULL) && (root->children->key == k)){
            found = root->children;
            root->children = found->brother;
            found->brother = NULL;
            return found;
        }
        temp = root->children;
        while(temp != NULL){
            found = DeleteKey(temp, k);
            if(found != NULL) break;
            temp = temp->brother;
        }
    }
    return found;
}
 
void Tree::DeleteTree(Tree* root){
    Tree* temp = new Tree;
    Tree* tree = new Tree;
    temp = root;
    while(temp != NULL){
        DeleteTree(temp->children);
        tree = temp;
        temp = temp->brother;
        delete tree;
    }
    root = NULL;
}
 
void Tree::Delete(Tree* root, int k){
    Tree * deleting = new Tree;
    if(root->key == k){
        DeleteTree(root);
    }
    else{
        deleting = DeleteKey(root, k);
        DeleteTree(deleting);
    }
}
 
void Tree::Print(Tree* root){
    Tree* temp = new Tree;
    temp = root;
    while(temp != 0){
        cout <<temp->value<<endl;
        Print(temp->children);
        temp = temp->brother;
    }
}
 
#include <iostream>
#include "tree.h"
using namespace std;
 
int main(){
 
    Tree * root = new Tree;
    root = root->CreateRoot(1);
    Tree t;
    t.Add(root, 1, 2);      t.Add(root, 1, 3);      t.Add(root, 1, 4);
    t.Add(root, 2, 5);      t.Add(root, 2, 6);  
    t.Add(root, 3, 7);
    t.Add(root, 4, 8);      t.Add(root, 4, 9);      t.Add(root, 4, 10);
    t.Add(root, 5, 11);     t.Add(root, 5, 12);
    t.Add(root, 7, 13);     t.Add(root, 7, 14);     t.Add(root, 7, 15);
    t.Add(root, 8, 16);
    t.Add(root, 10, 17);    t.Add(root, 10, 18);
 
    t.Print(root);
    t.Print(root);
    t.Delete(root, 1);
    return 0;
}
Татьяна20 вне форума Ответить с цитированием
Ответ


Купить рекламу на форуме - 42 тыс руб за месяц



Похожие темы
Тема Автор Раздел Ответов Последнее сообщение
Рекурсивно обойти поддерево файловой системы Fastex Общие вопросы C/C++ 1 24.03.2012 00:23
VBA_макрос: удалить всю строку в таблице, если в ней есть слово "удалить" макарошка Microsoft Office Excel 15 05.10.2010 09:09
удалить дубли в tstringlist, и удалить по списку AHTOLLlKA Компоненты Delphi 2 17.01.2010 10:20
как удалить анти вирус( касперский 2006)если она не работает и ее не возможно удалить Alar Общие вопросы Delphi 0 29.10.2006 21:36