问题描述
编写C++程序来实现执行以下操作:
-
向二叉搜索树中插入一个元素
-
从二叉搜索树中删除一个元素
-
显示所有叶子节点
-
遍历给定的二叉树
程序描述:
这个程序旨在通过各种操作来管理一个二叉搜索树(BST),如插入、删除、显示叶子节点和根据用户选择的方式遍历树。BST是一个基本的数据结构,它保持元素以排序的顺序存储,以便高效地进行搜索、插入和删除操作。每个节点包含一个键值和指向左右子节点的指针。节点的左子树仅包含键值小于该节点键值的节点,而右子树仅包含键值大于该节点键值的节点。
该程序实现的操作包括:
- 插入:提示用户依次输入元素到BST中。
- 删除:询问用户要从BST中删除的特定元素。
- 显示叶子节点:遍历BST并显示所有叶子节点(没有子节点的节点)。
- 树遍历:允许用户选择中序遍历和前序遍历方法来遍历和显示树。
实现代码
#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <stdio.h> using namespace std;
class Node { public: int key; Node* left, * right;
Node(int item) { key = item; left = right = nullptr; } };
class BST { private: Node* insert(Node* node, int key) { if (node == nullptr) return new Node(key);
if (key < node->key) node->left = insert(node->left, key); else if (key > node->key) node->right = insert(node->right, key);
return node; }
Node* minValueNode(Node* node) { Node* current = node; while (current && current->left != nullptr) current = current->left; return current; }
Node* deleteNode(Node* root, int key) { if (root == nullptr) return root;
if (key < root->key) root->left = deleteNode(root->left, key); else if (key > root->key) root->right = deleteNode(root->right, key); else { if (root->left == nullptr) { Node* temp = root->right; delete root; return temp; } else if (root->right == nullptr) { Node* temp = root->left; delete root; return temp; }
Node* temp = minValueNode(root->right); root->key = temp->key; root->right = deleteNode(root->right, temp->key); } return root; }
void inorderTraversal(Node* root) { if (root != nullptr) { inorderTraversal(root->left); cout << root->key << " "; inorderTraversal(root->right); } }
void preorderTraversal(Node* root) { if (root != nullptr) { cout << root->key << " "; preorderTraversal(root->left); preorderTraversal(root->right); } }
void displayLeafNodes(Node* root) { if (root != nullptr) { if (root->left == nullptr && root->right == nullptr) { cout << root->key << " "; } displayLeafNodes(root->left); displayLeafNodes(root->right); } }
public: Node* root;
BST() { root = nullptr; }
void insert(int key) { root = insert(root, key); }
void deleteNode(int key) { root = deleteNode(root, key); }
void inorderTraversal() { cout << "InOrder Traversal: "; inorderTraversal(root); cout << endl; }
void preorderTraversal() { cout << "PreOrder Traversal: "; preorderTraversal(root); cout << endl; }
void displayLeafNodes() { cout << "Leaf nodes: "; displayLeafNodes(root); cout << endl; } };
int main() { BST bst; int n, x; bool f = 0; while (1) { if (f) { cout << "--------------------------------\n"; } f = 1; printf("1. Insert Element into BST\n2. Delete Element from BST\n3. Display all leaf nodes\n4. Traverse the BST\n5. Exit\n"); cout << "Enter your choice: "; cin >> n; switch (n) { case 1: cout << "Enter key to insert: "; cin >> x; bst.insert(x); break; case 2: cout << "Enter key to delete: "; cin >> x; bst.deleteNode(x); break; case 3: bst.displayLeafNodes(); break; case 4: cout << "Choose traversal method (1 for InOrder, 2 for PreOrder): "; int m; cin >> m; if (m == 1) bst.inorderTraversal(); else if (m == 2) bst.preorderTraversal(); break; case 5: return 0; } } }
|