Fixed delete method - Or maybe not
This commit is contained in:
		@@ -7,16 +7,18 @@ namespace BinaryTree
 | 
			
		||||
        static void Main(string[] args)
 | 
			
		||||
        {
 | 
			
		||||
            tree tree = new tree();
 | 
			
		||||
            tree.Add(5);
 | 
			
		||||
            tree.Add(7);
 | 
			
		||||
            tree.Add(6);
 | 
			
		||||
            tree.Add(8);
 | 
			
		||||
            tree.Add(3);
 | 
			
		||||
            tree.Add(1);
 | 
			
		||||
            tree.Add(4);
 | 
			
		||||
            tree.Add(0.5);
 | 
			
		||||
 | 
			
		||||
            tree.Delete(1);
 | 
			
		||||
            Random random = new Random();
 | 
			
		||||
 | 
			
		||||
            for (var i = 0; i < 1000; i++)
 | 
			
		||||
            {
 | 
			
		||||
                tree.Add(random.Next(0, 10000));
 | 
			
		||||
            }
 | 
			
		||||
            
 | 
			
		||||
            
 | 
			
		||||
            
 | 
			
		||||
 | 
			
		||||
            tree.Delete(tree.Root.Data);
 | 
			
		||||
 | 
			
		||||
            Console.ReadLine();
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -82,8 +82,9 @@ namespace BinaryTree
 | 
			
		||||
            if (Current.GetLeft() != null && Current.GetRight() != null)
 | 
			
		||||
            {
 | 
			
		||||
                //If it has 2 children
 | 
			
		||||
                node temp = new node();
 | 
			
		||||
                temp = Current.GetLeft();
 | 
			
		||||
                node temp = Current.GetLeft();
 | 
			
		||||
 | 
			
		||||
                //Traverse to node that will replace Current
 | 
			
		||||
                while(true){
 | 
			
		||||
                    if (temp.GetRight() != null)
 | 
			
		||||
                    {
 | 
			
		||||
@@ -94,14 +95,22 @@ namespace BinaryTree
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                Current.Data = temp.Data;
 | 
			
		||||
                
 | 
			
		||||
                //Find if the temp node is a left or a right now before deleting. 
 | 
			
		||||
                //Adopt subtree Nodes// -- Subtrees are lost (If temp node is only 1 level down, data is lost)
 | 
			
		||||
                // if (temp.GetLeft() != null)
 | 
			
		||||
                // {
 | 
			
		||||
                //     temp.GetParent().SetRight(temp.GetLeft());
 | 
			
		||||
                // }else{
 | 
			
		||||
                //     temp.GetParent().SetRight(null);
 | 
			
		||||
                // }
 | 
			
		||||
 | 
			
		||||
                if (temp.GetLeft() != null)
 | 
			
		||||
                {
 | 
			
		||||
                    temp.GetParent().SetRight(temp.GetLeft());
 | 
			
		||||
 | 
			
		||||
                if(temp.GetParent() == Current){
 | 
			
		||||
                    Current.SetLeft(null);
 | 
			
		||||
                }else{
 | 
			
		||||
                    temp.GetParent().SetRight(null);
 | 
			
		||||
                    Current.SetLeft(temp.GetLeft());
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                return true;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@@ -136,6 +145,5 @@ namespace BinaryTree
 | 
			
		||||
            
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user