数据结构-红黑树

手撕红黑树——C++高阶数据结构详解 带你一命通关红黑树 十五分钟手撕数据结构原理 阅读详情

发布一个k8s部署视频:https://edu.youkuaiyun.com/course/detail/26967

课程内容:各种k8s部署方式。包括minikube部署,kubeadm部署,kubeasz部署,rancher部署,k3s部署。包括开发测试环境部署k8s,和生产环境部署k8s。

腾讯课堂连接地址https://ke.qq.com/course/478827?taid=4373109931462251&tuin=ba64518

第二个视频发布  https://edu.youkuaiyun.com/course/detail/27109

腾讯课堂连接地址https://ke.qq.com/course/484107?tuin=ba64518

介绍主要的k8s资源的使用配置和命令。包括configmap,pod,service,replicaset,namespace,deployment,daemonset,ingress,pv,pvc,sc,role,rolebinding,clusterrole,clusterrolebinding,secret,serviceaccount,statefulset,job,cronjob,podDisruptionbudget,podSecurityPolicy,networkPolicy,resourceQuota,limitrange,endpoint,event,conponentstatus,node,apiservice,controllerRevision等。

第三个视频发布:https://edu.youkuaiyun.com/course/detail/27574

详细介绍helm命令,学习helm chart语法,编写helm chart。深入分析各项目源码,学习编写helm插件
————————————————------------------------------------------------------------------------------------------------------------------

 

package com.data.struct;





public class RedBlackTree {
	private Node root;
	//private Node nil;
	
