[geeks]Floor and Ceil from a BST
来源:互联网 发布:暖脚宝哪个牌子好 知乎 编辑:程序博客网 时间:2024/06/04 18:24
There are numerous applications we need to find floor (ceil) value of a key in a binary search tree or sorted array. For example, consider designing memory management system in which free nodes are arranged in BST. Find best fit for the input request.
Ceil Value Node: Node with smallest data larger than or equal to key value.
Imagine we are moving down the tree, and assume we are root node. The comparison yields three possibilities,
A) Root data is equal to key. We are done, root data is ceil value.
B) Root data < key value, certainly the ceil value can’t be in left subtree. Proceed to search on right subtree as reduced problem instance.
C) Root data > key value, the ceil value may be in left subtree. We may find a node with is larger data than key value in left subtree, if not the root itself will be ceil node.
// Program to find ceil of a given value in BST#include <stdio.h>#include <stdlib.h> /* A binary tree node has key, left child and right child */struct node{ int key; struct node* left; struct node* right;}; /* Helper function that allocates a new node with the given key and NULL left and right pointers.*/struct node* newNode(int key){ struct node* node = (struct node*)malloc(sizeof(struct node)); node->key = key; node->left = NULL; node->right = NULL; return(node);} // Function to find ceil of a given input in BST. If input is more// than the max key in BST, return -1int Ceil(node *root, int input){ // Base case if( root == NULL ) return -1; // We found equal key if( root->key == input ) return root->key; // If root's key is smaller, ceil must be in right subtree if( root->key < input ) return Ceil(root->right, input); // Else, either left subtree or root has the ceil value int ceil = Ceil(root->left, input); return (ceil >= input) ? ceil : root->key;} // Driver program to test above functionint main(){ node *root = newNode(8); root->left = newNode(4); root->right = newNode(12); root->left->left = newNode(2); root->left->right = newNode(6); root->right->left = newNode(10); root->right->right = newNode(14); for(int i = 0; i < 16; i++) printf("%d %d\n", i, Ceil(root, i)); return 0;}
- [geeks]Floor and Ceil from a BST
- [geeks]Construct BST from given preorder traversal
- Play with Floor and Ceil
- uva10673 - Play with Floor and Ceil(floor和ceil)
- Does perl have a round function? What about ceil() and floor()?
- uva 10673 - Play with Floor and Ceil
- UVA 10673 Play with Floor and Ceil
- UVa 10673 - Play with Floor and Ceil
- uva 10673 - Play with Floor and Ceil
- 10673 - Play with Floor and Ceil
- UVaOJ10673 - Play with Floor and Ceil
- UVA 10673 Play with Floor and Ceil
- UVA - 10673 Play with Floor and Ceil
- UVA 10673 Play with Floor and Ceil
- UVa:10673 Play with Floor and Ceil
- UVA10673 - Play with Floor and Ceil
- UVa 10673 - Play with Floor and Ceil
- UVA10673 - Play with Floor and Ceil(数论)
- java 同步块(Java Synchronized Blocks)
- 【GBT28181开发:SIP协议实践】之注册流程
- android制作圆角图片和图片倒影
- Jquery 注册事件
- struts2 convention配置中常见配置选项及说明
- [geeks]Floor and Ceil from a BST
- 【认证系统】win7主题下载
- 康凯特脚本编辑工具简要说明(图解)
- Android Fragments详解【1】
- android Fragments详解【2】
- Sumblime Text 2 常用插件以及安装方法
- 【转】53个提高php效率的小细节--值得一看
- linux下本地yum源的配置方法
- Microsoft CRM 2011 Timeouts and Limits