簡體   English   中英

使用 C++ 在二叉搜索樹中存儲值

[英]Storing values in binary search tree with C++

標准二叉搜索樹的此代碼不包含任何信息,僅包含節點的值。 有什么辦法可以在節點中包含另一個值,例如年齡? 因此節點編號將是id並且它攜帶的值將是年齡。基本上每個節點都將包含一個鍵值對。 感謝您的幫助!

#include<stdio.h>
#include<stdlib.h>
#include<iostream>
using namespace std;

struct node
{
int key;
struct node *left, *right;
};

//to create a new BST node
struct node *newNode(int item)
{
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->key = item;
temp->left = temp->right = NULL;
return temp;
}

// to do inorder traversal of BST
void inorder(struct node *root)
{
if (root != NULL)
{
inorder(root->left);
printf("%d \n", root->key);
inorder(root->right);
}
}

//function to insert a new node with given key in BST
struct node* initialize(struct node* node, int key)
{
/* If the tree is empty, return a new node */
if (node == NULL) return newNode(key);

/* Otherwise, recur down the tree */
if (key < node->key)
node->left = initialize(node->left, key);
else if (key > node->key)
node->right = initialize(node->right, key);

/* return the  node pointer */
return node;
}
struct node* insert(node* root, int key)
{
    // Create a new Node containing
    // the new element
    node* newnode = newNode(key);

    // Pointer to start traversing from root and
    // traverses downward path to search
    // where the new node to be inserted
    node* x = root;

    // Pointer y maintains the trailing
    // pointer of x
    node* y = NULL;

    while (x != NULL) {
        y = x;
        if (key < x->key)
            x = x->left;
        else
            x = x->right;
    }

    // If the root is NULL i.e the tree is empty
    // The new node is the root node
    if (y == NULL) {
        y = newnode;
    }

    // If the new key is less then the leaf node key
    // Assign the new node to be its left child
    else if (key < y->key){
        y->left = newnode;
    }

    // else assign the new node its right child
    else{
        y->right = newnode;
    }
   
    // Returns the pointer where the
    // new node is inserted
    return y;
}

只需像這樣聲明節點

template <typename Key, typename Value>
struct node
{
    Key key;
    Value value;
    struct node *left, *right;
};

請注意,您應該使用運算符 new 而不是 malloc。

例如

template <typename Key, typename Value>
struct node<Key, Value>  *newNode( const Key &key, const Value &value )
{
    return new node<Key, Value> { key, value, nullptr, nullptr };
}

也就是說,如果您正在編寫 C++ 程序,則使用 C++ 結構。

可以通過以下方式定義函數insert

template <typename Key, typename Value>
struct node
{
    Key key;
    Value value;
    struct node *left, *right;
};

template <typename Key, typename Value>
struct node<Key, Value>  * newNode( const Key &key, const Value &value )
{
    return new node<Key, Value> { key, value, nullptr, nullptr };
}


template <typename Key, typename Value>
void insert( node<Key, Value> * &head, const Key &key, const Value &value )
{
    node<Key, Value> **current = &head;

    while ( *current != nullptr )
    {
        if ( key < ( *current )->key ) current = &( *current )->left;
        else current = &( *current )->right;
    }

    *current = newNode( key, value );
}                             

在主要你可以寫

int main()
{
    node<int, unsigned int> *head = nullptr;

    //…
}

如果這不是大學項目,請不要費心創建自己的二叉搜索樹,除非您真的想學習如何做到這一點。 由於 C++ 以 std::map 和 std::set 的形式為您提供二叉搜索樹

std::map<int, std::string> myMap

定義一個使用整數作為鍵並存儲 std::strings 的樹。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM