当前位置:   article > 正文

树结构及其算法-二叉树节点的插入_二叉树驻点插入法

二叉树驻点插入法

目录

树结构及其算法-二叉树节点的插入

C++代码


树结构及其算法-二叉树节点的插入

二叉树节点插入的情况和查找相似,重点是插入后仍要保持二叉查找树的特性。如果插入的节点已经在二叉树中,就没有插入的必要了,如果插入的值不在二叉树中,就会出现查找失败的情况,相当于找到了要插入的位置。

  1. if ((tree->Find(tree->GetTreeNode(), value)) != nullptr)
  2. cout << "二叉树中有此节点了" << endl;
  3. else
  4. tree->AddNodeToTree(&value, 1);

C++代码

  1. #include<iostream>
  2. using namespace std;
  3. struct TreeNode {
  4. int data;
  5. TreeNode* leftNode;
  6. TreeNode* rightNode;
  7. TreeNode(int tempData, TreeNode* tempLeftNode = nullptr, TreeNode* tempRightNode = nullptr) {
  8. this->data = tempData;
  9. this->leftNode = tempLeftNode;
  10. this->rightNode = tempRightNode;
  11. }
  12. };
  13. class Tree {
  14. private:
  15. TreeNode* treeNode;
  16. public:
  17. Tree() {
  18. treeNode = nullptr;
  19. }
  20. TreeNode* GetTreeNode() {
  21. return this->treeNode;
  22. }
  23. void AddNodeToTree(int* tempData, int tempSize) {
  24. for (int i = 0; i < tempSize; i++) {
  25. TreeNode* currentNode;
  26. TreeNode* newNode;
  27. int flag = 0;
  28. newNode = new TreeNode(tempData[i]);
  29. if (treeNode == nullptr)
  30. treeNode = newNode;
  31. else {
  32. currentNode = treeNode;
  33. while (!flag) {
  34. if (tempData[i] < currentNode->data) {
  35. if (currentNode->leftNode == nullptr) {
  36. currentNode->leftNode = newNode;
  37. flag = 1;
  38. }
  39. else
  40. currentNode = currentNode->leftNode;
  41. }
  42. else {
  43. if (currentNode->rightNode == nullptr) {
  44. currentNode->rightNode = newNode;
  45. flag = 1;
  46. }
  47. else
  48. currentNode = currentNode->rightNode;
  49. }
  50. }
  51. }
  52. }
  53. }
  54. void Inorder(TreeNode* tempTree) {
  55. if (tempTree != nullptr) {
  56. Inorder(tempTree->leftNode);
  57. cout << tempTree->data << " ";
  58. Inorder(tempTree->rightNode);
  59. }
  60. }
  61. TreeNode* Find(TreeNode* tree, int value) {
  62. while (true) {
  63. if (tree == nullptr)
  64. return nullptr;
  65. if (tree->data == value)
  66. return tree;
  67. else if (tree->data > value)
  68. tree = tree->leftNode;
  69. else
  70. tree = tree->rightNode;
  71. }
  72. }
  73. };
  74. int main() {
  75. int data[]{ 7,4,1,5,16,8,11,12,15,9,2 };
  76. cout << "原始数据:" << endl;
  77. for (int i = 0; i < 11; i++)
  78. cout << data[i] << " ";
  79. cout << endl;
  80. Tree* tree = new Tree;
  81. tree->AddNodeToTree(data, 11);
  82. cout << "中序遍历:" << endl;
  83. tree->Inorder(tree->GetTreeNode());
  84. cout << endl;
  85. cout << "请输入要插入的值:";
  86. int value;
  87. cin >> value;
  88. if ((tree->Find(tree->GetTreeNode(), value)) != nullptr)
  89. cout << "二叉树中有此节点了" << endl;
  90. else{
  91. tree->AddNodeToTree(&value, 1);
  92. cout << "中序遍历:" << endl;
  93. tree->Inorder(tree->GetTreeNode());
  94. cout << endl;
  95. }
  96. return 0;
  97. }

输出结果

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/528314
推荐阅读
相关标签
  

闽ICP备14008679号