1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257


      
  // --snip--
  ///  This crate is the wrapper version of the tree. Few features added that invoke your curiosity. Tree is the collection of ideas jiggling in the mind.
  ///  In future versions, I will add more features to the tree crate 
  pub mod tree{

    

    /// Tree attributes 
    #[derive(Debug, Clone, PartialEq)]
      pub struct PrimaryNode{

      /// Data attribute: this branch hold some leaves. Each leave have own information
        pub data : Vec::<String>,

     /// Left attribute: this branch have connection with other branch of tree but which are only on left side
        pub left : Vec::<PrimaryNode>,

     /// Right attribute: this branch have connection with other branch of tree but which are only on right side
        pub right : Vec::<PrimaryNode>,
      }

      
      /// Enums are helpful because, it tells us why program fail ?
      #[derive(Debug)]
      pub enum Results{
        Ok,
        Err,
      }

      /// Either a branch is left one or right one 
      /// Imagine binary tree 
      #[derive(Debug)]
      pub enum Branch{
        Left,
        Right,
        Parent,
      }

      // --snip--
      /// Traits provide wrapper functions which are nesscary for the implementation
      pub trait Node{

        /// Leaf function create new leaves on the branch
        fn leaf(&mut self, node: PrimaryNode, branch : Branch) -> Vec::<PrimaryNode>;
        
        /// Extend branch on left side of the tree
        fn extend_left_branch(&mut self, node: PrimaryNode, branch : Branch, iter : u8) -> Vec::<PrimaryNode>;
        
        /// Extend branch on right side of the tree
        fn extend_right_branch(&mut self, node: PrimaryNode, branch : Branch, iter : u8) -> Vec::<PrimaryNode>;
        
        /// Count the number of branches on the tree
        fn count(&mut self, branch : Branch, iter : u8, max : u8) -> usize;
      }
      

      /// Implentation methods are implemented when a user import my crate
      impl Node for PrimaryNode{
        
        // --snip--
        // leaf provide basic functionality for branch 
        // tree have either left or right branch
        // leaves on the branches grow with branch label 
        fn leaf(&mut self, node: PrimaryNode, branch : Branch) -> Vec::<PrimaryNode>{
          
          // either first leaf add on right or left  side branch.
          // in case parent provide then no data will add on any branch because parent is born before leaf birth.
          match branch{
            Branch::Left => {self.left.push(node); self.left.clone() },
            Branch::Right => {self.right.push(node); self.right.clone()},
            _ => {Vec::new()},
          }
        }

        /// create a new branch with new leaf
        fn extend_left_branch(&mut self, node: PrimaryNode, branch : Branch, iter : u8) -> Vec::<PrimaryNode> {

          match branch{
            Branch::Left => {self.left[iter as usize].left.push(node); self.left[iter as usize].left.clone()},
            Branch::Right => {self.left[iter as usize].right.push(node); self.left[iter as usize].right.clone()}
            _=>{Vec::new()},
          }
        }

        fn extend_right_branch(&mut self, node: PrimaryNode, branch : Branch, iter : u8) -> Vec::<PrimaryNode>{
          
          
          match branch{
            Branch::Left => {self.left[iter as usize].left.push(node); self.left[iter as usize].left.clone()},
            Branch::Right => {self.right[iter as usize].right.push(node); self.right[iter as usize].right.clone()}
            _=>{Vec::new()},
          }
        }

        fn count(&mut self, branch : Branch, iter : u8, max : u8) -> usize {
          
          // total number of branches
          let mut counter : usize = 0;
          
          match branch{
            Branch::Left => {
              
              // in case left branch is not empty then increment
              if !self.left[iter as usize].left.is_empty(){
                counter += 1;
              }
              
              // iterate all the branches on the left of the root
              for i in 0 .. max {
                if !self.left[iter as usize].left[i as usize].data.is_empty() {
                  counter += 1;
                }
              }
              counter
            },
            Branch::Right => {
              
              // in case right branch is not empty then increment
              if !self.left[iter as usize].right.is_empty(){
                counter +=1;
              }
              
              // iterate all the branches on the left of the root
              for i in 0 .. max {
                if !self.left[iter as usize].left[i as usize].data.is_empty() {
                  counter += 1;
                }
              }
              counter
            },
            _=>{

              // root of the tree
              counter += 1;
              counter
            },
          }
        }
        
      }
    }

    // --snip 

    /// tree_test modue provide nesscary test methods
    /// all these test execute succeed under the assumption environment
    pub mod tree_tests{

      
      #[cfg(test)]
      mod tests {

        // crates imported 
        pub use crate::tree::PrimaryNode;
        pub use crate::tree::*;
        
        #[test]

        // --snip--
        /// all the test in one main function called init
        fn init(){

          // inital test -- add some data 
          let mut ttree = Vec::<PrimaryNode>::new();
          let mut node = Vec::<String>::new();
          node.push("hello".to_string());
          ttree.push(PrimaryNode{
            data: node,
            left: Vec::<PrimaryNode>::new(),
            right: Vec::<PrimaryNode>::new(),
          });

          // create new nodes 
          let mut another = Vec::<String>::new();
          another.push("world".to_string());
          let other = PrimaryNode{data: another, left : Vec::<PrimaryNode>::new(), right : Vec::<PrimaryNode>::new()};
          let n0 = ttree[0].leaf(other,Branch::Left);
          assert_ne!(n0[0].data.is_empty(),true);

          // create left_child on root
          let mut left_child = Vec::<String>::new();
          left_child.push("".to_string());
          let other = PrimaryNode{data: left_child, left : Vec::<PrimaryNode>::new(), right : Vec::<PrimaryNode>::new()};
          let extend = ttree[0].extend_left_branch(other, Branch::Left, 0);
          assert_eq!(extend[0].data.is_empty(), false);

          // count root childs
          let child_count = ttree[0].count(Branch::Left, 0, 0);
          assert_eq!(child_count, 1);

        }


      }

    }

