• Najnowsze pytania
  • Bez odpowiedzi
  • Zadaj pytanie
  • Kategorie
  • Tagi
  • Zdobyte punkty
  • Ekipa ninja
  • IRC
  • FAQ
  • Regulamin
  • Książki warte uwagi

Usuwanie elementu z drzewa(klasy) BST

Cloud VPS
0 głosów
1,167 wizyt
pytanie zadane 2 kwietnia 2019 w C i C++ przez Zielony12 Nowicjusz (200 p.)

Mam do zaprojektowania i zaimplementowania klase BST jak w temacie. Udalo mi sie zrobic dodawanie elementu do drzewa i wyswietlanie, co nie bylo takie trudne.Problem pojawia sie jednak przy probach usuwania elementu z drzewa.W jaki sposob mogłaby wyglądac przykladowa implementacja?Mam tez do zrobienia obsługę błędów, ale z tym mysle, ze sobie poradzę.

class BuildandReconstructBST
{
	BuildandReconstructBST *left, *right;
	int value;
public:
	BuildandReconstructBST();
	~BuildandReconstructBST();
	void AddElement(int);
	void deleteElement(int);
	BuildandReconstructBST* FindMin(BuildandReconstructBST*);
	
	void PrintTree();
};
BuildandReconstructBST::BuildandReconstructBST() { this->value = 0; this->left = nullptr; this->right = nullptr; };
BuildandReconstructBST::~BuildandReconstructBST() {};
void BuildandReconstructBST::AddElement(int value)
{
	
	if (this->value != 0)
	{
		if (this->value > value)
		{
			if (this->value == value)
			{
				std::string wyjatek = "BLAD:Istnieje juz taki element na drzewie\n"; throw wyjatek;
			}
			else 
			if (this->left == nullptr)
				this->left = new BuildandReconstructBST();
			this->left->AddElement(value);
		}
		else
		{
			if (this->value == value)
			{
				std::string wyjatek = "BLAD:Istnieje juz taki element na drzewie\n"; throw wyjatek;
			}
			else 
			if (this->right == nullptr)
				this->right = new BuildandReconstructBST();
			this->right->AddElement(value);
		}
	}
	else
		 this->value = value;
}
void BuildandReconstructBST::PrintTree()
{
	if (this->left != nullptr)
		this->left->PrintTree();
	std::cout << this->value << std::endl;
	if (this->right != nullptr)
		this->right->PrintTree();
}


 

komentarz 2 kwietnia 2019 przez Zielony12 Nowicjusz (200 p.)
BuildandReconstructBST* BuildandReconstructBST::deleteElement(BuildandReconstructBST* root, int key)
{
	if (root == NULL) return root;
	if (key < root->value)
		root->left = deleteElement(root->left, value);

	else if (key > root->value)
		root->right = deleteElement(root->right, value);
	else
	{
		if (root->left == NULL)
		{
			struct BuildandReconstructBST *temp = root->right;
			free(root);
			return temp;
		}
		else if (root->right == NULL)
		{
			struct BuildandReconstructBST *temp = root->left;
			free(root);
			return temp;
		}
		BuildandReconstructBST *temp = FindMin(root->right);
		root->value = temp->value;
		root->right = deleteElement(root->right, temp->value);
	}
	return root;
}

Tak wyglada.

komentarz 2 kwietnia 2019 przez Zielony12 Nowicjusz (200 p.)
Pomysl fajny i doceniam wysilek w to wlozony, program sie kompiluje ale nie usuwa zadanego elementu.
komentarz 2 kwietnia 2019 przez j23 Mędrzec (195,240 p.)
Wysiłek żaden, Pokaż kod.

 

PS. pisz w komentarzu mojej odpowiedzi.
komentarz 2 kwietnia 2019 przez Zielony12 Nowicjusz (200 p.)

Funkcja main.

BuildandReconstructBST bst;
	int wybor;
	do
	{
		std::cout << "Prosze wybrac ktoras z ponizszych opcji: ";
		std::cout << "\n'1'-dodawanie do drzewa\n'2'-usuwanie z drzewa\n'3'-Wyswietlanie drzewa";
		std::cin >> wybor;
		switch (wybor)
		{
		case 1:
		{
			try {
				int y;
				std::cout << "Wpisz element do drzewa: ";
				std::cin >> y;
				try { bst.AddElement(y); }catch(std::string x) { std::cout << x;};
			}
			catch(std::string x){std::cout << x; };
			break;
		}
		case 2:
		{
			bst.deleteElement(2);
		}
		case 3:
		{
			bst.PrintTree();
			break;
		}
		}
	} while (wybor != 0);

