帮我用qt写一个二叉树代码
时间: 2023-08-28 12:13:37 浏览: 90
二叉树的代码
5星 · 资源好评率100%
### 回答1:
我可以提供一个简单的二叉树代码:
#include <QtCore>class BinaryTree {
public:
BinaryTree() : root(nullptr) {}
~BinaryTree() { if (root != nullptr) delete root; } void insert(int data) {
if (root == nullptr)
root = new Node(data);
else
root->insert(data);
} void printLevelOrder() {
if (root == nullptr)
return;
else
root->printLevelOrder();
}private:
class Node {
public:
Node(int data) : data(data), left(nullptr), right(nullptr) {}
~Node() {
if (left != nullptr)
delete left;
if (right != nullptr)
delete right;
} void insert(int data) {
if (data < this->data) {
if (left == nullptr)
left = new Node(data);
else
left->insert(data);
} else {
if (right == nullptr)
right = new Node(data);
else
right->insert(data);
}
} void printLevelOrder() {
int level = 0;
printLevelOrder(level);
} void printLevelOrder(int level) {
if (left != nullptr)
left->printLevelOrder(level + 1);
qDebug() << QString("Level: %1, Data: %2").arg(level).arg(data);
if (right != nullptr)
right->printLevelOrder(level + 1);
} private:
int data;
Node* left;
Node* right;
}; Node* root;
};int main() {
BinaryTree bt;
bt.insert(5);
bt.insert(2);
bt.insert(7);
bt.insert(3);
bt.insert(6);
bt.insert(1);
bt.insert(4); bt.printLevelOrder(); return 0;
}
### 回答2:
下面是使用Qt编写的二叉树代码的示例:
```
#include <QCoreApplication>
#include <QDebug>
class TreeNode {
public:
int data;
TreeNode* left;
TreeNode* right;
TreeNode(int value) {
data = value;
left = nullptr;
right = nullptr;
}
};
class BinaryTree {
public:
TreeNode* root;
BinaryTree() {
root = nullptr;
}
void insert(int value) {
root = insertNode(root, value);
}
void remove(int value) {
root = removeNode(root, value);
}
TreeNode* search(int value) {
return searchNode(root, value);
}
private:
TreeNode* insertNode(TreeNode* node, int value) {
if (node == nullptr) {
return new TreeNode(value);
}
if (value < node->data) {
node->left = insertNode(node->left, value);
} else {
node->right = insertNode(node->right, value);
}
return node;
}
TreeNode* removeNode(TreeNode* node, int value) {
if (node == nullptr) {
return nullp,tmpl;
}
if (value < node->data) {
node->left = removeNode(node->left, value);
} else if (value > node->data) {
node->right = removeNode(node->right, value);
} else {
if (node->left == nullptr && node->right == nullptr) {
delete node;
node = nullptr;
} else if (node->left == nullptr) {
TreeNode* temp = node;
node = node->right;
delete temp;
} else if (node->right == nullptr) {
TreeNode* temp = node;
node = node->left;
delete temp;
} else {
TreeNode* temp = findMinimumNode(node->right);
node->data = temp->data;
node->right = removeNode(node->right, temp->data);
}
}
return node;
}
TreeNode* searchNode(TreeNode* node, int value) {
if (node == nullptr || node->data == value) {
return node;
}
if (value < node->data) {
return searchNode(node->left, value);
} else {
return searchNode(node->right, value);
}
}
TreeNode* findMinimumNode(TreeNode* node) {
while (node->left != nullptr) {
node = node->left;
}
return node;
}
};
int main(int argc, char *argv[]) {
QCoreApplication a(argc, argv);
BinaryTree tree;
tree.insert(50);
tree.insert(30);
tree.insert(70);
tree.insert(20);
tree.insert(40);
tree.insert(60);
tree.insert(80);
qDebug() << "Binary Tree Inorder Traversal:";
printInorderTraversal(tree.root);
return a.exec();
}
```
这段代码使用了Qt的QCoreApplication来运行程序,构建了一个二叉树类(BinaryTree)和一个节点类(TreeNode)。BinaryTree类包含了插入节点(insert)、删除节点(remove)和查找节点(search)的方法。代码中还实现了二叉树的中序遍历(printInorderTraversal)方法。
在示例的main函数中,创建了一个二叉树对象(tree),并插入了一些节点。然后,通过调用printInorderTraversal方法,按中序遍历的顺序打印出二叉树的节点值。
这只是一个简单的二叉树实现示例,你可以根据自己的需要对代码进行修改和扩展。
### 回答3:
好的,下面是使用Qt编写的一个简单的二叉树代码。该代码实现了二叉树的创建、插入节点、删除节点,以及前序遍历、中序遍历和后序遍历等基本操作。
```cpp
#include <QCoreApplication>
#include <QDebug>
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
class BinaryTree {
public:
BinaryTree() : root(nullptr) {}
void insertNode(int val) {
if (root == nullptr) {
root = new TreeNode(val);
} else {
insertNode(root, val);
}
}
void deleteNode(int val) {
deleteNode(root, val);
}
void preOrderTraversal() {
preOrderTraversal(root);
qDebug() << endl;
}
void inOrderTraversal() {
inOrderTraversal(root);
qDebug() << endl;
}
void postOrderTraversal() {
postOrderTraversal(root);
qDebug() << endl;
}
private:
TreeNode *root;
void insertNode(TreeNode *node, int val) {
if (val < node->val) {
if (node->left) {
insertNode(node->left, val);
} else {
node->left = new TreeNode(val);
}
} else {
if (node->right) {
insertNode(node->right, val);
} else {
node->right = new TreeNode(val);
}
}
}
TreeNode* deleteNode(TreeNode *node, int val) {
if (node == nullptr) {
return nullptr;
}
if (val < node->val) {
node->left = deleteNode(node->left, val);
} else if (val > node->val) {
node->right = deleteNode(node->right, val);
} else {
if (node->left == nullptr) {
TreeNode *temp = node->right;
delete node;
return temp;
} else if (node->right == nullptr) {
TreeNode *temp = node->left;
delete node;
return temp;
}
TreeNode *temp = findMin(node->right);
node->val = temp->val;
node->right = deleteNode(node->right, temp->val);
}
return node;
}
TreeNode* findMin(TreeNode *node) {
while (node->left) {
node = node->left;
}
return node;
}
void preOrderTraversal(TreeNode *node) {
if (node) {
qDebug() << node->val << " ";
preOrderTraversal(node->left);
preOrderTraversal(node->right);
}
}
void inOrderTraversal(TreeNode *node) {
if (node) {
inOrderTraversal(node->left);
qDebug() << node->val << " ";
inOrderTraversal(node->right);
}
}
void postOrderTraversal(TreeNode *node) {
if (node) {
postOrderTraversal(node->left);
postOrderTraversal(node->right);
qDebug() << node->val << " ";
}
}
};
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
BinaryTree tree;
tree.insertNode(5);
tree.insertNode(3);
tree.insertNode(8);
tree.insertNode(2);
tree.insertNode(4);
tree.insertNode(7);
tree.insertNode(9);
tree.preOrderTraversal();
tree.inOrderTraversal();
tree.postOrderTraversal();
tree.deleteNode(4);
tree.inOrderTraversal();
return a.exec();
}
```
此代码使用C++的语法,通过Qt的QDebug输出结果。主函数中创建了一个BinaryTree对象,并调用了插入节点、删除节点以及三种遍历方式的函数进行操作和展示效果。
阅读全文