日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

bst 删除节点_在BST中删除大于或等于k的节点

發(fā)布時間:2025/3/11 编程问答 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 bst 删除节点_在BST中删除大于或等于k的节点 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

bst 刪除節(jié)點

Problem statement:

問題陳述:

Given a BST and a value x, write a function to delete the nodes having values greater than or equal to x. The function will return the modified root.

給定一個BST和一個值x ,編寫一個函數(shù)刪除值大于或等于x的節(jié)點 。 該函數(shù)將返回修改后的根。

Example:

例:

Input BST8/ \4 10/ \ / \3 5 9 11Input X: 10After deletion:8/ \4 9/ \ 3 5

Solution:

解:

Basic properties of BST:

BST的基本屬性:

  • All node values are distinct.

    所有節(jié)點值都是不同的。

  • The left child node is always smaller than the root & right child node is always greater than the root.

    左子節(jié)點始終小于根節(jié)點,右子節(jié)點始終大于根節(jié)點。

  • Thus it's clear whenever a root has a value greater than or equal to the input value X, the entire right subtree and root need to be deleted, but not the left one.

    因此,很明顯,只要根的值大于或等于輸入值X ,就需要刪除整個右子樹和根,而不必刪除左子樹和根。

    Algorithm:

    算法:

    FUNCTION buildTree (root,X)IF (root==NULL)Return NULL;END IFIF (root->data is equal to X)return root->left; //returning the left subtree basicallyELSE IF(root->data>key)//recur for the left subtree since left subtree may also have nodes //that need to be deleted due to greater or equal valuesreturn buildTree(root->left, X); ELSE//root is not at all to be deleted, same for left subtree, //recursively process right subtreeroot->right=buildTree(root->right, X);return root; END FUNCTION

    The above function actually builds the tree deleting the nodes having greater and equal value than X.

    上面的函數(shù)實際上是在構(gòu)建樹,刪除值大于和等于X的節(jié)點。

    Example with Explanation:

    解釋示例:

    Nodes are represented by respected values for better understanding8/ \4 10/ \ / \3 5 9 11Input X: 10At main we callbuildTree (8, 10) ------------------------------------------------buildTree (8, 10) root not NULL 8<10 Thus root->left= 8->left root->right=buildTree (8->right, 10) //buildTree (10, 10) ------------------------------------------------buildTree (10, 10) root not NULL 10==10 Thus return root->left= 10->left=9 ------------------------------------------------Hence At buildTree (8, 10) Thus root->left= 8->left root->right=9 (9 is the root to the remaining subtree which is NULL here luckily)So the tree actually becomes8/ \4 9/ \ 3 5 .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}} .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}}

    C++ Implementation:

    C ++實現(xiàn):

    #include <bits/stdc++.h> using namespace std;class Node{public: int data; //valueNode *left; //pointer to left childNode *right; //pointer to right child };// creating new node Node* newnode(int data) { Node* node = (Node*)malloc(sizeof(Node)); node->data = data; node->left = NULL; node->right = NULL; return(node); }Node* buildTree(Node* root,int key){if(!root)return NULL;if(root->data==key){return root->left; //only left subtree not deleted}else if(root->data>key)return buildTree(root->left,key); //recur for left subtreeelse{//root not deleted//left subtree not deleted//buld right subtreeroot->right=buildTree(root->right,key); return root;} }Node* deleteNode(Node* root, int key) {root=buildTree(root,key);return root; }void levelwisedisplay(Node *root) {//to display the tree level wisequeue<Node*> q;Node* temp;int count=0;q.push(root);q.push(NULL);while(!q.empty()){temp=q.front();q.pop();if(temp==NULL){if(!q.empty())q.push(NULL);cout<<"\nend of level "<<count++<<endl;}else{cout<<temp->data<<" ";if(temp->left)q.push(temp->left);if(temp->right)q.push(temp->right);}}return ; }int main(){cout<<"tree is built as per example\n";Node *root=newnode(8); root->left= newnode(4); root->right= newnode(10); root->right->right=newnode(11);root->right->left=newnode(9);root->left->left=newnode(3); root->left->right=newnode(5);printf("Displaying level wise\n");levelwisedisplay(root);root=deleteNode(root,10);//as per exampleprintf("after deleting nodes greater or equal to 10\n");levelwisedisplay(root);return 0; }

    Output

    輸出量

    tree is built as per example Displaying level wise 8 end of level 0 4 10 end of level 1 3 5 9 11 end of level 2 after deleting nodes greater or equal to 10 8 end of level 0 4 9 end of level 1 3 5 end of level 2

    翻譯自: https://www.includehelp.com/icp/delete-nodes-greater-than-or-equal-to-k-in-a-bst.aspx

    bst 刪除節(jié)點

    總結(jié)

    以上是生活随笔為你收集整理的bst 删除节点_在BST中删除大于或等于k的节点的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。