1123 Is It a Complete AVL Tree (30分) AVL平衡二叉搜索树层序遍历

    技术2022-07-10  92

    An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is done to restore this property. Figures 1-4 illustrate the rotation rules.

    Now given a sequence of insertions, you are supposed to output the level-order traversal sequence of the resulting AVL tree, and to tell if it is a complete binary tree.

    Input Specification:

    Each input file contains one test case. For each case, the first line contains a positive integer N (≤ 20). Then N distinct integer keys are given in the next line. All the numbers in a line are separated by a space.

    Output Specification:

    For each test case, insert the keys one by one into an initially empty AVL tree. Then first print in a line the level-order traversal sequence of the resulting AVL tree. All the numbers in a line must be separated by a space, and there must be no extra space at the end of the line. Then in the next line, print YES if the tree is complete, or NO if not.

    Sample Input 1:

    5 88 70 61 63 65

    Sample Output 1:

    70 63 88 61 65 YES

    Sample Input 2:

    8 88 70 61 96 120 90 65 68

    Sample Output 2:

    88 65 96 61 70 90 120 68 NO

     吾:其实只是层序输出方式不同,没有用queue。因为看柳之前的代码,可以通过index的大小输出层序遍历,觉得很好用,所以这里用了这种方式,也算是小小的学以致用吧。 

    树的旋转很重要!!!

    #include <iostream> #include <vector> #include <algorithm> using namespace std; int maxindex = -1; struct node { node* left, * right; int data, level,index; }; bool cmp(node *a, node *b) { return a->index < b->index; } node * rotateleft(node *root) { node *t = root->right; root->right = t->left; t->left = root; return t; } node * rotateright(node *root) { node *t = root->left; root->left = t->right; t->right = root; return t; } node * leftToright(node *root) { root->left=rotateleft(root->left); return rotateright(root); } node * rightToleft(node *root) { root->right=rotateright(root->right); return rotateleft(root); } int getheight(node *root) { if (root==NULL) { return 0; } return max(getheight(root->left), getheight(root->right)) + 1; } void dfsindex(node *root) { if (root!=NULL&&root->index>maxindex) { maxindex = root->index; } if (root->left!=NULL) { root->left->index = root->index * 2; dfsindex(root->left); } if (root->right!=NULL) { root->right->index = root->index * 2 + 1; dfsindex(root->right); } } node* insert(node* root, int value) { if (root==NULL) { root = new node(); root->data = value; root->left = NULL; root->right = NULL; } else if(value<root->data) { root->left=insert(root->left, value); if (getheight(root->left) - getheight(root->right) == 2) { root=value < root->left->data ? rotateright(root) : leftToright(root); } } else { root->right = insert(root->right, value); if (getheight(root->right)-getheight(root->left)==2) { root = value > root->right->data ? rotateleft(root) : rightToleft(root); } } return root; } vector<node*> v; void getnode(node* root) { if (root!=NULL) { v.push_back(root); } if (root->left!=NULL) { getnode(root->left); } if (root->right!=NULL) { getnode(root->right); } } int main() {//其实也只是层序输出方式不同而已,前面的调整数要会 //freopen("in.txt", "r", stdin); int n,t; cin >> n; node *root = NULL; for (int i = 0; i < n; i++) { cin >> t; root=insert(root, t); } root->index = 1; dfsindex(root);//从上到下,把每个点的index纠正好 getnode(root);//遍历树种所有的点,压入v中 sort(v.begin(), v.end(), cmp);//把v按照index排序,用于层序输出 cout << v[0]->data; for (int i = 1; i < v.size(); i++) { cout << " " << v[i]->data; } cout << endl; cout << ((maxindex == n) ? "YES" : "NO"); cout<< endl; return 0; }

     

    Processed: 0.029, SQL: 9