bst

二叉排序树操作(一)

浪尽此生 提交于 2019-11-26 17:42:20
判断给定的二叉树是否是二叉排序树 void JudegBST(BSTree &T){ Queue q; BSTree bst; int flag=1; q.front=-1; q.rear=-1; q.a[++q.rear]=T; while(q.front<q.rear){ bst=q.a[++q.front]; if(bst->lchild){ if(bst->lchild->data<=bst->data){ q.a[++q.rear]=bst->lchild; } else{flag=0;break;} } if(bst->rchild){ if(bst->rchild->data>=bst->data){ q.a[++q.rear]=bst->rchild; } else{flag=0;break;} } } if(flag==0){printf("不是二叉排序树\n");} else{printf("是二叉排序树\n");} } 来源: https://www.cnblogs.com/Yshun/p/11329529.html

POJ 2309 BST

喜欢而已 提交于 2019-11-26 16:08:29
题目链接: https://vjudge.net/problem/POJ-2309 题目大意   略。 分析   lowbit的运用很妙,学到了。 代码如下 1 #include <cmath> 2 #include <ctime> 3 #include <iostream> 4 #include <string> 5 #include <vector> 6 #include <cstdio> 7 #include <cstdlib> 8 #include <cstring> 9 #include <queue> 10 #include <map> 11 #include <set> 12 #include <algorithm> 13 #include <cctype> 14 #include <stack> 15 #include <deque> 16 #include <list> 17 #include <sstream> 18 #include <cassert> 19 using namespace std; 20 21 #define INIT() ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); 22 #define Rep(i,n) for (int i = 0; i < (int)(n); ++i) 23

UVA 1525 Falling Leaves

别来无恙 提交于 2019-11-26 16:04:54
题目链接: https://vjudge.net/problem/UVA-1525 题目链接: https://vjudge.net/problem/POJ-1577 题目大意   略。 分析   建树,然后先序遍历。 代码如下 1 #include <cmath> 2 #include <ctime> 3 #include <iostream> 4 #include <string> 5 #include <vector> 6 #include <cstdio> 7 #include <cstdlib> 8 #include <cstring> 9 #include <queue> 10 #include <map> 11 #include <set> 12 #include <algorithm> 13 #include <cctype> 14 #include <stack> 15 #include <deque> 16 #include <list> 17 #include <sstream> 18 #include <cassert> 19 using namespace std; 20 21 #define INIT() ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); 22 #define Rep(i,n) for

二叉树的遍历

我只是一个虾纸丫 提交于 2019-11-26 14:04:45
前序遍历 若树为空,则空操作返回。否则,先访问根节点,然后前序遍历左子树,再前序遍历右子树。(中 左 右) 中序遍历 若树为空,则空操作返回。否则,从根节点开始(注意并不是先访问根节点),中序遍历根节点的左子树,然后是访问根节点,最后中序遍历根节点的右子树。(左 中 右) 后续遍历 若树为空,则空操作返回。否则,从左到右先叶子后节点的方式遍历访问左右子树,最后访问根节点。(左 右 中) 层序遍历 若树为空,则空操作返回。否则,从树的第一层,也就是根节点开始访问,从上到下逐层遍历,在同一层中,按从左到右的顺序结点逐个访问。 /** * 前序遍历BST树的API接口 */ public void preOrder(){ System.out.print(“递归前序遍历:”); preOrder(this.root); System.out.println(); } /** * 前序遍历BST树的递归操作 VLR * @param root */ private void preOrder(BSTNode<T> root) { if(root != null){ System.out.print(root.getData() + " "); preOrder(root.getLeft()); preOrder(root.getRight()); } } /** *