	public RedBlackTree(int []data){
		for(int i=0;i<data.length;i++){
			Node node=new Node();
			node.setValue(data[i]);
			insert(node);
		}
	}
	/**
	 * 中序遍历
	 */
	public void inorderTreeWalk(){
		innerInorderTreeWalk(root);
		System.out.println();
	}
	/**
	 * 中序遍历
	 */
	private void innerInorderTreeWalk(Node node){
		if(node!=null){
			innerInorderTreeWalk(node.left);
			System.out.print("-->"+node.value);
			innerInorderTreeWalk(node.right);
		}
	}
	/**
	 * 先序遍历
	 */
	public void preorderWalk(){
		System.out.println("      "+((root.color==Node.RED?"R":"B")+root.value));
		innerPreorderWalk(root,1);
		System.out.println();
	}
	private void innerPreorderWalk(Node node,int depth){
		if(node!=null){
			if(node.left!=null){
				for(int i=6-depth*2;i>0;i--){
					System.out.print(" ");
				}
				System.out.print((node.left.color==Node.RED?"R":"B")+node.left.value+node.value);
			}else{
				System.out.print("  ");
			}
			if(node.right!=null){
				System.out.print("  ");
				System.out.print((node.right.color==Node.RED?"R":"B")+node.right.value+node.value);
			}else{
				System.out.print("  ");
			}
			if(node.left!=null||node.right!=null){
				System.out.println();
			}
			innerPreorderWalk(node.left,depth+1);
			innerPreorderWalk(node.right,depth+1);
			
		}
		
	}
	/**
	 * 后续遍历
	 */
	public void postorderWalk(){
		innerPostorderWalk(root);
		System.out.println();
	}
	private void innerPostorderWalk(Node node){
		innerPostorderWalk(node.left);
		innerPostorderWalk(node.right);
		System.out.print("-->"+node.value);
	}
	/**
	 * 查找值为value的节点
	 * @param value
	 * @return
	 */
	public Node search(int value){
		return innerSearch(root,value);
	}
	private Node innerSearch(Node node,int value){
		if(node==null||node.value==value){
			return node;
		}
		if(node.value>value){
			return innerSearch(node.left,value);
		}else{
			return innerSearch(node.right,value);
		}
	}
	/**
	 * 返回最小值节点
	 * @return
	 */
	public Node minimum(){
		return innerMinimum(root);
	}
	private Node innerMinimum(Node node){
		if(node.left!=null){
			return innerMinimum(node.left);
		}
		return node;
	}
	/**
	 * 返回最大值节点
	 * @return
	 */
	public Node maximum(){
		return innerMaximum(root);
	}
	private Node innerMaximum(Node node){
		if(node.right!=null){
			return innerMaximum(node.right);
		}
		return node;
	}
	/**
	 * 返回给定节点的后继结点
	 * @param node
	 * @return
	 */
	public Node successor(Node node){
		if(node.right!=null){
			return innerMinimum(node.right);
		}
		Node y=node.parent;
		while(y!=null&&y.right==node){
			node=y;
			y=y.parent;
		}
		return y;
	}
	/**
	 * 插入
	 * @param node
	 */
	private void insert(Node node){
		Node y=null;
		Node x=root;
		while(x!=null){
			y=x;
			if(node.value<x.value){
				x=x.left;
			}else{
				x=x.right;
			}
		}
		node.parent=y;
		if(y==null){
			root=node;
		}else if(y.value>node.value){
			y.left=node;
		}else{
			y.right=node;
		}
		root.parent=null;
		node.color=Node.RED;
		preorderWalk();
		insertFixUp(node);
		preorderWalk();

	}
	/**
	 * 插入修复颜色
	 * @param node
	 */
	private void insertFixUp(Node node){
		if(node.parent==null){
			node.color=Node.BLACK;
		}else if(node.parent.parent==null){
			node.color=Node.RED;
		}else {
			while(node.parent==null||node.parent.color==Node.RED){
				if(node.parent==null){
					node.color=Node.BLACK;
					return;
				}else if(node.parent==node.parent.parent.left){
					Node y=node.parent.parent.right;
					if(y==null||y.color==Node.RED){
						node.parent.color=Node.BLACK;
						if(y!=null){
							y.color=Node.BLACK;
						}else{
							node.parent.parent.color=Node.RED;
							rightRotate(node.parent.parent);
							return;
						}
						node.parent.parent.color=Node.RED;
						
						node=node.parent.parent;
					}else if(node==node.parent.right){
						node=node.parent;
						leftRotate(node);
					}else{
						node.parent.color=Node.BLACK;
						node.parent.parent.color=Node.RED;
						rightRotate(node.parent.parent);
					}
				}else{
					Node y=node.parent.parent.left;
					if(y==null||y.color==Node.RED){
						node.parent.color=Node.BLACK;
						if(y!=null){
							y.color=Node.BLACK;
						}else{
							node.parent.parent.color=Node.RED;
							leftRotate(node.parent.parent);
							return;
						}
						node.parent.parent.color=Node.RED;
						
						node=node.parent.parent;
					}else if(node==node.parent.left){
						node=node.parent;
						rightRotate(node);
					}else{
						node.parent.color=Node.BLACK;
						node.parent.parent.color=node.RED;
						leftRotate(node.parent.parent);
					}
				}
			}
		}
	}
	public void insert(int value){
		Node node=new Node();
		node.setValue(value);
		insert(node);
	}
	private void delete(Node node){
		
	}
	/**
	 * 左旋转
	 * @param node
	 */
	private void leftRotate(Node node){
		Node y=node.right;
		node.right=y.left;
		if(y.left!=null){
			y.left.parent=node;
		}
		y.parent=node.parent;
		if(node.parent==null){
			root=y;
		}else if(node==node.parent.left){
			node.parent.left=y;
		}else{
			node.parent.right=y;
		}
		y.left=node;
		node.parent=y;
	}
	/**
	 * 右旋转
	 * @param node
	 */
	private void rightRotate(Node node){
		Node y=node.left;
		node.left=y.right;
		if(y.right!=null){
			y.right.parent=node;
		}
		y.parent=node.parent;
		if(node.parent==null){
			root=y;
		}else if(node==node.parent.left){
			node.parent.left=y;
		}else{
			node.parent.right=y;
		}
		y.right=node;
		node.parent=y;
	}
	private void transplant(Node u,Node v){
		if(u.parent==null){
			root=v;
		}else if(u==u.parent.left){
			u.parent.left=v;
		}else{
			u.parent.right=v;
		}
		if(v!=null){
			v.parent=u.parent;
		}
	}
	/**
	 * 删除节点
	 * @param value
	 */
	public void delete(int value){
		delete(search(value));
	}
	private static class Node{
		public static final int RED=1;
		public static final int BLACK=2;
		private Node left;
		private Node right;
		private Node parent;
		private int value;
		private int color;
		public Node getLeft() {
			return left;
		}
		public void setLeft(Node left) {
			this.left = left;
		}
		public Node getRight() {
			return right;
		}
		public void setRight(Node right) {
			this.right = right;
		}
		public Node getParent() {
			return parent;
		}
		public void setParent(Node parent) {
			this.parent = parent;
		}
		public int getValue() {
			return value;
		}
		public void setValue(int value) {
			this.value = value;
		}
		public int getColor() {
			return color;
		}
		public void setColor(int color) {
			this.color = color;
		}
		
		
		
	}
	public static void main(String[] args) {
		int [] data=new int[]{8,3,2,6,3,9,1,10,11};
		RedBlackTree tree=new RedBlackTree(data);
		tree.inorderTreeWalk();
		//tree.insert(5);
		tree.inorderTreeWalk();
		//tree.delete(3);
		//tree.inorderTreeWalk();
		tree.preorderWalk();

	}

}

删除操作还没实现,后续补上。

 

package com.data.struct;





public class RedBlackTree {
	private Node root;
	//private Node nil;
	
