Skip to content

Instantly share code, notes, and snippets.

@fatihgokce
Last active December 10, 2022 17:08
Show Gist options
  • Select an option

  • Save fatihgokce/f92d0b5dd2ca1cdb58e14a395a886525 to your computer and use it in GitHub Desktop.

Select an option

Save fatihgokce/f92d0b5dd2ca1cdb58e14a395a886525 to your computer and use it in GitHub Desktop.

Revisions

  1. fatihgokce renamed this gist Dec 10, 2022. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. fatihgokce renamed this gist Dec 10, 2022. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. fatihgokce created this gist Apr 7, 2020.
    57 changes: 57 additions & 0 deletions rust_btree_rc
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,57 @@
    use std::cell::RefCell;
    use std::rc::Rc;
    #[derive(Debug, PartialEq)]
    struct Node2 {
    data: i32,
    left: Option<Rc<RefCell<Node2>>>,
    right: Option<Rc<RefCell<Node2>>>,
    }
    fn add2(tree:&mut Rc<RefCell<Node2>>, x: i32){
    let mut u=tree.borrow_mut();
    if u.data==x{
    return;
    }
    let t=u.data;

    let target_node = if x < t { &mut u.left } else { &mut u.right };
    match target_node {
    Some(node)=>add2(node, x),
    None=>{
    let new_node=Node2{data:x,left:None,right:None};
    let boxed=Some(Rc::new(RefCell::new(new_node)));


    *target_node=boxed;
    }
    }

    }
    fn travel2(tree: &Rc<RefCell<Node2>>) {

    let l=&tree.borrow().left;
    if let Some(t)=l{
    travel2(t);
    }
    let d=tree.borrow().data;
    println!("{}",d);
    let r=&tree.borrow().right;
    if let Some(t)=r{
    travel2(t);
    }
    }


    fn main() {
    let node=Node2{data:12,left:None,right:None};
    let mut tree=Rc::new(RefCell::new(node));
    let b=&mut tree;
    add2(b, 200);
    add2(b, 190);
    add2(b, 213);
    add2(b, 56);
    add2(b, 24);
    add2(b, 18);
    add2(b, 27);
    add2(b, 28);
    travel2(&tree);
    }