Professional Documents
Culture Documents
DSA Tuần 8
DSA Tuần 8
#include <iostream>
using namespace std;
struct TNODE {
int key;
TNODE* pLeft;
TNODE* pRight;
};
typedef TNODE* TREE;
void InsertNode(TNODE*& root, int value) {
if (value == -1)
return;
if (root == NULL) {
root = new TNODE;
root->key = value;
root->pLeft = NULL;
root->pRight = NULL;
return;
}
if (value == root -> key) {
return;
}
if (value < root->key) {
InsertNode(root->pLeft, value);
} else {
InsertNode(root->pRight, value);
}
}
if (root == NULL) {
root = new TNODE;
root->key = value;
root->pLeft = NULL;
root->pRight = NULL;
return;
}
if (value == root -> key) {
return;
}
if (value < root->key) {
InsertNode(root->pLeft, value);
} else {
InsertNode(root->pRight, value);
}}
void CreateTree(TREE& root) {
int value;
while (true) {
cin >> value;
if (value == -1) {
break;
}
InsertNode(root, value);
}}
TNODE* search(TREE t, int x){
if (t==NULL) return NULL;
if (t != NULL)
{
if (t->key == x) return t;
else if (t->key > x) return search(t->pLeft, x);
Bài 3:
void InsertNode(TNODE*& root, int value) {
if (value == -1)
return;
if (root == NULL) {
root = new TNODE;
root->key = value;
root->pLeft = NULL;
root->pRight = NULL;
return;
}
if (value == root -> key) {
return;
}
if (value < root->key) {
InsertNode(root->pLeft, value);
} else {
InsertNode(root->pRight, value);
}}
void CreateTree(TREE& root) {
int value;
while (true) {
cin >> value;
if (value == -1) {
break;
}
InsertNode(root, value);
}}
if (root == NULL) {
root = new TNODE;
root->key = value;
root->pLeft = NULL;
root->pRight = NULL;
return;
}
if (value == root -> key) {
return;
}
if (value < root->key) {
InsertNode(root->pLeft, value);
} else {
InsertNode(root->pRight, value);
}}
void CreateTree(TREE& root) {
int value;
while (true) {
cin >> value;
if (value == -1) {
break;
}
InsertNode(root, value);
}}
void PrintTree(TREE T) {
if (T == NULL) {
cout << "Empty Tree.";
}
else {
preOrderTraversal(T);
}}
Bài 10:
void InsertNode(TNODE*& root, int value) {
if (value == -1)
return;
if (root == NULL) {
root = new TNODE;
root->key = value;
root->pLeft = NULL;
root->pRight = NULL;
return;
}
if (value == root -> key) {
return;
}
if (value < root->key) {
InsertNode(root->pLeft, value);
} else {
InsertNode(root->pRight, value);
}}
void CreateTree(TREE& root) {
int value;
while (true) {
cin >> value;
if (value == -1) {
break;
}
InsertNode(root, value);
}}