zoukankan      html  css  js  c++  java
  • 二叉搜索树API

    struct BinarySearchTree {
    	Node* search(Node* node, int key) {
    		while (nil != node && key != node->key) {
    			if (key < node->key) {
    				node = node->leftChild;
    			} else {
    				node = node->rightChild;
    			}
    		}
    		return node;
    	}
    	Node* minimum(Node* node) {
    		while (nil != node->leftChild) {
    			node = node->leftChild;
    		}
    		return node;
    	}
    	Node* maximum(Node* node) {
    		while (nil != node->rightChild) {
    			node = node->rightChild;
    		}
    		return node;
    	}
    	Node* predecessor(Node* node) {
    		if (nil != node->leftChild) {
    			return maximum(node->leftChild);
    		}
    		while (nil != node->parent && node == node->parent->leftChild) {
    			node = node->parent;
    		}
    		return node->parent;
    	}
    	Node* successor(Node* node) {
    		if (nil != node->rightChild) {
    			return minimum(node->rightChild);
    		}
    		while (nil != node->parent && node == node->parent->rightChild) {
    			node = node->parent;
    		}
    		return node->parent;
    	}
    	void insert(Node* node) {
    		Node* father = nil;
    		Node* current = root;
    		while (nil != current) {
    			father = current;
    			if (node->key < current->key) {
    				current = current->leftChild;
    			} else {
    				current = current->rightChild;
    			}
    		}
    		node->parent = father;
    		if (nil == father) {
    			root = node;
    		} else if (node->key < father->key) {
    			father->leftChild = node;
    		} else {
    			father->rightChild = node;
    		}
    	}
    	void transplant(Node* des, Node* src) {
    		if (nil == des->parent) {
    			root = src;
    		} else if (des == des->parent->leftChild) {
    			des->parent->leftChild = src;
    		} else {
    			des->parent->rightChild = src;
    		}
    		if (nil != src) {
    			src->parent = des->parent;
    		}
    	}
    	void del(Node* node) {
    		if (nil == node->leftChild) {
    			transplant(node, node->rightChild);
    		} else if (nil == node->rightChild) {
    			transplant(node, node->leftChild);
    		} else {
    			Node* suc = minimum(node->rightChild);
    			if (suc->parent != node) {
    				transplant(suc, suc->rightChild);
    				suc->rightChild = node->rightChild;
    				suc->rightChild->parent = suc;
    			}
    			transplant(node, suc);
    			suc->leftChild = node->leftChild;
    			suc->leftChild->parent = suc;
    		}
    	}
    };
    

      

  • 相关阅读:
    Javascript函数式编程要掌握的知识点讲解
    任务
    怎么添加项目到SVN上面
    随便写一点最近开发遇到的问题和解决方法 大部分关于laravel和php
    laravel 将数组转化成字符串 再把字符串转化成数组
    laravel 配置了自己的域名以后, localhost 无法访问 404 not found 的解决方法
    实习日记15
    php接收post过来的 json数据 例子
    数据库如何让自增id重置
    C#连接MySQL数据库
  • 原文地址:https://www.cnblogs.com/lvcoding/p/7543530.html
Copyright © 2011-2022 走看看