use crate::tree::*;

// --snip--
fn main() {

  // create tree structure 
    let mut tree : Vec::<PrimaryNode> = Vec::<PrimaryNode>::new();
  
  // tree vector have finite length   
    let height : usize = 5;

    // tree counter represent height of the tree
    if tree.iter().count() < height   {

      // create a new first leaf genesis   
      tree.push(new_node(String::from("Name : Ali Hassan")));

      let children = new_node(String::from("Occuption: Engineer"));
      let _ = tree[0].leaf(children, Branch::Left);
      
      let children = new_node(String::from("Born: 1995"));
      let _ = tree[0].leaf(children, Branch::Right);

      let children = new_node(String::from("Graduate : UMT"));
      let _ = tree[0].extend_left_branch(children,Branch::Left, 0);

      let children = new_node(String::from("Affliate : Computer Science and Economics"));
      let _ = tree[0].extend_left_branch(children,Branch::Right,  0);

      let children = new_node(String::from("Graduate Year : 2017/11/14"));
      let _ = tree[0].extend_right_branch(children,Branch::Right, 0);

      let children = new_node(String::from("Class : Entrepreneur"));
      let _ = tree[0].extend_right_branch(children,Branch::Left, 0);
      
      let left_count = tree[0].count(Branch::Left, 0, (height - height+2) as u8);
      let right_count = tree[0].count(Branch::Right, 0, (height - height+2) as u8);
      let parent_count = tree[0].count(Branch::Parent, 0, (height - height+2) as u8);
        
      println!(" value :{:#?}", tree);
      println!("left count: {:?}, right count: {:?} parent count: {:?}", left_count, right_count, parent_count);     
    }
}


// create a new leaf
fn new_node(str : String) -> PrimaryNode{
  
  let mut node_data: Vec::<String> = Vec::<String>::new();
  node_data.push(str);

  let child : PrimaryNode = PrimaryNode{data : node_data, 
      left : Vec::<PrimaryNode>::new(), 
      right : Vec::<PrimaryNode>::new()
    };
  child 
}