	public RedBlackTree(int []data){
		for(int i=0;i<data.length;i++){
			Node node=new Node();
			node.setValue(data[i]);
			insert(node);
		}
	}
	/**
	 * 中序遍历
	 */
	public void inorderTreeWalk(){
		innerInorderTreeWalk(root);
		System.out.println();
	}
	/**
	 * 中序遍历
	 */
	private void innerInorderTreeWalk(Node node){
		if(node!=null){
			innerInorderTreeWalk(node.left);
			System.out.print("-->"+node.value);
			innerInorderTreeWalk(node.right);
		}
	}
	/**
	 * 先序遍历
	 */
	public void preorderWalk(){
		System.out.println("      "+((root.color==Node.RED?"R":"B")+root.value));
		innerPreorderWalk(root,1);
		System.out.println();
	}
	private void innerPreorderWalk(Node node,int depth){
		if(node!=null){
			if(node.left!=null){
				for(int i=6-depth*2;i>0;i--){
					System.out.print(" ");
				}
				System.out.print((node.left.color==Node.RED?"R":"B")+node.left.value+node.value);
			}else{
				System.out.print("  ");
			}
			if(node.right!=null){
				System.out.print("  ");
				System.out.print((node.right.color==Node.RED?"R":"B")+node.right.value+node.value);
			}else{
				System.out.print("  ");
			}
			if(node.left!=null||node.right!=null){
				System.out.println();
			}
			innerPreorderWalk(node.left,depth+1);
			innerPreorderWalk(node.right,depth+1);
			
		}
		
	}
	/**
	 * 后续遍历
	 */
	public void postorderWalk(){
		innerPostorderWalk(root);
		System.out.println();
	}
	private void innerPostorderWalk(Node node){
		innerPostorderWalk(node.left);
		innerPostorderWalk(node.right);
		System.out.print("-->"+node.value);
	}
	/**
	 * 查找值为value的节点
	 * @param value
	 * @return
	 */
	public Node search(int value){
		return innerSearch(root,value);
	}
	private Node innerSearch(Node node,int value){
		if(node==null||node.value==value){
			return node;
		}
		if(node.value>value){
			return innerSearch(node.left,value);
		}else{
			return innerSearch(node.right,value);
		}
	}
	/**
	 * 返回最小值节点
	 * @return
	 */
	public Node minimum(){
		return innerMinimum(root);
	}
	private Node innerMinimum(Node node){
		if(node.left!=null){
			return innerMinimum(node.left);
		}
		return node;
	}
	/**
	 * 返回最大值节点
	 * @return
	 */
	public Node maximum(){
		return innerMaximum(root);
	}
	private Node innerMaximum(Node node){
		if(node.right!=null){
			return innerMaximum(node.right);
		}
		return node;
	}
	/**
	 * 返回给定节点的后继结点
	 * @param node
	 * @return
	 */
	public Node successor(Node node){
		if(node.right!=null){
			return innerMinimum(node.right);
		}
		Node y=node.parent;
		while(y!=null&&y.right==node){
			node=y;
			y=y.parent;
		}
		return y;
	}
	/**
	 * 插入
	 * @param node
	 */
	private void insert(Node node){
		Node y=null;
		Node x=root;
		while(x!=null){
			y=x;
			if(node.value<x.value){
				x=x.left;
			}else{
				x=x.right;
			}
		}
		node.parent=y;
		if(y==null){
			root=node;
		}else if(y.value>node.value){
			y.left=node;
		}else{
			y.right=node;
		}
		root.parent=null;
		node.color=Node.RED;
		preorderWalk();
		insertFixUp(node);
		preorderWalk();

	}
	/**
	 * 插入修复颜色
	 * @param node
	 */
	private void insertFixUp(Node node){
		if(node.parent==null){
			node.color=Node.BLACK;
		}else if(node.parent.parent==null){
			node.color=Node.RED;
		}else {
			while(node.parent==null||node.parent.color==Node.RED){
				if(node.parent==null){
					node.color=Node.BLACK;
					return;
				}else if(node.parent==node.parent.parent.left){
					Node y=node.parent.parent.right;
					if(y==null||y.color==Node.RED){
						node.parent.color=Node.BLACK;
						if(y!=null){
							y.color=Node.BLACK;
						}else{
							node.parent.parent.color=Node.RED;
							rightRotate(node.parent.parent);
							return;
						}
						node.parent.parent.color=Node.RED;
						
						node=node.parent.parent;
					}else if(node==node.parent.right){
						node=node.parent;
						leftRotate(node);
					}else{
						node.parent.color=Node.BLACK;
						node.parent.parent.color=Node.RED;
						rightRotate(node.parent.parent);
					}
				}else{
					Node y=node.parent.parent.left;
					if(y==null||y.color==Node.RED){
						node.parent.color=Node.BLACK;
						if(y!=null){
							y.color=Node.BLACK;
						}else{
							node.parent.parent.color=Node.RED;
							leftRotate(node.parent.parent);
							return;
						}
						node.parent.parent.color=Node.RED;
						
						node=node.parent.parent;
					}else if(node==node.parent.left){
						node=node.parent;
						rightRotate(node);
					}else{
						node.parent.color=Node.BLACK;
						node.parent.parent.color=node.RED;
						leftRotate(node.parent.parent);
					}
				}
			}
		}
	}
	public void insert(int value){
		Node node=new Node();
		node.setValue(value);
		insert(node);
	}
	private void delete(Node node){
		Node y=node;
		int yoc=y.color;
		Node x;
		if(node.left==null&&node.right!=null){
			 x=node.right;
			transplant(node, node.right);
		}else if(node.right==null&&node.left!=null){
			 x=node.left;
			transplant(node, node.left);
		}else if(node.left==null&&node.right==null){
			x=node.parent;
			if(node==node.parent.left){
				node.parent.left=null;
				if(x.color==Node.RED){
					leftRotate(x);
					preorderWalk();
					return;
				}else if(x.color==Node.BLACK&&node.color==Node.BLACK){
					x=node.parent.right;
					deleteFixUp(x);
					return;
				}
			}else{
				node.parent.right=null;
				if(x.color==Node.RED){
					rightRotate(x);
					preorderWalk();
					return;
				}else if(x.color==Node.BLACK&&node.color==Node.BLACK){
					x=node.parent.left;
					deleteFixUp(x);
					return;
				}
			}
		}else{
			y=innerMinimum(node.right);
			yoc=y.color;
			 
			if(y.parent!=node){
				transplant(y, y.right);
				y.right=node.right;
				y.right.parent=y;
			}
			transplant(node, y);
			x=y.right;
			y.left=node.left;
			y.left.parent=y;
			y.color=node.color;
			if(x==null){
				preorderWalk();
				y.color=Node.RED;
				if(y.left!=null){
					y.left.color=Node.BLACK;
					rightRotate(y);
				}else if(y.right!=null){
					y.right.color=Node.RED;
					leftRotate(y);
				}else{
					y.color=Node.RED;
				}
				preorderWalk();
				return;
				
			}
		}
		preorderWalk();
		if(yoc==Node.BLACK){
			deleteFixUp(x);
			preorderWalk();
		}
		
	}
	private void deleteFixUp(Node node){
		if(node==null){
			
		}
		while(node!=root&&node.color==Node.BLACK){
			Node w;
			if(node==node.parent.left){
				w=node.parent.right;
				if(w.color==Node.RED){
					w.color=Node.BLACK;
					node.parent.color=Node.RED;
					leftRotate(node.parent);
					w=node.parent.right;
				}
				if(w.left.color==Node.BLACK&&w.right.color==Node.BLACK){
					w.color=Node.RED;
					node=node.parent;
				}else if(w.right.color==Node.BLACK){
					w.left.color=Node.BLACK;
					w.color=Node.RED;
					rightRotate(w);
					w=node.parent.right;
				}
				w.color=node.parent.color;
				node.parent.color=Node.BLACK;
				w.right.color=Node.BLACK;
				leftRotate(node.parent);
				node=root;
				
			}else{
				w=node.parent.left;
				if(w.color==Node.RED){
					w.color=Node.BLACK;
					node.parent.color=Node.RED;
					rightRotate(node.parent);
					w=node.parent.left;
				}
				if(w.right.color==Node.BLACK&&w.left.color==Node.BLACK){
					w.color=Node.RED;
					node=node.parent;
				}else if(w.left.color==Node.BLACK){
					w.left.color=Node.BLACK;
					w.color=Node.RED;
					leftRotate(w);
					w=node.parent.left;
				}
				w.color=node.parent.color;
				node.parent.color=Node.BLACK;
				w.left.color=Node.BLACK;
				rightRotate(node.parent);
				node=root;
			}
		}
		node.color=Node.BLACK;
		
	}
	/**
	 * 左旋转
	 * @param node
	 */
	private void leftRotate(Node node){
		Node y=node.right;
		node.right=y.left;
		if(y.left!=null){
			y.left.parent=node;
		}
		y.parent=node.parent;
		if(node.parent==null){
			root=y;
		}else if(node==node.parent.left){
			node.parent.left=y;
		}else{
			node.parent.right=y;
		}
		y.left=node;
		node.parent=y;
	}
	/**
	 * 右旋转
	 * @param node
	 */
	private void rightRotate(Node node){
		Node y=node.left;
		node.left=y.right;
		if(y.right!=null){
			y.right.parent=node;
		}
		y.parent=node.parent;
		if(node.parent==null){
			root=y;
		}else if(node==node.parent.left){
			node.parent.left=y;
		}else{
			node.parent.right=y;
		}
		y.right=node;
		node.parent=y;
	}
	private void transplant(Node u,Node v){
		if(u.parent==null){
			root=v;
		}else if(u==u.parent.left){
			u.parent.left=v;
		}else{
			u.parent.right=v;
		}
		if(v!=null){
			v.parent=u.parent;
		}
	}
	/**
	 * 删除节点
	 * @param value
	 */
	public void delete(int value){
		delete(search(value));
	}
	private static class Node{
		public static final int RED=1;
		public static final int BLACK=2;
		private Node left;
		private Node right;
		private Node parent;
		private int value;
		private int color;
		public Node getLeft() {
			return left;
		}
		public void setLeft(Node left) {
			this.left = left;
		}
		public Node getRight() {
			return right;
		}
		public void setRight(Node right) {
			this.right = right;
		}
		public Node getParent() {
			return parent;
		}
		public void setParent(Node parent) {
			this.parent = parent;
		}
		public int getValue() {
			return value;
		}
		public void setValue(int value) {
			this.value = value;
		}
		public int getColor() {
			return color;
		}
		public void setColor(int color) {
			this.color = color;
		}
		
		
		
	}
	public static void main(String[] args) {
		int [] data=new int[]{8,3,2,6,3,9,1,10,11};
		RedBlackTree tree=new RedBlackTree(data);
		tree.inorderTreeWalk();
		//tree.insert(5);
		tree.inorderTreeWalk();
		tree.preorderWalk();
		System.out.println("===========");
		tree.delete(10);
		tree.delete(2);
		tree.delete(1);
		tree.delete(8);
		tree.delete(6);
		tree.delete(9);

	}

}
没有完全测,节点少时可能报异常

 
package com.data.struct; public class RedBlackTree { private Node root; //private Node nil; public RedBlackTree(int []data){ for(int i=0;i<data.length;i++){ Node node=new Node(); node.setValue(data[i]); insert(node); } } /** * 中序遍历 */ public void inorderTreeWalk(){ innerInorderTreeWalk(root); System.out.println(); } /** * 中序遍历 */ private void innerInorderTreeWalk(Node node){ if(node!=null){ innerInorderTreeWalk(node.left); System.out.print("-->"+node.value); innerInorderTreeWalk(node.right); } } /** * 先序遍历 */ public void preorderWalk(){ System.out.println(" "+((root.color==Node.RED?"R":"B")+root.value)); innerPreorderWalk(root,1); System.out.println(); } private void innerPreorderWalk(Node node,int depth){ if(node!=null){ if(node.left!=null){ for(int i=6-depth*2;i>0;i--){ System.out.print(" "); } System.out.print((node.left.color==Node.RED?"R":"B")+node.left.value+node.value); }else{ System.out.print(" "); } if(node.right!=null){ System.out.print(" "); System.out.print((node.right.color==Node.RED?"R":"B")+node.right.value+node.value); }else{ System.out.print(" "); } if(node.left!=null||node.right!=null){ System.out.println(); } innerPreorderWalk(node.left,depth+1); innerPreorderWalk(node.right,depth+1); } } /** * 后续遍历 */ public void postorderWalk(){ innerPostorderWalk(root); System.out.println(); } private void innerPostorderWalk(Node node){ innerPostorderWalk(node.left); innerPostorderWalk(node.right); System.out.print("-->"+node.value); } /** * 查找值为value的节点 * @param value * @return */ public Node search(int value){ return innerSearch(root,value); } private Node innerSearch(Node node,int value){ if(node==null||node.value==value){ return node; } if(node.value>value){ return innerSearch(node.left,value); }else{ return innerSearch(node.right,value); } } /** * 返回最小值节点 * @return */ public Node minimum(){ return innerMinimum(root); } private Node innerMinimum(Node node){ if(node.left!=null){ return innerMinimum(node.left); } return node; } /** * 返回最大值节点 * @return */ public Node maximum(){ return innerMaximum(root); } private Node innerMaximum(Node node){ if(node.right!=null){ return innerMaximum(node.right); } return node; } /** * 返回给定节点的后继结点 * @param node * @return */ public Node successor(Node node){ if(node.right!=null){ return innerMinimum(node.right); } Node y=node.parent; while(y!=null&&y.right==node){ node=y; y=y.parent; } return y; } /** * 插入 * @param node */ private void insert(Node node){ Node y=null; Node x=root; while(x!=null){ y=x; if(node.value<x.value){ x=x.left; }else{ x=x.right; } } node.parent=y; if(y==null){ root=node; }else if(y.value>node.value){ y.left=node; }else{ y.right=node; } root.parent=null; node.color=Node.RED; preorderWalk(); insertFixUp(node); preorderWalk(); } /** * 插入修复颜色 * @param node */ private void insertFixUp(Node node){ if(node.parent==null){ node.color=Node.BLACK; }else if(node.parent.parent==null){ node.color=Node.RED; }else { while(node.parent==null||node.parent.color==Node.RED){ if(node.parent==null){ node.color=Node.BLACK; return; }else if(node.parent==node.parent.parent.left){ Node y=node.parent.parent.right; if(y==null||y.color==Node.RED){ node.parent.color=Node.BLACK; if(y!=null){ y.color=Node.BLACK; }else{ node.parent.parent.color=Node.RED; rightRotate(node.parent.parent); return; } node.parent.parent.color=Node.RED; node=node.parent.parent; }else if(node==node.parent.right){ node=node.parent; leftRotate(node); }else{ node.parent.color=Node.BLACK; node.parent.parent.color=Node.RED; rightRotate(node.parent.parent); } }else{ Node y=node.parent.parent.left; if(y==null||y.color==Node.RED){ node.parent.color=Node.BLACK; if(y!=null){ y.color=Node.BLACK; }else{ node.parent.parent.color=Node.RED; leftRotate(node.parent.parent); return; } node.parent.parent.color=Node.RED; node=node.parent.parent; }else if(node==node.parent.left){ node=node.parent; rightRotate(node); }else{ node.parent.color=Node.BLACK; node.parent.parent.color=node.RED; leftRotate(node.parent.parent); } } } } } public void insert(int value){ Node node=new Node(); node.setValue(value); insert(node); } private void delete(Node node){ Node y=node; int yoc=y.color; Node x; if(node.left==null&&node.right!=null){ x=node.right; transplant(node, node.right); }else if(node.right==null&&node.left!=null){ x=node.left; transplant(node, node.left); }else if(node.left==null&&node.right==null){ if(node.parent==null){ root=null; System.out.println("root is null"); return; } x=node.parent; if(node==node.parent.left){ node.parent.left=null; if(x.color==Node.RED){ leftRotate(x); preorderWalk(); return; }else if(x.color==Node.BLACK&&node.color==Node.BLACK){ x=node.parent.right; deleteFixUp(x); return; } }else{ node.parent.right=null; if(x.color==Node.RED){ rightRotate(x); preorderWalk(); return; }else if(x.color==Node.BLACK&&node.color==Node.BLACK){ x=node.parent.left; deleteFixUp(x); return; } } }else{ y=innerMinimum(node.right); yoc=y.color; if(y.parent!=node){ transplant(y, y.right); y.right=node.right; y.right.parent=y; } transplant(node, y); x=y.right; y.left=node.left; y.left.parent=y; y.color=node.color; if(x==null){ preorderWalk(); y.color=Node.RED; if(y.left!=null){ y.left.color=Node.BLACK; rightRotate(y); }else if(y.right!=null){ y.right.color=Node.RED; leftRotate(y); }else{ y.color=Node.RED; } preorderWalk(); return; } } preorderWalk(); if(yoc==Node.BLACK){ deleteFixUp(x); preorderWalk(); } } private void deleteFixUp(Node node){ if(node==null){ } while(node!=root&&node.color==Node.BLACK){ Node w; if(node==node.parent.left){ w=node.parent.right; if(w.color==Node.RED){ w.color=Node.BLACK; node.parent.color=Node.RED; leftRotate(node.parent); w=node.parent.right; } if(w.left.color==Node.BLACK&&w.right.color==Node.BLACK){ w.color=Node.RED; node=node.parent; }else if(w.right.color==Node.BLACK){ w.left.color=Node.BLACK; w.color=Node.RED; rightRotate(w); w=node.parent.right; } w.color=node.parent.color; node.parent.color=Node.BLACK; w.right.color=Node.BLACK; leftRotate(node.parent); node=root; }else{ w=node.parent.left; if(w.color==Node.RED){ w.color=Node.BLACK; node.parent.color=Node.RED; rightRotate(node.parent); w=node.parent.left; } if(w.right.color==Node.BLACK&&w.left.color==Node.BLACK){ w.color=Node.RED; node=node.parent; }else if(w.left.color==Node.BLACK){ w.left.color=Node.BLACK; w.color=Node.RED; leftRotate(w); w=node.parent.left; } w.color=node.parent.color; node.parent.color=Node.BLACK; w.left.color=Node.BLACK; rightRotate(node.parent); node=root; } } node.color=Node.BLACK; } /** * 左旋转 * @param node */ private void leftRotate(Node node){ Node y=node.right; node.right=y.left; if(y.left!=null){ y.left.parent=node; } y.parent=node.parent; if(node.parent==null){ root=y; }else if(node==node.parent.left){ node.parent.left=y; }else{ node.parent.right=y; } y.left=node; node.parent=y; } /** * 右旋转 * @param node */ private void rightRotate(Node node){ Node y=node.left; node.left=y.right; if(y.right!=null){ y.right.parent=node; } y.parent=node.parent; if(node.parent==null){ root=y; }else if(node==node.parent.left){ node.parent.left=y; }else{ node.parent.right=y; } y.right=node; node.parent=y; } private void transplant(Node u,Node v){ if(u.parent==null){ root=v; }else if(u==u.parent.left){ u.parent.left=v; }else{ u.parent.right=v; } if(v!=null){ v.parent=u.parent; } } /** * 删除节点 * @param value */ public void delete(int value){ delete(search(value)); } private static class Node{ public static final int RED=1; public static final int BLACK=2; private Node left; private Node right; private Node parent; private int value; private int color; public Node getLeft() { return left; } public void setLeft(Node left) { this.left = left; } public Node getRight() { return right; } public void setRight(Node right) { this.right = right; } public Node getParent() { return parent; } public void setParent(Node parent) { this.parent = parent; } public int getValue() { return value; } public void setValue(int value) { this.value = value; } public int getColor() { return color; } public void setColor(int color) { this.color = color; } } public static void main(String[] args) { int [] data=new int[]{8,3,2,6,3,9,1,10,11}; RedBlackTree tree=new RedBlackTree(data); tree.inorderTreeWalk(); //tree.insert(5); tree.inorderTreeWalk(); tree.preorderWalk(); System.out.println("==========="); tree.delete(10); tree.delete(2); tree.delete(1); tree.delete(8); tree.delete(6); tree.delete(9); tree.delete(11); tree.delete(3); tree.delete(3); } } 

 

 


 

 

 

 

【数据结构】红黑树 红黑树的概念、红黑树的性质、红黑树结点的定义、红黑树的增删查改、红黑树的验证、红黑树与AVL树的比较 阅读详情

相关推荐

数据结构 - 红黑树

数据结构 - 红黑树 - 面试常问知识点 数据结构是面试中必定考查的知识点,面试者需要掌握几种经典的数据结构:线性表(数组、链表)、栈与队列、树(二叉树、二叉查找树、平衡二叉树、红黑树)、图。 本文主要介绍树中的常见的红黑树数据结构。包括 简介 应用 左旋和右旋 简介 R-B Tree,全称是Red-Black Tree,又称为“红黑树”,它一种特殊的二叉查找树。红黑树的

从零开始 3411

数据结构—红黑树

优点。

2302_80190174的博客 1333

【数据结构进阶】手撕红黑树

红黑树的介绍,以及底层代码逻辑和实现。刚刚接触编程的时候就听说有的大厂HR会让手撕红黑树。心中一直对这个数据结构保持着敬畏和向往,今天在将其撕出来后,用本篇博客加以记录,望周知。红黑树的结点包含四个成员变量,模板类型T:可以存储K或者pair类型,便于后期封装;三个指针:分别为指向左孩子结点的指针,指向右孩子结点的指针,指向父结点的指针;最后变量_col:枚举类型,可以存RED和BLACK。

2303_79329831的博客 1819

数据结构——红黑树

数据结构——红黑树 前言 红黑树是map,set,mutilmap,mutilset容器的底层数据结构,也是一种非常重要的数据结构,它在效率上对AVL又做了进一步的优化,因为AVL结构在插入,或删除时,无法避免大量的旋转操作,导致效率有所损耗,但是红黑树,就在这一点有了更好的优化,但是牺牲了平衡性,所以是不是一个完全平衡的二叉搜索树。 一、红黑树的概念和性质 概念: 红黑树,是一种二叉搜索树,但在每个结点上增加一个存储位表示结点的颜色,可以是Red或Black。 通过对任何一条从根到叶子的路径上各个结点着色

stromSLR的博客 4982

CAN信号定义避坑指南:Kvaser Database Editor里Byteorder和Bitpos设置的那些‘坑’

本文详细解析了在Kvaser Database Editor中配置CAN信号时,字节序(Byteorder)和起始位(Bitpos)设置的常见错误及解决方案。通过实际案例和验证技巧,帮助工程师避免DBC文件配置中的陷阱,确保车载电子系统的稳定通信。

weixin_28678831的博客 360

Open3D:Win10 + VS2017配置Open3D(C++、python)

一、下载源码 地址:https://github.com/intel-isl/Open3D 1.1 克隆源码【笔者的源码路径为D:\Open3D\Open3D】 1.2 切换版本【笔者的版本为0.8.0】 1.3 更新子模块【用于获取第三方库!很重要!】 二、CMake编译 2.1 设置路径 【source code: D:\Open3D\Open3D;build binaries: D:\Open3D】 【笔者设置CMAKE/CMAKE_INSTALL_PREFIX的路径为D:/Open.

累了就要打游戏 1万+

HashMap 数据结构之红黑树, 红黑树在什么时候左旋 右旋 如何旋转

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 树是数据结构中经典的结构之一,也是面试中常问的面试题之一,最近复习了一个红黑树的知识,做一下整理 文章目录 前言 一、pandas是什么? 二、使用步骤 1.引入库 2.读入数据 总结 前言 提示:这里可以添加本文要记录的大概内容: 例如:随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了机器学习的基础内容。 提示:以下是本篇文章正文内容,下面案例可供参考 ...

huqiankunlol的博客 4223

脑电分析中的频谱特征提取 —— 从理论到实践

本文系统介绍了脑电信号(EEG)频谱特征提取方法及其应用。EEG信号通过频谱分析可分为Delta、Theta、Alpha、Beta和Gamma等具有明确生理意义的频段。文章详细讲解了功率谱密度、频带功率、相对功率、频谱熵等关键特征的计算方法,并提供了Python实现示例,包括模拟数据和真实CHB-MIT癫痫数据集的处理流程。这些频谱特征在脑机接口、情绪识别、疲劳监测和癫痫检测等领域具有重要应用价值。文章强调频谱特征结合时域和非线性特征可提升分析效果,为EEG研究提供了实用指导。

qq_32516809的博客 1441

深度剖析数据结构—红黑树

学过数据数据结构都知道二叉树的概念,而又有多种比较常见的二叉树类型,比如完全二叉树、满二叉树、二叉搜索树、均衡二叉树、完美二叉树等; 今天我们要说的红黑树就是就是一颗非严格均衡的二叉树,均衡二叉树又是在二叉搜索树的基础上增加了自动维持平衡的性质,插入、搜索、删除的效率都比较高。红黑树也是实现TreeMap存储结构的基石。

4万+

使用DDR4控制器实现多通道数据读写

这种高带宽对于大量的数据的应用非常重要。对于Controller/PHY Mode 的选择,我们使用Controller + PHY模式,Xilinx UltraScale/UltraScale+等高端FPGA通常包含硬核PHY,选择Controller + PHY模式可直接利用硬核,降低功耗和延迟,并且此模式的开发更易上手。在Basic页面下,首先勾选上AXI4 Interface选项,我们选用的内存接口速度为2133MHz的ddr4芯片,由于双沿触发,频率为1066MHz,那么周期应为938ps。

weixin_38828382的博客 1364

手把手教你用u-center配置ublox GPS模块:从波特率到报文频率全流程

本文提供了一份详细的u-center软件配置ublox GPS模块的完整指南。从硬件连接到软件设置,逐步讲解了如何探测并设置正确的波特率、选择与启用关键的NMEA和UBX报文,以及配置输出频率。文章特别强调了配置固化的重要性,并分享了使用UBX协议进行高效数据解析等高级技巧,帮助开发者快速上手并优化GPS模块性能。

weixin_28285943的博客 901

红黑树(数据结构篇)

讲解数据结构中的红黑树

hellmorning的博客 2728

平均电流控制模式BUCK变换器的环路设计

引言 电压模式的BUCK变换器存在的常见缺点: 1、在没有前馈补偿的时候,对输入源的跳动响应较慢; 2、在不加入电感电流控制的时候,由于不直接对电感电流进行采样,因此不能实时的控制电感电流,有可能在某些负载极端变化时,系统直接奔溃; 3、LC组成的二阶系统存在谐振点,谐振峰随负载变化而变化,不容易做到完全消除; 峰值电流模式的BUCK变换器存在的常见缺点: 1、占空比大于50%时,存在次谐波震荡的问题,需要添加斜坡补偿已解决该问题; 2、抗干扰能力较差; 3、峰值电流和平均电流间存在误差,不适合做PFC线路

c007106333的博客 1万+

C++数据结构——红黑树

简要总结了数据结构红黑树的基本原理和关键操作

m0_46329175的博客 1187

SecureCRT 9.0最新版安装与激活全攻略(附驱动识别技巧)

本文提供SecureCRT 9.0最新版的详细安装与激活指南,并重点解析了嵌入式开发中串口连接的硬件驱动识别技巧。内容涵盖从安全获取软件、解决USB转串口驱动兼容性问题,到配置SSH与串口会话的完整流程,旨在帮助开发者和工程师高效部署并精通使用这款终端仿真利器。

star的博客 415

金融监管压力测试场景生成

压力测试是一种评估金融资产或机构在极端市场条件下的表现和风险承受能力的方法。它通过模拟各种不利的市场情景,测试金融机构在这些情景下的财务状况和运营能力。压力测试的作用在于帮助金融机构识别潜在风险,评估应对策略,并制定应急预案。总结压力测试场景生成系统的开发过程和实现效果,强调其在金融监管中的重要性和应用价值。随着金融市场的发展,实时压力测试将越来越重要。未来,压力测试需要更加注重实时性和动态性,及时反映市场的变化。本文详细介绍了金融监管压力测试的重要性、核心概念、算法原理、系统架构以及实战案例。

AI天才研究院 1210

数据结构之红黑树Java实现

【本文是为了梳理知识的总结性文章,总结了一些自认为相关的重要知识点,只为巩固记忆以及技术交流,忘批评指正。其中参考了很多前辈的文章,包括图片也是引用,如有冒犯,侵删。】 1 红黑树的进化历程 1.1 第一阶段:树 树是一种常用的数据结构,它是一个由有限节点组成的一个具有层次关系的集合,数据就存在树的这些节点中。最顶层只有一个节点,称为根节点。在分支处有一个节点,指向多个方向,如果某节点下方没...

InkGenius 1068

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

数据结构之红黑树简介

一、红黑树定义 红黑树是二叉查找树,二叉查找树这一数据结构并不难,而红黑树之所以难是难在它是自平衡的二叉查找树,在进行插入和删除等可能会破坏树的平衡的操作时,需要重新自处理达到平衡状态。 红黑树是一种含有红色和黑色节点并能自平衡的二叉查找树,红黑树和其他二叉查找树类似,都是在进行插入和删除操作时通过特定操作保持二叉查找树的性质,从而获得较高的查找性能。它虽然是复杂的,但它的最坏情况运行时间也是非常良好的,并且在实践中是高效的:它可以在 O(log n) 时间内做查找,插入和删除,这里的 n 是树中元素的

weixiaohuai的博客 3918

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

数据结构之红黑树

红黑树中节点,一类被标记为黑色,一类被标记为红色。除此之外,一棵红黑树还需要满足这样几个要求:根节点是黑色的每个叶子节点都是黑色的空节点(NIL),也就是说,叶子节点不存储数据。任何相邻的节点都不能同时为红色,也就是,红色节点是被黑色节点隔开的;每个节点,从该节点到达其可达叶子节点的所有路径,都包含相同数目的黑色节点。为什么说红黑树是近似的平衡二叉树。

weixin_44399827的博客 990

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

2.数据结构-红黑树

上篇文章我们研究了二叉树,发现二叉树在不平衡状态下,其时间复杂度不够优秀,有没有一种比较稳定高效的二叉树,他就是红黑树。 红黑树是什么 红黑树(Red Black Tree) 是一种自平衡二叉查找树,是在计算机科学中用到的一种数据结构,红黑树是在1972年由Rudolf Bayer发明的,当时被称为平衡二叉B树(symmetric binary B-trees)。后来,在1978年被 Leo J. Guibas 和 Robert Sedgewick 修改为如今的“红黑树”。红黑树是一种特化的AVL树(平

爱欧米 477

Java数据结构:树之红黑树

1. 概述 在前面的篇章已经写了Java数据结构:树基础及二叉树,对于树已经有了一定的了解。而红黑树在树中是比较重要的一种,运用也比较多,比如HashMap的源码中就用到了红黑树。通过此篇对红黑树的了解,后续会写一篇关于HashMap源码的解析。 2. 红黑树的定义: 红黑树,R-B Tree,全称是Red-Black Tree,又称为“红黑树”。它一种特殊的二叉查找树,它满足二叉查找树的特...

接受自己的普通,然后拼尽全力去与众不同 583

数据结构与算法之红黑树

数据结构与算法系列 数据结构与算法之哈希表 数据结构与算法之跳跃表 数据结构与算法之字典树 数据结构与算法之2-3树 数据结构与算法之平衡二叉树 数据结构与算法之红黑树 数据结构与算法之十大经典排序 数据结构与算法之二分查找三模板 数据结构与算法之动态规划 数据结构与算法之回溯算法 数据结构与算法之Morris算法 数据结构与算法之贪心算法 数据结构与算法之摩尔投票 目录数据结构与算法系列数据结构与算法之哈希表数据结构与算法之跳跃表数据结构与算法之字典树数据结构与算法之2-3树数据结构与算法之平衡二叉树数据

LInthunder的博客 435

数据结构:红黑树

一、定义     红黑树是一种特殊的二叉查找树,它的每一个结点都被标记为红色或者黑色。是在计算机科学中用到的一种数据结构,典型的用途是实现关联数组。它是在1972年由Rudolf Bayer发明的,他称之为"对称二叉B树",它现代的名字是在 Leo J. Guibas 和 Robert Sedgewick 于1978年写的一篇论文中获得的。它是复杂的,但它的操作有着良好的最坏情况运行时间,并且在

过往记忆大数据 3万+
上一篇: 计算机科学中的树
下一篇: 数据结构-红黑树扩张-顺序统计树
hxpjava1
博客等级 码龄19年 1365粉丝 511原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hxpjava1

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值