原文:
给定两个值 n1 和 n2(其中 n1 < n2) and a root pointer to a binary search tree. print all the keys of tree in range n1 to n2. i.e. print all nodes n such that n1<=n<=n2 and n is a key of given bst. print all the keys in increasing order. 先决条件: | 有序遍历使用递归或消耗 0(n)空间的堆栈/队列。但是有一种有效的方法可以使用基于线程二叉树的莫里斯遍历来进行有序树遍历。morris 遍历不使用递归或堆栈/队列,只是在浪费的空指针中存储一些重要信息。morris 遍历消耗恒定的额外内存 o(1),因为它不使用递归或堆栈/队列。因此,我们将使用 morris 遍历来执行本教程中介绍的算法中的有序遍历,以打印给定范围内的 bst 的键,这在内存方面是高效的。 线程二叉树的概念很简单,因为它们在浪费的空指针中存储了一些有用的信息。在有 n 个节点的普通二叉树中,n 1 个空指针会浪费内存。 方法:莫里斯遍历是一种非常好的内存高效的技术,可以在基于线程二叉树的常量内存 o(1)中不使用堆栈或递归来进行树遍历。morris 遍历可用于解决使用有序树遍历的问题,尤其是在顺序统计eg-中第 kth 个最大元素,中第 kth 个最小元素等。因此,这就是 morris 遍历作为一种更有效的方法在常量 o(1)空间中进行有序遍历而不使用任何堆栈或递归的地方。 算法
1) initialize current as root.
2) while current is not null :
2.1) if current has no left child
a) check if current lies between n1 and n2.
1)if so, then visit the current node.
b)otherwise, move to the right child of current.
3) else, here we have 2 cases:
a) find the inorder predecessor of current node.
inorder predecessor is the right most node
in the left subtree or left child itself.
b) if the right child of the inorder predecessor is null:
1) set current as the right child of its inorder predecessor.
2) move current node to its left child.
c) else, if the threaded link between the current node
and it's inorder predecessor already exists :
1) set right pointer of the inorder predecessor as null.
2) again check if current node lies between n1 and n2.
a)if so, then visit the current node.
3)now move current to it's right child.
以下是上述方法的实施。
c
// cpp code to print bst keys in given range in
// constant space using morris traversal.
#include
using namespace std;
struct node {
int data;
struct node *left, *right;
};
// function to print the keys in range
void rangetraversal(node* root,
int n1, int n2)
{
if (!root)
return;
node* curr = root;
while (curr) {
if (curr->left == null)
{
// check if current node
// lies between n1 and n2
if (curr->data <= n2 &&
curr->data >= n1)
{
cout << curr->data << " ";
}
curr = curr->right;
}
else {
node* pre = curr->left;
// finding the inorder predecessor-
// inorder predecessor is the right
// most in left subtree or the left
// child, i.e in bst it is the
// maximum(right most) in left subtree.
while (pre->right != null &&
pre->right != curr)
pre = pre->right;
if (pre->right == null)
{
pre->right = curr;
curr = curr->left;
}
else {
pre->right = null;
// check if current node lies
// between n1 and n2
if (curr->data <= n2 &&
curr->data >= n1)
{
cout << curr->data << " ";
}
curr = curr->right;
}
}
}
}
// helper function to create a new node
node* newnode(int data)
{
node* temp = new node;
temp->data = data;
temp->right = temp->left = null;
return temp;
}
// driver code
int main()
{
/* constructed binary tree is
4
/ \
2 7
/ \ / \
1 3 6 10
*/
node* root = newnode(4);
root->left = newnode(2);
root->right = newnode(7);
root->left->left = newnode(1);
root->left->right = newnode(3);
root->right->left = newnode(6);
root->right->right = newnode(10);
rangetraversal(root, 4, 12);
return 0;
}
java 语言(一种计算机语言,尤用于创建网站)
// java code to print bst keys in given range in
// constant space using morris traversal.
class gfg {
static class node {
int data;
node left, right;
}
// function to print the keys in range
static void rangetraversal(node root, int n1, int n2)
{
if (root == null)
return;
node curr = root;
while (curr != null) {
if (curr.left == null)
{
// check if current node
// lies between n1 and n2
if (curr.data <= n2 && curr.data >= n1)
{
system.out.print(curr.data " ");
}
curr = curr.right;
}
else {
node pre = curr.left;
// finding the inorder predecessor-
// inorder predecessor is the right
// most in left subtree or the left
// child, i.e in bst it is the
// maximum(right most) in left subtree.
while (pre.right != null && pre.right != curr)
pre = pre.right;
if (pre.right == null)
{
pre.right = curr;
curr = curr.left;
}
else {
pre.right = null;
// check if current node lies
// between n1 and n2
if (curr.data <= n2 && curr.data >= n1)
{
system.out.print(curr.data " ");
}
curr = curr.right;
}
}
}
}
// helper function to create a new node
static node newnode(int data)
{
node temp = new node();
temp.data = data;
temp.right = null;
temp.left = null;
return temp;
}
// driver code
public static void main(string[] args)
{
/* constructed binary tree is
4
/ \
2 7
/ \ / \
1 3 6 10
*/
node root = newnode(4);
root.left = newnode(2);
root.right = newnode(7);
root.left.left = newnode(1);
root.left.right = newnode(3);
root.right.left = newnode(6);
root.right.right = newnode(10);
rangetraversal(root, 4, 12);
}
}
python 3
# python3 code to print bst keys in given range
# in constant space using morris traversal.
# helper function to create a new node
class newnode:
# constructor to create a new node
def __init__(self, data):
self.data = data
self.left = none
self.right = none
# function to print the keys in range
def rangetraversal(root, n1, n2):
if root == none:
return
curr = root
while curr:
if curr.left == none:
# check if current node lies
# between n1 and n2
if curr.data <= n2 and curr.data >= n1:
print(curr.data, end = " ")
curr = curr.right
else:
pre = curr.left
# finding the inorder predecessor-
# inorder predecessor is the right
# most in left subtree or the left
# child, i.e in bst it is the
# maximum(right most) in left subtree.
while (pre.right != none and
pre.right != curr):
pre = pre.right
if pre.right == none:
pre.right = curr;
curr = curr.left
else:
pre.right = none
# check if current node lies
# between n1 and n2
if curr.data <= n2 and curr.data >= n1:
print(curr.data, end = " ")
curr = curr.right
# driver code
if __name__ == '__main__':
# constructed binary tree is
# 4
# / \
# 2 7
# / \ / \
# 1 3 6 10
root = newnode(4)
root.left = newnode(2)
root.right = newnode(7)
root.left.left = newnode(1)
root.left.right = newnode(3)
root.right.left = newnode(6)
root.right.right = newnode(10)
rangetraversal(root, 4, 12)
# this code is contributed by pranchalk
c
// c# code to print bst keys in given range in
// constant space using morris traversal.
using system;
public class gfg
{
public class node
{
public int data;
public node left, right;
}
// function to print the keys in range
static void rangetraversal(node root, int n1, int n2)
{
if (root == null)
return;
node curr = root;
while (curr != null)
{
if (curr.left == null)
{
// check if current node
// lies between n1 and n2
if (curr.data <= n2 && curr.data >= n1)
{
console.write(curr.data " ");
}
curr = curr.right;
}
else
{
node pre = curr.left;
// finding the inorder predecessor-
// inorder predecessor is the right
// most in left subtree or the left
// child, i.e in bst it is the
// maximum(right most) in left subtree.
while (pre.right != null && pre.right != curr)
pre = pre.right;
if (pre.right == null)
{
pre.right = curr;
curr = curr.left;
}
else
{
pre.right = null;
// check if current node lies
// between n1 and n2
if (curr.data <= n2 && curr.data >= n1)
{
console.write(curr.data " ");
}
curr = curr.right;
}
}
}
}
// helper function to create a new node
static node newnode(int data)
{
node temp = new node();
temp.data = data;
temp.right = null;
temp.left = null;
return temp;
}
// driver code
public static void main(string[] args)
{
/* constructed binary tree is
4
/ \
2 7
/ \ / \
1 3 6 10
*/
node root = newnode(4);
root.left = newnode(2);
root.right = newnode(7);
root.left.left = newnode(1);
root.left.right = newnode(3);
root.right.left = newnode(6);
root.right.right = newnode(10);
rangetraversal(root, 4, 12);
}
}
// this code has been contributed by 29ajaykumar
java 描述语言
output:
4 6 7 10
时间复杂度:o(n) t3】辅助空间: o(1)
麻将胡了pg电子网站的版权属于:月萌api www.moonapi.com,转载请注明出处