IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    二叉树-cpp

    Fish (fsh267@gmail.com)发表于 2014-08-16 00:00:00
    love 0

    建立一个二叉树,然后中序遍历一下,存储过程留意下, 代码实现如下:

    #include<iostream>
    using namespace std;
    
    struct tree{
        int data;
        tree *right, *left;
    };
    
    class Btree{
        private:
            tree *root;
        public:
            Btree(){root = NULL;}
            void create_btree(int);
            void display(){ inorder(root); cout << endl;}
            void inorder(tree *);
    };
    
    void Btree::create_btree(int x){
        tree *newnode = new tree;
        newnode->data = x;
        newnode->right = newnode->left = NULL;
        if(root == NULL)
            root = newnode;
        else{
            tree *back;
            tree *current = root;
            while(current != NULL){
                back = current;
                if(current->data > x)
                    current = current->left;
        
                else
                    current = current->right;
            }
            if(back->data > x)
                back->left = newnode;
            else
                back->right = newnode;
        }
    }
    void Btree::inorder(tree *tmp){
        if(tmp != NULL){
            inorder(tmp->left);
            cout << tmp->data << " ";
            inorder(tmp->right);
        }
    }
    int main(){
        Btree a;
        for(int i = 0; i < 10; i++)
            a.create_btree(i);
        a.display();
        return 0;
    }

    #END



沪ICP备19023445号-2号
友情链接