Get the App
SLTechnology News&Howtos  ›  Development  › 

How to write the code of C++ to realize binary search tree?

Shulou Source: shulou.com Published: 2022-05-31 22:47:17 09月21日 Update

Most people do not understand the knowledge points of this article "how to write the code for C++ to achieve a binary search tree", so the editor summarizes the following contents for you. The content is detailed, the steps are clear, and it has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "C++ to achieve binary search tree code how to write" article.

The specific code is as follows:

Class BST {public: value Node* left; Node* right; int N of the key int value;// node of the struct Node {key node / number of leaf nodes of the node Node (int _ key, int _ value, int _ N) {key = _ key; value = _ value; N = _ N;}; BST (); ~ BST (); void put (int key, int value) Int get (int key); int deleteKey (int key); private: Node* _ deleteKey (int key, Node* x); Node* _ deleteMin (Node* x); int size (Node* x); int _ get (int key, Node* x); Node* _ put (int key, int value,Node * x); Node* min (Node* x); Node* root;}; inline int BST::size (Node* x) {if (x = nullptr) return 0; return x-> N } int BST::_get (int key, Node * x) {if (x = = nullptr) return 0; if (x-> key)

< key)_get(key, x->

Right); else if (x-> key > key) _ get (key, x-> left); else {return x-> value;} return 0;} BST::Node* BST::_put (int key, int value, Node* x) {if (x = = nullptr) {Node* tmp = new Node (key, value, 1); return tmp;} if (x-> key > key) {x-> left=_put (key, value, x-> left);} else if (x-> key)

< key) { x->

Right=_put (key, value, x-> right);} else x-> key = key; x-> N = size (x-> left) + size (x-> right) + 1; return x;} BST::Node* BST::min (Node* x) {if (x-> left = = nullptr) return x; return min (x-> left);} BST::BST () {} BST::~BST () {} void BST::put (int key, int value) {root=_put (key, value, root) } int BST::get (int key) {return _ get (key, root);} BST::Node* BST::_deleteKey (int key, Node* x) {if (x-> key > key) x-> left = _ deleteKey (key, x-> left); else if (x-> key)

< key)x->

Right = _ deleteKey (key, x-> right); else {if (x-> left = = nullptr) return x-> right; else if (x-> right = = nullptr) return x-> left; else {Node * tmp = x; x = min (tmp- > right); x-> left = tmp- > left; x-> right = _ deleteMin (tmp- > right);} x-> N = size (x-> left) + size (x-> right) + 1; return x } BST::Node* BST::_deleteMin (Node* x) {if (x-> left = = nullptr) return x-> right; x-> left = _ deleteMin (x-> left); x-> N = size (x-> left) + size (x-> right) + 1; return x;} int BST::deleteKey (int key) {return _ get (key, root) } the above is about the content of this article, "how to write the code for C++ to achieve a binary search tree". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.

Tags: Content code nodes clips search articles knowledge articles values leaves most that is number more steps knowledge points industry information information channels channels Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux OPPO Reno MySQL Huawei Redmi