Binary-Search-005-Tree-Traverse

main.cpp

#include <iostream>
#include <queue>
#include <ctime>

using namespace std;

template <typename Key, typename Value>
class BST{

private:
    struct Node{
        Key key;
        Value value;
        Node *left;
        Node *right;

        Node(Key key, Value value){
            this->key = key;
            this->value = value;
            this->left = this->right = NULL;
        }
    };

    Node *root;
    int count;

public:
    BST(){
        root = NULL;
        count = 0;
    }
    ~BST(){
        destroy( root );
    }

    int size(){
        return count;
    }

    bool isEmpty(){
        return count == 0;
    }

    void insert(Key key, Value value){
        root = insert(root, key, value);
    }

    bool contain(Key key){
        return contain(root, key);
    }

    Value* search(Key key){
        return search( root , key );
    }

    // 前序遍历
    void preOrder(){
        preOrder(root);
    }

    // 中序遍历
    void inOrder(){
        inOrder(root);
    }

    // 后序遍历
    void postOrder(){
        postOrder(root);
    }

private:
    // 向以node为根的二叉搜索树中,插入节点(key, value)
    // 返回插入新节点后的二叉搜索树的根
    Node* insert(Node *node, Key key, Value value){

        if( node == NULL ){
            count ++;
            return new Node(key, value);
        }

        if( key == node->key )
            node->value = value;
        else if( key < node->key )
            node->left = insert( node->left , key, value);
        else    // key > node->key
            node->right = insert( node->right, key, value);

        return node;
    }

    // 查看以node为根的二叉搜索树中是否包含键值为key的节点
    bool contain(Node* node, Key key){

        if( node == NULL )
            return false;

        if( key == node->key )
            return true;
        else if( key < node->key )
            return contain( node->left , key );
        else // key > node->key
            return contain( node->right , key );
    }

    // 在以node为根的二叉搜索树中查找key所对应的value
    Value* search(Node* node, Key key){

        if( node == NULL )
            return NULL;

        if( key == node->key )
            return &(node->value);
        else if( key < node->key )
            return search( node->left , key );
        else // key > node->key
            return search( node->right, key );
    }

    // 对以node为根的二叉搜索树进行前序遍历
    void preOrder(Node* node){

        if( node != NULL ){
            cout<<node->key<<endl;
            preOrder(node->left);
            preOrder(node->right);
        }
    }

    // 对以node为根的二叉搜索树进行中序遍历
    void inOrder(Node* node){

        if( node != NULL ){
            inOrder(node->left);
            cout<<node->key<<endl;
            inOrder(node->right);
        }
    }

    // 对以node为根的二叉搜索树进行后序遍历
    void postOrder(Node* node){

        if( node != NULL ){
            postOrder(node->left);
            postOrder(node->right);
            cout<<node->key<<endl;
        }
    }

    void destroy(Node* node){

        if( node != NULL ){
            destroy( node->left );
            destroy( node->right );

            delete node;
            count --;
        }
    }
};


int main() {

    srand(time(NULL));
    BST<int,int> bst = BST<int,int>();

    int n = 10;
    for( int i = 0 ; i < n ; i ++ ){
        int key = rand()%n;
        // 为了后续测试方便,这里value值取和key值一样
        int value = key;
        cout<<key<<" ";
        bst.insert(key,value);
    }
    cout<<endl;

    // test size
    cout<<"size: "<<bst.size()<<endl<<endl;

    // test preOrder
    cout<<"preOrder: "<<endl;
    bst.preOrder();
    cout<<endl<<endl;

    // test inOrder
    cout<<"inOrder: "<<endl;
    bst.inOrder();
    cout<<endl<<endl;

    // test postOrder
    cout<<"postOrder: "<<endl;
    bst.postOrder();
    cout<<endl<<endl;

    return 0;
}
【Solution】 Here is a C++ implementation of the solution to the problem: ```cpp #include <iostream> #include <stack> using namespace std; struct TreeNode { int val; TreeNode* left; TreeNode* right; TreeNode(int x) : val(x), left(NULL), right(NULL) {} }; class Solution { public: TreeNode* treeToDoublyList(TreeNode* root) { if (!root) { return NULL; } stack<TreeNode*> s; TreeNode* p = root; TreeNode* pre = NULL; TreeNode* head = NULL; while (p || !s.empty()) { while (p) { s.push(p); p = p->left; } p = s.top(); s.pop(); if (pre) { pre->right = p; p->left = pre; } else { head = p; } pre = p; p = p->right; } head->left = pre; pre->right = head; return head; } }; void printBST(TreeNode* root) { if (root) { printBST(root->left); cout << root->val << " "; printBST(root->right); } } void printList(TreeNode* head) { TreeNode* p = head; do { cout << p->val << " "; p = p->right; } while (p != head); cout << endl; } void printListReverse(TreeNode* head) { TreeNode* p = head->left; do { cout << p->val << " "; p = p->left; } while (p != head->left); cout << endl; } int main() { TreeNode* root = new TreeNode(4); root->left = new TreeNode(2); root->right = new TreeNode(5); root->left->left = new TreeNode(1); root->left->right = new TreeNode(3); Solution s; TreeNode* head = s.treeToDoublyList(root); cout << "Binary Search Tree: "; printBST(root); cout << endl; cout << "Doubly Linked List: "; printList(head); cout << "Doubly Linked List in Reverse Order: "; printListReverse(head); return 0; } ``` 【Explanation】 To convert a binary search tree into a sorted circular doubly linked list, we can use a stack to perform an inorder traversal of the binary search tree. During the traversal, we maintain a pointer to the previous node visited and link the current node to the previous node. After the traversal, we link the head and tail of the linked list to form a circular doubly linked list. Finally, we can print the binary search tree, traverse the doubly linked list, and output all elements in positive and reverse order. 【Example】 Input: ``` 4 / \ 2 5 / \ 1 3 ``` Output: ``` Binary Search Tree: 1 2 3 4 5 Doubly Linked List: 1 2 3 4 5 Doubly Linked List in Reverse Order: 5 4 3 2 1 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值