日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

树、二叉树、二叉搜索树_检查二叉树是否为BST(二叉搜索树)

發布時間:2023/12/1 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 树、二叉树、二叉搜索树_检查二叉树是否为BST(二叉搜索树) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

樹、二叉樹、二叉搜索樹

Description:

描述:

This article describes how to check whether a given tree is BST or not? This problem came in coding round of Microsoft.

本文介紹如何檢查給定的樹是否是BST ? 這個問題來自微軟的編碼回合。

Problem Statement:

問題陳述:

Given a binary tree check whether it is a binary search tree or not.

給定二叉樹,請檢查它是否為二叉搜索樹。

Solution

Algorithm:

算法:

From the definition of BST, it may seem that for a binary tree to be BST, it’s enough to check for each node if the node on its left is smaller & node on its right is greater. But this is actually the wrong approach since it will give wrong output for many test-cases.

從BST的定義來看,對于一棵二叉樹來說,似乎BST足以檢查每個節點,如果其左側的節點較小,而其右側的節點較大。 但這實際上是錯誤的方法,因為它將為許多測試用例提供錯誤的輸出。

The correct algorithm is to check for each node whether the maximum of the left subtree is lesser than the node & the minimum of the right subtree is greater than the node. This algorithm works perfect but not efficient in terms of time complexity.

正確的算法是檢查每個節點的左子樹的最大值是否小于該節點,以及右子樹的最小值是否大于該節點。 該算法在時間復雜度方面工作完美,但效率不高。

Intuition says that the in-order traversal for the BST results in a sorted list of nodes and we use this in our algorithm.

直覺說BST的有序遍歷會導致節點的排序列表,我們在算法中使用了它。

1. Set prev to INT_MIN. 2. From main function call checkBST(root, prev)//passing prev by reference to update it every timecheckBST(root, &prev) 3. if(root==NULL) return 1; //null tree is BST 4. do in-order traversal and checking whether all tree node data is sorted or notif(!(checkBST(root->left,prev))) //check left subtree return 0;//root->data must be greater than prevsince BST results in //sorted list after in-order traversal. 5. if(root->data<prev) return 0; 6. prev=root->data; //update prev value 7. return checkBST(root->right,prev);//check right subtree .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}} .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}}

Example 1:

范例1:

Clearly Example 1 is a binary search tree. We will check out further through our function.

顯然,示例1是一個二進制搜索樹。 我們將通過功能進一步檢查。

Example 2:

范例2:

Clearly Example 2 is not a binary tree. We will check out through our function.

顯然,示例2不是二叉樹。 我們將通過我們的功能簽出。

樹的C ++類實現 (C++ class implementation for tree)

// tree node is defined class tree{ public:int data;tree *left;tree *right; };

C ++函數checkBST實現 (C++ function checkBST for implementation)

//passing reference of prev int checkBST(tree* root,int &prev){ //null tree is BSTif(root==NULL) return 1;//doing inorder traversal and checking whether //all tree node data is sorted or notif(!(checkBST(root->left,prev))) return 0;if(root->data<prev)return 0;prev=root->data; //update prev valuereturn checkBST(root->right,prev); }

用于創建樹節點的C ++實現 (C++ implementation for creating tree nodes)

// creating new node tree* newnode(int data) { tree* node = (tree*)malloc(sizeof(tree)); node->data = data; node->left = NULL; node->right = NULL; return(node); } .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}} .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}}

主驅動程序功能例如 (Main driver function for example1)

#include <bits/stdc++.h> using namespace std;int main() { //**same tree is builted as shown in example**int c,prev=INT_MIN;//prev initialized to INT_MINcout<<"Tree is built like the example 1 aforesaid"<<endl;tree *root=newnode(8); root->left= newnode(3); root->right= newnode(10); root->right->right=newnode(14);root->right->right->left=newnode(13);root->left->left=newnode(1); root->left->right=newnode(6);root->left->right->left=newnode(4);root->left->right->right=newnode(7);cout<<"builting the binary tree like example 1......"<<endl; c=checkBST(root,prev);if(c)cout<<"This binary tree is binary search tree"<<endl;elsecout<<"This is not a binary search tree";return 0; }

主驅動程序功能例如 (Main driver function for example2)

#include <bits/stdc++.h> using namespace std;int main() { //**same tree is builted as shown in example**int c,prev=INT_MIN;//prev initialized to INT_MINcout<<"Tree is built like the example 2 aforesaid"<<endl;tree *root=newnode(2); root->left= newnode(7); root->right= newnode(5); root->right->right=newnode(9);root->right->right->left=newnode(4);root->left->left=newnode(2); root->left->right=newnode(6);root->left->right->left=newnode(5);root->left->right->right=newnode(11);cout<<"builting the binary tree like example 2......"<<endl; c=checkBST(root,prev);if(c)cout<<"This binary tree is binary search tree"<<endl;elsecout<<"This is not a binary search tree";return 0; }

Output 1

輸出1

Tree is built like the example 1 aforesaid builting the binary tree like example 1...... This binary tree is binary search tree

Output 2

輸出2

Tree is built like the example 2 aforesaid builting the binary tree like example 2...... This is not a binary search tree

翻譯自: https://www.includehelp.com/icp/check-whether-a-binary-tree-is-bst-binary-search-tree-or-not.aspx

樹、二叉樹、二叉搜索樹

總結

以上是生活随笔為你收集整理的树、二叉树、二叉搜索树_检查二叉树是否为BST(二叉搜索树)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。