I need some help with a problem. I am attempting to insert numbers into a binary tree, then converted it to a rbtree, just curious on how i can change it over i know that
10 (black), 4(red), 5(black), 6(red), 11(red), 12(black), 13(red) as my input.

int a[] ={10, 4, 5, 6, 11, 12, 13}

the pseudo code that i got from the web is below for a tree-insert. Thanks in advance for any advice or code you can help with.

TREE-INSERT(T, z)	
y = nil	
x = T.root	
while (x == 0) 				
	y = x	;				

if (z.key < y.key) 				
x = x.left;		
else
 x = x.right;	
z.parent = y;	
if (y == 0)					
T.root = z;		
if (z.key < y.key) 			
y.left = z;		
else			
y.right = z;

A red black tree is more than just a binary search tree with nodes marked red and black. The structure also has to be balanced according to the red black rules. Why not do a red black insert in the first place instead of trying to convert between a simple binary search tree and a balanced red black tree?

A red black tree is more than just a binary search tree with nodes marked red and black. The structure also has to be balanced according to the red black rules. Why not do a red black insert in the first place instead of trying to convert between a simple binary search tree and a balanced red black tree?

I have to start with a premade tree with the following parameters.

10 (black)
4(red)
5(black)
6(red)
11(red)
12(black)
13(red)
Then insert nodes like 9 15 18 into this tree. So i thought create the tree in a binary tree it would be easier to handle. But do you have any suggestions on how to do this.

So i thought create the tree in a binary tree it would be easier to handle.

I disagree. You can manually create the tree easily since it is small and the rules for red black insertion are simple. But if this is a school exercise, I can guarantee that adding the next three nodes will require rotations. So you will be writing a red black insertion function anyway. It is easier to create the tree using that algorithm from the start.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.