static TriNode Add(TriNode n, string s, int pos) { if (n == null) { n = new TriNode(s[pos]); } if (s[pos] < n.data) { n.small = Add(n.small, s, pos); } else if (s[pos] > n.data) { n.large = Add(n.large, s, pos); } else { if (pos < s.Length - 1) { n.equal = Add(n.equal, s, ++pos); } else { n.end = true; } } return(n); }
static bool Search(TriNode n, string s) { if (String.IsNullOrEmpty(s)) { return(true); } if (n == null) { return(false); } int pos = 0; TriNode current = n; while (current != null) { if (s[pos] < current.data) { current = current.small; } else if (s[pos] > current.data) { current = current.large; } else { pos++; if (pos == s.Length && current.end == true) { return(true); } current = current.equal; } } return(false); }
static bool Search(TriNode n, string s) { if(String.IsNullOrEmpty(s))return true; if(n==null)return false; int pos=0; TriNode current=n; while(current!=null) { if(s[pos]<current.data) { current=current.small; } else if(s[pos]>current.data) { current=current.large; } else { pos++; if(pos==s.Length && current.end==true) { return true; } current=current.equal; } } return false; }
static TriNode Add(TriNode n,string s,int pos) { if(n==null) { n=new TriNode(s[pos]); } if(s[pos]<n.data) { n.small=Add(n.small,s,pos); } else if(s[pos]>n.data) { n.large=Add(n.large,s,pos); } else { if(pos<s.Length-1) { n.equal=Add(n.equal,s,++pos); } else { n.end=true; } } return n; }
public TriNode(char data) { this.data = data; small = null; equal = null; large = null; end = false; }
static void Traverse(TriNode n, int depth) { if (n != null) { Traverse(n.small, depth); c[depth] = n.data; if (n.end) { c[depth + 1] = '\0'; //Print(c); Console.WriteLine(new string(c)); } Traverse(n.equal, depth + 1); Traverse(n.large, depth); } }
public bool Equals(TriNode obj) { // STEP 1: Check for null if nullable (e.g., a reference type) if (obj == null) { return(false); } // STEP 2: Check for ReferenceEquals if this is a reference type if (ReferenceEquals(this, obj)) { return(true); } // STEP 3: Compare identifying fields for equality. return((this.vert_index.Equals(obj.vert_index))); }
// static void Print(char[] c) // { // int i=0; // while(c[i]!='\0') // { // Console.Write(c[i]); // i++; // } // } static void Main() { TriNode root = null; root = Add(root, "cat", 0); Add(root, "cab", 0); Add(root, "bug", 0); if (Search(root, "caddd")) { Console.WriteLine("found"); } else { Console.WriteLine("not found"); } Traverse(root, 0); // Console.WriteLine(root.data); // Console.WriteLine(root.equal.data); // Console.WriteLine(root.equal.equal.data); // Console.WriteLine(root.equal.equal.small.data); // Console.WriteLine(root.small.data); // Console.WriteLine(root.small.equal.data); }
public IEnumerator create_child(int vert_index, TriNode node, Mesh mesh, Transform transform, GameObject slime_template) { yield return(new WaitForSeconds(0.5f + Random.Range(0, 1.5f))); Vector3 spawn_world = transform.localPosition + mesh.vertices[vert_index] * transform.localScale.x; GameObject go = GameObject.Instantiate(slime_template); go.transform.parent = this.gameObject.transform; Slime child_slime = go.AddComponent <Slime>(); child_slime.maxDepth = this.maxDepth; child_slime.childScale = this.childScale; child_slime.depth = this.depth + 1; go.transform.localScale = Vector3.one * childScale; go.transform.position = spawn_world; if (child_slime.depth < child_slime.maxDepth) { child_slime.create_parent(node.neightbours, mesh, transform, slime_template); } }
public TriNode(char data) { this.data=data; small=null; equal=null; large=null; end=false; }
static void Traverse(TriNode n,int depth) { if(n!=null) { Traverse(n.small,depth); c[depth]=n.data; if(n.end) { c[depth+1]='\0'; //Print(c); Console.WriteLine(new string(c)); } Traverse(n.equal,depth+1); Traverse(n.large,depth); } }
Dictionary <int, TriNode> map = new Dictionary <int, TriNode>(); // mesh.verticies index to TriNode map private void OnEnable() { GetComponent <MeshFilter>().mesh = mesh; for (int tri_index = 0; tri_index < mesh.triangles.Length; tri_index = tri_index + 3) { int v0_i = mesh.triangles[tri_index]; int v1_i = mesh.triangles[tri_index + 1]; int v2_i = mesh.triangles[tri_index + 2]; // Get current vertice TriNode node0; TriNode node1; TriNode node2; // Node 0 if (map.ContainsKey(v0_i)) { node0 = map[v0_i]; } else { node0 = new TriNode(v0_i); map.Add(v0_i, node0); } // Node 1 if (map.ContainsKey(v1_i)) { node1 = map[v1_i]; } else { node1 = new TriNode(v1_i); map.Add(v1_i, node1); } // Node 2 if (map.ContainsKey(v2_i)) { node2 = map[v2_i]; } else { node2 = new TriNode(v2_i); map.Add(v2_i, node2); } // Add neighbours if (!node0.neightbours.Contains(node1)) { node0.add_neighbour(node1); } if (!node0.neightbours.Contains(node2)) { node0.add_neighbour(node2); } if (!node1.neightbours.Contains(node0)) { node1.add_neighbour(node0); } if (!node1.neightbours.Contains(node2)) { node1.add_neighbour(node2); } if (!node2.neightbours.Contains(node0)) { node2.add_neighbour(node0); } if (!node2.neightbours.Contains(node1)) { node2.add_neighbour(node1); } } //Vector3 spawn_world = transform.localPosition + mesh.vertices[origin_index] * transform.localScale.x; //Debug.DrawRay(spawn_world, mesh.normals[origin_index] * scale, Color.red, 60, false); //map[origin_index].visited = true; //foreach (TriNode node in map[origin_index].neightbours) //{ // if (!node.visited) // { // Debug.DrawRay(transform.localPosition + mesh.vertices[node.vert_index] * transform.localScale.x, mesh.normals[node.vert_index] * scale, Color.magenta, 60, true); // node.visited = true; // } //} //foreach (var node in map.Values) //{ // node.visited = false; //} foreach (int i in origin_indexes) { StartCoroutine(spawn_point(i, 0.1f)); StartCoroutine(spawn_point(i, 1.0f + Random.Range(0, 2f))); StartCoroutine(spawn_point(i, 2.0f + Random.Range(0, 3.5f))); StartCoroutine(spawn_point(i, 3.0f + Random.Range(0, 5.2f))); StartCoroutine(spawn_point(i, 4.0f + Random.Range(0, 7f))); } }
public void add_neighbour(TriNode neighbour) { this.neightbours.Add(neighbour); }