Ejemplo n.º 1
0
	public void PrintTree(Node r) {
		if(r==null) return;
					
		Console.WriteLine("{0}, {1}",r.key,r.color);
		if(r.left!=null) PrintTree(r.left);
		if(r.right!=null) PrintTree(r.right);
		}
Ejemplo n.º 2
0
	public bool InsertNode() {
			Node n=BuildNode();
			
			if(n==null) return false;

			if(root==null) {
				root=n;
				root.color="black";
				return true;
			}

			//Rule 1: Every node has value (key) greater than left-child & less than the right-child value
			// so traverse tree to find it's place

			Node temp=root;
			while((temp.left!=null) || (temp.right!=null)) {
				if(n.key < temp.key) {
					if(temp.left != null) {
					     temp=temp.left;
					     continue;
					}
					else {
					    temp.left=n;
					    n.parent=temp;
					    return true;
					}
				}
				else if(n.key > temp.key){
					if(temp.right != null) {
					    temp=temp.right;
					    continue;
					}
					else {
					    temp.right=n;
					    n.parent=temp;
					    return true;
					}
				}
			}
			if(n.key < temp.key) 
				temp.left=n;
			else if(n.key > temp.key)
				temp.right=n;
			n.parent=temp;

			// Adjust tree after insertion
			AdjustTree(n);
			return true;
	}
Ejemplo n.º 3
0
	public void SetNodeColor(Node n) {
		
		//Rule 5. Red node which is not a leaf has black children(consecutive red nodes not allowed)		
		
		if(n.color=="red") {   //set child color as black
			if(n.left!=null) n.left.color="black";
			if(n.right!=null) n.right.color="black";
		}

		if(n.left!=null) SetNodeColor(n.left);
		if(n.right!=null) SetNodeColor(n.right);
	}
Ejemplo n.º 4
0
	public Node FindNode(Node r,int k) {
		//Console.WriteLine("In FindNode()");
		try {
			if(k==r.key) return r;
			
			else if(k<r.key) {
			    if(r.left!=null) return(FindNode(r.left,k));
			    else return null; // skip this node
			}
			else {
		    	if(r.right!=null) return(FindNode(r.right,k));
		    	else return null;
			}
		}catch(Exception e) {
			Console.WriteLine("Exception caught in FindNode(): "+e);
			return null;
		}
	}
Ejemplo n.º 5
0
	public string WhichChild(Node n) {
		if(n==root) return "none";
		if(n==n.parent.left) return "left";
		else return "right";
	}
Ejemplo n.º 6
0
		public void RotateTree(Node x) {
		Node uncle=null;
		Node sibling=null;

		if(x.parent.color=="red") {
			
			if(WhichChild(x.parent)=="left") 	// uncle is the right child
				//if((x.parent).parent.right != null) 
					uncle=(x.parent).parent.right;
			else if(WhichChild(x.parent)=="right") 	// uncle is the left child
				//if((x.parent).parent.left != null)
					 uncle=(x.parent).parent.left;

			if(WhichChild(x)=="left") // x is left child
				//if(x.parent.right != null) 
				sibling=x.parent.right;	
			else  //x is right child
				//if(x.parent.left != null)
				sibling=x.parent.left;

		}
		else return;

		//Rotation Type 1 : x is red, p[x] is red and uncle=null, sibling=null
		if((uncle==null)&&(sibling==null)) {

			//Different orientation...Works!

			if(WhichChild(x)!= WhichChild(x.parent)) {
				int temp = x.key;
				x.key=x.parent.key;
				x.parent.key=temp;

				// reverse orientations
				if(WhichChild(x)=="left") {
					x.parent.right=x;
					x.parent.left=null;
				}
				else {
					x.parent.left=x;
					x.parent.right=null;
				}
				RotateTree(x);
			}

			// Same orientation..Works!

			else {
			if(x.parent.parent != root) {

				if(WhichChild(x.parent.parent) == "left") 
					x.parent.parent.parent.left=x.parent;
				else x.parent.parent.parent.right=x.parent;
			}
	
			else root=x.parent;

			if(WhichChild(x)=="left") {
				(x.parent).right=(x.parent).parent;
				
				((x.parent).right).parent=x.parent;
				
				/*if(WhichChild(x.parent) == "left") { 
					Console.WriteLine("ok");
					x.parent.parent.left=null;
				}
				else { ((x.parent).right).right=null;}*/
				
				x.parent.parent.left=null;
				
				x.parent.right.color="red";
				//Console.WriteLine("root {0},left {1}, right {2}, last row {3},{4},{5},{6}",root.key,root.left.key,root.right.key,root.left.left,root.left.right,root.right.left,root.right.right);
				
			}
			else {
				x.parent.left=x.parent.parent;
				x.parent.left.parent=x.parent;
				if(WhichChild(x.parent) == "left") x.parent.left.left=null;
				else x.parent.left.right=null;
				x.parent.left.color="red";
			}
			
			x.parent.color="black";
			}
		}  // end of Rotation Type 1

		//Rotation Type 2: depending on uncle's color
		else {
			switch(uncle.color) {
			case "red":
				if(WhichChild(uncle)=="left") x.parent.parent.left.color="black";   //u[x] = black
				else x.parent.parent.right.color="black";
				x.parent.color="black";		//p[x] = black
				x.parent.parent.color="red";    //p(p[x]) = red
				break;
			case "black":
				if(WhichChild(x)=="right") {
					x.parent.color="black";
					x.parent.left.color="red";
				}
				else {
				}
				break;
			default: break;
			}
		}
	
	}
Ejemplo n.º 7
0
	public void AdjustTree(Node x) {
		
		//Rule 10: Readjustment of the tree is by rotation
		RotateTree(x);

		//Rule 3: Root is black
		root.color="black";	

		//Rule 4: Leaf is red...automatically as we have all nodes as red to begin with
	
		//Rule 5. Red node which is not a leaf has black children(consecutive red nodes not allowed)
		//SetNodeColor(root);
		
	}
Ejemplo n.º 8
0
	public bool UniqueKey(Node r,int k) {
		if(root==null) return true;
		
		//Console.WriteLine("r.key={0} , k={1}",r.key, k);
		
		if(k==r.key) return false;
		
		else if(k<r.key){
			if(r.left!=null) return(UniqueKey(r.left,k));
			else { return true;}
		}
		else {
		 	if(r.right!=null) return(UniqueKey(r.right,k));
			else { return true;}
		}
	}
Ejemplo n.º 9
0
	public Node BuildNode() {
		Node temp;
		try {
			temp = new Node();
		}catch(Exception e) {
			Console.WriteLine("Caught: {0}",e);
			return null;
		}
		int k = rand.Next(0,nodes);
		bool result = UniqueKey(root,k);

		while(result==false) {
			k = rand.Next(0,nodes);
			result=UniqueKey(root,k);
		}
		
		temp.key = k;
		temp.color = "red";   //Rule 4: Leaf is red
			
		try {
	 		temp.array = new int[128]; // Just allocating array of small size nodes(due to bug#84431)
		//temp.array = new int[k]; // Just allocating array of size of the key
		}catch(Exception e) {
			Console.WriteLine("Caught: {0}",e);
			temp.array = new int[5];
		}

		//Console.WriteLine("inserted: {0}",temp.key);
		return temp;
	}