plik .hpp

class BuildandReconstructBST
{
	BuildandReconstructBST *left, *right;
	int value;
	BuildandReconstructBST* delete_element(BuildandReconstructBST* root, int key)
	{
		if (root == nullptr) return root;
		if (key < root->value)
		{
			root->left = delete_element(root->left, value);
		}
		else if (key > root->value)
		{
			root->right = delete_element(root->right, value);
		}
		else
		{
			if (root->left == nullptr)
			{
				BuildandReconstructBST *temp = root->right;
				delete root;
				return temp;
			}
			else if (root->right == nullptr)
			{
				BuildandReconstructBST *temp = root->left;
				delete root;
				return temp;
			}

			BuildandReconstructBST *temp = FindMin(root->right);
			root->value = temp->value;
			root->right = delete_element(root->right, temp->value);
		}
		return root;
	}
	BuildandReconstructBST* root_node;
public:
	BuildandReconstructBST();
	~BuildandReconstructBST();
	void AddElement(int);
	void deleteElement(int key);
	BuildandReconstructBST* FindMin(BuildandReconstructBST*);
	void PrintTree();
};

A nie wiem czy ja mialem stworzyc druga klase i po prostu zastosowac dziedziczenie czy zrobic tak jak zrobilem?

 

komentarz 2 kwietnia 2019 przez j23 Mędrzec (195,240 p.)
Nie. Ja zmieniłem kod. Patrz poniższa odpowiedź.

1 odpowiedź

0 głosów
odpowiedź 2 kwietnia 2019 przez j23 Mędrzec (195,240 p.)
wybrane 2 kwietnia 2019 przez Zielony12
 
Najlepsza

@Zielony12,  Ta funkcja musi mieć dwa parametry, bo jest wywoływana rekurencyjnie. Dodatkowo ta funkcja może usunąć obiekt root, więc nie powinna być metodą klasy BuildandReconstructBST.

 

Tak bym zrobił:

class BSTTree
{
private:
	BuildandReconstructBST* delete_element(BuildandReconstructBST* root, int key)
	{
		if (root == NULL) return root;
		if (key < root->value)
		{
			root->left = delete_element(root->left, value);
		}
		else if (key > root->value)
		{
			root->right = delete_element(root->right, value);
		}
		else
		{
			if (root->left == NULL)
			{
				BuildandReconstructBST *temp = root->right;
				delete root;
				return temp;
			}
			else if (root->right == NULL)
			{
				BuildandReconstructBST *temp = root->left;
				delete root;
				return temp;
			}

			BuildandReconstructBST *temp = FindMin(root->right);
			root->value = temp->value;
			root->right = delete_element(root->right, temp->value);
		}
		return root;
	}

	BuildandReconstructBST* root_node;

public:

	...

	void deleteElement(int key)
	{
		root_node = delete_element(root_node, key);
	}

};

 

PS. przeniosłem posta z komentarza, bo w zasadzie to odpowiedź :)

komentarz 2 kwietnia 2019 przez Zielony12 Nowicjusz (200 p.)

@j23, Co to jest za klasa ta BSTTree?

komentarz 2 kwietnia 2019 przez j23 Mędrzec (195,240 p.)
Klasa, która reprezentuje drzewo.
komentarz 2 kwietnia 2019 przez Zielony12 Nowicjusz (200 p.)
Tyle to sie zdolalem domyslic, mialem na mysli czy to jest dodatkowa klasa?
komentarz 2 kwietnia 2019 przez j23 Mędrzec (195,240 p.)
edycja 2 kwietnia 2019 przez j23
No tak. Skoro się domyśliłeś, to powinieneś to wiedzieć ;) Powinieneś dodać do niej resztę niezbędnych metod.

Podobne pytania

0 głosów
1 odpowiedź 424 wizyt
pytanie zadane 4 lutego 2020 w C i C++ przez Kate1243 Nowicjusz (120 p.)
0 głosów
1 odpowiedź 335 wizyt
pytanie zadane 24 maja 2018 w C i C++ przez kikosiak Obywatel (1,010 p.)
0 głosów
1 odpowiedź 183 wizyt
pytanie zadane 28 listopada 2023 w C i C++ przez natalia2002. Początkujący (400 p.)

93,483 zapytań

142,417 odpowiedzi

322,763 komentarzy

62,895 pasjonatów

Motyw:

Akcja Pajacyk

Pajacyk od wielu lat dożywia dzieci. Pomóż klikając w zielony brzuszek na stronie. Dziękujemy! ♡

Oto polecana książka warta uwagi.
Pełną listę książek znajdziesz tutaj

Kursy INF.02 i INF.03
...