• 二叉搜索树中序遍历与插入


    二叉树递归中序遍历的算法很简单,伪代码如下:

    1 INORDER-TREE-WALK(x)
    2     if x!=NIL
    3         INORDER-TREE--WALK(x.left)
    4         print x.key
    5         INORDER-TREE-WALK(x.right)

    二叉树插入的算法也很简单,伪代码如下:

     1 TREE-INSERT(T,z)
     2     y=NIL
     3     x=T.root
     4     while x!=NIL
     5         y=x
     6         if z.key<x.key
     7             x=x.left
     8         else x=x.right
     9     z.p=y
    10     if y==NIL
    11         T.root=z     // tree T was empty
    12     else if z.key<y.key
    13         y.left=z
    14     else y.right=z

    二叉树插入一个新元素的示例图如下:

    将关键字为16的数据项插入到一棵二叉树中。浅阴影节点指示了一条从树根向下到插入数据项位置处的简单路径。虚线表示了为插入数据项而加入的树中的一条链

    在插入关键字为16的节点之前,中序遍历结果应该是2 5 9 12 15 17 18 19

    插入关键字为16的节点之后,中序遍历的结果应该是2 5 9 12 15 16 17 18 19

    代码实现如下:

     1 #include<iostream>
     2 #include<stdlib.h>
     3 using namespace std;
     4 class Node
     5 {
     6 public:
     7     int data;
     8     Node* left;
     9     Node* right;
    10     Node* parent;
    11     bool visited;
    12     Node();
    13     Node(int d);
    14     Node(int d, Node* l,Node* r);
    15 };
    16 class BinaryTree
    17 {
    18 public:
    19    Node* root;
    20    BinaryTree(Node* root);
    21    void insert(Node* root,Node* z);
    22    void recurse_in_order_visit(Node* root);
    23 };
    24 int main()
    25 {
    26    int i;
    27    Node* node1=new Node(2,NULL,NULL);
    28    Node* node2 =new Node(9, NULL, NULL);
    29    Node* node3 =new Node(5, node1, node2);
    30    Node* node4 =new Node(17,NULL,NULL);
    31    Node* node5 =new Node(15, NULL, node4);
    32    Node* node6 =new Node(19, NULL, NULL);
    33    Node* node7 =new Node(18, node5, node6);
    34    Node* root =new Node(12, node3, node7);
    35    BinaryTree* binary_tree=new BinaryTree(root);
    36    cout<<"原始二叉树递归中序遍历的结果为:"<<endl;
    37    binary_tree->recurse_in_order_visit(root);
    38    cout<<endl;
    39    cout<<"请输入要插入节点的键值"<<endl;
    40    cin>>i;
    41    Node* z=new Node(i);
    42    binary_tree->insert(root,z);
    43    cout<<"插入节点后递归中序遍历的结果为:"<<endl;
    44    binary_tree->recurse_in_order_visit(root);
    45    cout<<endl;
    46    return 0;
    47 }
    48 Node::Node(int d)
    49 {
    50    data=d;
    51    left=right=parent=NULL;
    52    visited=false;
    53 }
    54 Node::Node(int d ,Node* l,Node* r)
    55 {
    56    data=d;
    57    left=l;
    58    right=r;
    59    parent=NULL;
    60    visited=false;
    61 }
    62 BinaryTree::BinaryTree(Node* r)
    63 {
    64    root=r;
    65 }
    66 void BinaryTree::recurse_in_order_visit(Node* root)
    67 {
    68    if(root!=NULL)
    69    {
    70       recurse_in_order_visit(root->left);
    71       printf("%d	",root->data);
    72       recurse_in_order_visit(root->right);
    73    }
    74 }
    75 void BinaryTree::insert(Node* root,Node* z)
    76 {
    77     Node* y=(Node*)malloc(sizeof(Node));
    78     y=NULL;
    79     Node* x=(Node*)malloc(sizeof(Node));
    80     x=root;
    81     while(x!=NULL)
    82     {
    83        y=x;
    84        if(z->data <x->data)
    85            x=x->left;
    86        else x=x->right;
    87     }
    88     z->parent =y;
    89     if(y==NULL)
    90         root=z;
    91     else if(z->data<y->data)
    92         y->left=z;
    93     else y->right=z;
    94 }

    程序运行结果如下:

    结果证明是正确的,插入其他数据结果类似,如插入元素6:

  • 相关阅读:
    洛谷 P1635 跳跃
    python write() argument must be str, not bytes
    python write() argument must be str, not bytes
    Python hashlib Unicode-objects must be encoded before hashing
    Python hashlib Unicode-objects must be encoded before hashing
    洛谷P1629 邮递员送信
    TypeError: Error when calling the metaclass bases Cannot create a consistent method resolution
    TypeError: Error when calling the metaclass bases Cannot create a consistent method resolution
    [USACO07FEB]银牛派对Silver Cow Party
    [USACO09OPEN]捉迷藏Hide and Seek
  • 原文地址:https://www.cnblogs.com/hubavyn/p/3668208.html
Copyright © 2020-2023  润新知