- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Golang program to find floor and ceil in binary search tree
A binary search tree (BST) is a type of binary tree where every node has at most two children, commonly referred to as the left child and the right child.
In this Golang article, we will learn how to find floor and ceil in binary search tree using recursion and iterative method. The binary search tree is a useful data structure for efficient searching, insertion, and deletion of elements.
Syntax
func ceil(root *Node, val int) int {…}
The ceil() function is used to find the ceil value in the binary search tree.
func floor(root *Node, val int) int {…}
The floor() function is used to find the floor value in the binary search tree.
func floorCeil(root *Node, key int) (int, int) {…}
The floorCeil() function is used to find the floor and ceil value in the binary search tree recursively.
Algorithm
Step 1 − First, we need to import the fmt package.
Step 2 − Then, initialize a node struct and assign three variables in it. The first variable stores the integer value, while the second and third pointer variable stores the address to the left and right node respectively.
Step 3 − Now, create aninsert() function that takes a node and a value to insert. This function inserts the node value into the appropriate binary search tree.
Step 4 − This function recursively searches for the appropriate position to insert the new node based on the binary search tree property.
Step 5 − Now, define a function called ceil(). It takes a node root and a value val as input and returns the smallest value in the tree that is greater than or equal to val.
Step 6 − Then, define the floor() function that takes a node root and a value val as input and returns the largest value in the tree that is less than or equal to val.
Step 7 − Start the main() function. Inside the main() function, insert several nodes into the Binary Search tree.
Step 8 − Now, calls the ceil() and floor() function and pass the integer value as argument to the function.
Step 9 − Further, the ceil and floor value of binary search tree is printed on the screen by using the fmt.Println() function.
Example1
In this example, we will define a ceil() and floor() function iteratively that is used to find the ceil and floor in a binary search tree.
package main import ( "fmt" ) type Node struct { val int left *Node right *Node } func insert(root *Node, val int) *Node { if root == nil { root = &Node{val, nil, nil} return root } if val < root.val { root.left = insert(root.left, val) } else { root.right = insert(root.right, val) } return root } func ceil(root *Node, val int) int { if root == nil { return -1 } if root.val == val { return root.val } if val > root.val { return ceil(root.right, val) } leftCeil := ceil(root.left, val) if leftCeil >= val { return leftCeil } return root.val } func floor(root *Node, val int) int { if root == nil { return -1 } if root.val == val { return root.val } if val < root.val { return floor(root.left, val) } rightFloor := floor(root.right, val) if rightFloor <= val { return rightFloor } return root.val } func main() { var root *Node root = insert(root, 10) insert(root, 5) insert(root, 15) insert(root, 1) insert(root, 8) insert(root, 12) insert(root, 18) fmt.Println("Floor of 9:", floor(root, 9)) fmt.Println("Ceil of 9:", ceil(root, 9)) fmt.Println("Floor of 11:", floor(root, 11)) fmt.Println("Ceil of 11:", ceil(root, 11)) }
Output
Floor of 9: -1 Ceil of 9: 10 Floor of 11: -1 Ceil of 11: 12
Example 2
In this example, we will define a floorCeil() function recursively that is used to find the ceil and floor in a binary search tree.
package main import ( "fmt" ) type Node struct { val int left *Node right *Node } func newnode(val int) *Node { node := &Node{} node.val = val node.left = nil node.right = nil return node } func floorCeil(root *Node, key int) (int, int) { floor := -1 ceil := -1 if root == nil { return floor, ceil } if root.val == key { return root.val, root.val } if root.val > key { ceil = root.val f, c := floorCeil(root.left, key) if f != -1 { floor = f } if c != -1 && c < ceil { ceil = c } } else { floor = root.val f, c := floorCeil(root.right, key) if f != -1 && f > floor { floor = f } if c != -1 { ceil = c } } return floor, ceil } func main() { 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) key := 14 floor, ceil := floorCeil(root, key) fmt.Printf("Floor of %d is %d\n", key, floor) fmt.Printf("Ceil of %d is %d\n", key, ceil) key = 11 floor, ceil = floorCeil(root, key) fmt.Printf("Floor of %d is %d\n", key, floor) fmt.Printf("Ceil of %d is %d\n", key, ceil) key = 5 floor, ceil = floorCeil(root, key) fmt.Printf("Floor of %d is %d\n", key, floor) fmt.Printf("Ceil of %d is %d\n", key, ceil) }
Output
Floor of 14 is 14 Ceil of 14 is 14 Floor of 11 is 10 Ceil of 11 is 12 Floor of 5 is 4 Ceil of 5 is 6
Conclusion
We have successfully compiled and executed a go language program to find floor and ceil in binary search tree using recursion and iterative methodalong with two examples. In the first example, we have used the iterative method and in the second example, we have used the recursive method.