Технический форум

Технический форум (http://www.tehnari.ru/)
-   C/C++/С# (http://www.tehnari.ru/f42/)
-   -   Удалить поддерево (http://www.tehnari.ru/f42/t88115/)

Татьяна20 07.05.2013 21:58

Удалить поддерево
 
Здравствуйте, подскажите, пожалуйста, как можно обойти дерево и посчитать минимальное отношение число листьев/число не листьев, а затем еще и удалить поддерево с этим отношением.
Мой код для построения дерева:
#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;
}


Часовой пояс GMT +4, время: 20:48.

Powered by vBulletin® Version 4.5.3
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.