Skip to content

Commit f150288

Browse files
Merge pull request #2868 from NarutoUchiha39/main
2 parents bb722f7 + c8ce505 commit f150288

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed

rust/0450-delete-node-in-a-bst.rs

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
3+
In this code we have to delete the node of a binary search tree the logic is as follows:
4+
5+
1) If the value of the current node is greater than the value of the key to be deleted go left
6+
7+
2) If the value of the current node is less than the value of the key to be deleted go right
8+
9+
3) If the value of the current node is the value to be deleted there can be two senarios :
10+
11+
a) The left side of the node is none in that case simply return the right side of the node
12+
13+
b) The right side of the node is none in that case simply return the left side of the node
14+
15+
c) Both left and right side of the node are not none in that case:
16+
17+
* Go to the right branch of the tree and then go as left as possible. Return the number
18+
19+
* The value of the node to be deleted should be replaced by the value return by the inorder_successor function
20+
21+
* Recursively call the delete node function in order to delete the leaf node whose value we returned from the inorder_successor function
22+
23+
24+
*/
25+
26+
27+
use std::rc::Rc;
28+
use std::cell::RefCell;
29+
30+
#[derive(Debug, PartialEq, Eq)]
31+
pub struct TreeNode {
32+
pub val: i32,
33+
pub left: Option<Rc<RefCell<TreeNode>>>,
34+
pub right: Option<Rc<RefCell<TreeNode>>>,
35+
}
36+
37+
impl TreeNode {
38+
#[inline]
39+
pub fn new(val: i32) -> Self {
40+
TreeNode {
41+
val,
42+
left: None,
43+
right: None
44+
}
45+
}
46+
}
47+
48+
struct Solution{}
49+
50+
impl Solution {
51+
pub fn delete_node(root: Option<Rc<RefCell<TreeNode>>>, key: i32)->Option<Rc<RefCell<TreeNode>>>{
52+
Solution::helper(root.as_ref(), key)
53+
}
54+
55+
pub fn helper(root:Option<&Rc<RefCell<TreeNode>>>,key:i32)->Option<Rc<RefCell<TreeNode>>>{
56+
57+
if root.is_none(){
58+
return None;
59+
}
60+
61+
let current_node: &Rc<RefCell<TreeNode>> = root.unwrap();
62+
63+
if current_node.borrow().val > key{
64+
let l = Solution::helper(current_node.borrow().left.as_ref(), key);
65+
current_node.borrow_mut().left = l;
66+
}else if current_node.borrow().val < key {
67+
let r = Solution::helper(current_node.borrow().right.as_ref(), key);
68+
current_node.borrow_mut().right = r;
69+
}
70+
71+
else{
72+
73+
if current_node.borrow().left.is_none(){
74+
return current_node.borrow().right.clone();
75+
} else if current_node.borrow().right.is_none() {
76+
return current_node.borrow().left.clone();
77+
}
78+
79+
else{
80+
81+
let number = Solution::inorder_successor(current_node.borrow().right.as_ref(), key);
82+
if let Some(num) = number{
83+
let r = Solution::helper(current_node.borrow().right.as_ref(), num);
84+
current_node.borrow_mut().right = r;
85+
current_node.borrow_mut().val = num;
86+
}
87+
}
88+
}
89+
90+
return Some(current_node.clone());
91+
92+
}
93+
94+
pub fn inorder_successor(root:Option<&Rc<RefCell<TreeNode>>>,value:i32)->Option<i32>{
95+
96+
if let Some(node) = root {
97+
if node.borrow().left.is_none(){
98+
return Some(node.borrow().val);
99+
}
100+
101+
else
102+
{
103+
return Solution::inorder_successor(node.borrow().left.as_ref(), value);
104+
}
105+
}
106+
107+
return None
108+
}
109+
}

0 commit comments

Comments
 (0)