Skip to content

Commit 0325548

Browse files
author
cfdcoder
committed
Add Red Black Tree
1 parent 15ca6a1 commit 0325548

File tree

109 files changed

+30328
-1595
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

109 files changed

+30328
-1595
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package study;
2+
3+
public class RBTree<T extends Comparable<T>> {
4+
private RBNode<T> Root;
5+
6+
7+
public RBTree(){
8+
Root=null;
9+
}
10+
11+
private RBNode<T> parent(RBNode<T> node){
12+
if (node!=null){
13+
return node.parent;
14+
}else{
15+
return null;
16+
}
17+
}
18+
19+
private NodeColor getColor(RBNode<T> node){
20+
if(node!=null)
21+
return node.color;
22+
else
23+
return null;
24+
}
25+
26+
private boolean isRed(RBNode<T> node){
27+
if(node!=null && node.color==NodeColor.red){
28+
return true;
29+
}else
30+
return false;
31+
}
32+
33+
private boolean isBlack(RBNode<T> node){
34+
if(node!=null && node.color==NodeColor.black){
35+
return true;
36+
}else
37+
return false;
38+
}
39+
40+
private void setRed(RBNode<T>node){
41+
if(node!=null)
42+
node.color=NodeColor.red;
43+
}
44+
45+
private void setBlack(RBNode<T>node){
46+
if(node!=null)
47+
node.color=NodeColor.black;
48+
}
49+
50+
private void setParent(RBNode<T>node, RBNode<T>parent){
51+
if(node!=null)
52+
node.parent=parent;
53+
}
54+
55+
56+
57+
public static void main(String[] args) {
58+
// TODO Auto-generated method stub
59+
60+
}
61+
62+
}
63+
64+
class RBNode<T extends Comparable <T>>{
65+
public NodeColor color;
66+
T key;
67+
RBNode<T> left;
68+
RBNode<T> right;
69+
RBNode<T> parent;
70+
71+
public RBNode(T key, NodeColor color, RBNode<T>parent, RBNode<T>left, RBNode<T>right){
72+
this.key=key;
73+
this.color=color;
74+
this.parent=parent;
75+
this.left=left;
76+
this.right=right;
77+
}
78+
79+
public T getKey(){
80+
return key;
81+
}
82+
83+
public String toString(){
84+
return " "+key+this.color+" ";
85+
}
86+
87+
}
88+
89+
90+
enum NodeColor{
91+
red, black;
92+
}

0 commit comments

Comments
 (0)