public Node insert(Node root, MobileObject /*int*/ v) { if (root.value == null) { root = new Node(); root.value = v; root.parent = null; } // insertion logic, if the value (v )is < root, insert to the root.left // otherwise it's >=, so insert to the right //Objects are sorted based on their distance from origin //Parent tracking takes place here, but won't work for MOBS no matter how you do it else if (v.Dfo < root.value.Dfo) { Node Left = insert(root.left, v); root.left = Left; Left.parent = root; } else { Node Right = insert(root.right, v); root.right = Right; Right.parent = root; } return(root); }
public void Delete(MobileObject key) { //first find the node in the tree to delete and assign to item pointer/reference Node item = Find(key); Node X = null; Node Y = null; if (item == null) { Console.WriteLine("Nothing to delete!"); return; } if (item.left == null || item.right == null) { Y = item; } else { Y = TreeSuccessor(item); } if (Y.left != null) { X = Y.left; } else { X = Y.right; } if (X != null) { X.parent = Y; } if (Y.parent == null) { root = X; } else if (Y == Y.parent.left) { Y.parent.left = X; } else { Y.parent.left = X; } if (Y != item) { item.value = Y.value; } if (Y.colour == Color.Black) { DeleteFixUp(X); } }
/// <summary> /// Find item in the tree /// </summary> /// <param name="key"></param> /// <summary> /// Insert a new object into the RB Tree /// </summary> /// <param name="item"></param> public void Insert(MobileObject item) { Node newItem = new Node(); newItem.value = item; if (root == null) { root = newItem; root.colour = Color.Black; return; } Node Y = null; Node X = root; while (X != null) { Y = X; if (newItem.value.CompareTo(X.value) == -1) { X = X.left; } else { X = X.right; } } newItem.parent = Y; if (Y == null) { root = newItem; } else if (newItem.value.CompareTo(Y.value) == -1) { Y.left = newItem; } else { Y.right = newItem; } newItem.left = null; newItem.right = null; newItem.colour = Color.Red; //colour the new node red InsertFixUp(newItem); //call method to check for violations and fix }
public Node insert(Node root, MobileObject v) { if (root == null) { root = new Node(); root.value = v; } else if (v.CompareTo(root.value) == -1) { root.left = insert(root.left, v); root = balance_tree(root); } else { root.right = insert(root.right, v); root = balance_tree(root); } return(root); }
/// <summary> /// Deletes a specified value from the tree /// </summary> /// <param name="item"></param> /// public Node Find(MobileObject key) { bool isFound = false; Node temp = root; Node item = null; while (!isFound) { if (temp == null) { break; } if (key.CompareTo(temp.value) == -1) { temp = temp.left; } if (key.CompareTo(temp.value) == 1) { temp = temp.right; } if (key.CompareTo(temp.value) == 0) { isFound = true; item = temp; } } if (isFound) { Console.WriteLine("{0} was found", key); return(temp); } else { Console.WriteLine("{0} not found", key); return(null); } }
static void Main(string[] args) { Node root = new Node(); BinarySearchTree bst = new BinarySearchTree(); AVLTree avl = new AVLTree(); RB rb = new RB(); SplayTree <MobileObject, MobileObject> splay = new SplayTree <MobileObject, MobileObject>(); Stopwatch bstWatch = new Stopwatch(); Stopwatch avlWatch = new Stopwatch(); Stopwatch rbWatch = new Stopwatch(); Stopwatch splayWatch = new Stopwatch(); //My laptop refuses to load more than 100 or so objects, it just won't do it MobileObject[] MOBArray = new MobileObject[100]; MobileObject[] MOBArray2 = new MobileObject[20]; Random rnd = new Random(); //testing for (int i = 0; i < MOBArray2.Length; i++) { MOBArray2[i] = new Vehicle(i.ToString(), i, i, i, i, i, i); } foreach (MobileObject item in MOBArray2) { bst.insert(root, item); avl.insert(root, item); rb.Insert(item); splay.Add(item, item); } Console.WriteLine("rb tree find function test: found matching mob, dfo = " + MOBArray2[3].Dfo); Console.WriteLine(); //For sorted insert for (int i = 0; i < MOBArray.Length; i++) { MOBArray[i] = new Vehicle(i.ToString(), i, i, i, i, i, i); } //Unsorted insert /* for(int i = 0; i<MOBArray.Length; i++) * { * MOBArray[i] = new Vehicle(i.ToString(), i, rnd.Next(100), rnd.Next(100), rnd.Next(100), rnd.Next(100), rnd.Next(100)); * } */ bstWatch.Start(); foreach (MobileObject item in MOBArray) { bst.insert(root, item); } bstWatch.Stop(); avlWatch.Start(); foreach (MobileObject item in MOBArray) { avl.insert(root, item); } avlWatch.Stop(); rbWatch.Start(); foreach (MobileObject item in MOBArray) { rb.Insert(item); } rbWatch.Stop(); splayWatch.Start(); foreach (MobileObject item in MOBArray) { splay.Add(item, item); } splayWatch.Stop(); Console.WriteLine("BST time(ticks) = " + bstWatch.ElapsedTicks); Console.WriteLine("AVL time(ticks) = " + avlWatch.ElapsedTicks); Console.WriteLine("RB time(ticks) = " + rbWatch.ElapsedTicks); Console.WriteLine("splay time(ticks) = " + splayWatch.ElapsedTicks); Console.ReadLine(); }