list文件内容: $ cat list John Daggett, 341 King Road, Plymouth MA Alice Ford, 22 East Broadway, Richmond VA Orville Thomas, 11345 Oak Bridge Road, Tulsa OK Terry Kalkas, 402 Lans Road, Beaver Falls PA Eric Adams, 20 Post Road, Sudbury MA Hubert Sims, 328A Brook Road, Roanoke VA Amy Wilde, 334 Bayshore Pkwy, Mountain View CA Sal Carpenter, 73 6th Street, Boston MA
将 MA替换成, Massachusetts:
$ sed 's/ MA/, Massachusetts/' list John Daggett, 341 King Road, Plymouth, Massachusetts Alice Ford, 22 East Broadway, Richmond VA Orville Thomas, 11345 Oak Bridge Road, Tulsa OK Terry Kalkas, 402 Lans Road, Beaver Falls PA Eric Adams, 20 Post Road, Sudbury, Massachusetts Hubert Sims, 328A Brook Road, Roanoke VA Amy Wilde, 334 Bayshore Pkwy, Mountain View CA Sal Carpenter, 73 6th Street, Boston, Massachusetts
{print$1}表示打印第一个字段到标准输出,字段是以空格或者制表符分割的;$1表示一个字段, $2表示第二个字段,$3表示第三个字段,...以此类推。$0表示所有字段。 $ awk '{print $1}' list John Alice Orville Terry Eric Hubert Amy Sal
打印包含MA的行 $ awk '/MA/' list John Daggett, 341 King Road, Plymouth MA Eric Adams, 20 Post Road, Sudbury MA Sal Carpenter, 73 6th Street, Boston MA
打印包含MA的所有行的第一个字段: $ awk '/MA/ { print $1 }' list John Eric Sal
awk的-F参数可以设定字段分隔符:
1 2 3 4 5
将字段分隔符改为`,`,然后打印第一个字段: $ awk -F, '/MA/{print $1}' list John Daggett Eric Adams Sal Carpenter
打印1,2,3字段 $ awk -F, '{ print $1; print $2; print $3 }' list John Daggett 341 King Road Plymouth MA Alice Ford 22 East Broadway Richmond VA Orville Thomas 11345 Oak Bridge Road Tulsa OK Terry Kalkas 402 Lans Road Beaver Falls PA Eric Adams 20 Post Road Sudbury MA Hubert Sims 328A Brook Road Roanoke VA Amy Wilde 334 Bayshore Pkwy Mountain View CA Sal Carpenter 73 6th Street Boston MA
AVL树是最先发明的自平衡二叉查找树。在AVL树中任何节点的两个子树的高度最大差别为一,所以它也被称为高度平衡树。 查找、插入和删除在平均和最坏情况下都是O(log n)。增加和删除可能需要通过一次或多次树旋转来重新平衡这个树。 AVL树得名于它的发明者G.M. Adelson-Velsky和E.M. Landis,他们在1962年的论文《An algorithm for the organization of information》中发表了它。
#ifndef _AVL_H #define _AVL_H typedefstructnode *Position; typedef Position AVLtree;
typedefstructnode { int height; AVLtree left; AVLtree right; int data; }node;
intHeight(Position p); //return the height of a node Position SingleRotateWithRight(Position k2); //left rotate when in RR operation Position SingleRotateWithLeft(Position k2); // LL Position DoubleRotateWithLeft(Position k3); // LR Position DoubleRotateWithRight(Position k3); // RL Position Rotate(Position k); AVLtree insert(AVLtree root, int data); //Insert operation Position search(AVLtree root, int data); AVLtree delete(AVLtree root, int data); voidinorder_print(AVLtree root); voiddispose(AVLtree root); //free the tree #endif
intmain(int argc, charconst *argv[]) { AVLtree root = NULL; char choice; printf("Welcome to the AVL tree program\n"); while (1) { printf("Please make a choice\n"); printf("----------------------\n"); printf("i. insert an element\n"); printf("p. print the tree\n"); printf("d. delete an element from the tree\n"); printf("f. find an element \n"); printf("q. quit the program\n"); printf("----------------------\n");
/*get rid of '\n' in buffer*/ if ((choice = getchar()) == '\n') choice = getchar();
int number; Position p; switch (choice) { case'i': printf("Which number to insert?\n"); scanf("%d", &number); root = insert(root, number); break; case'p': printf("The tree is \n"); printf("######################\n"); inorder_print(root); printf("######################\n"); break; case'd': printf("Which number to delete?\n"); scanf("%d", &number); root = delete(root, number); break; case'f': printf("Which number to find?\n"); scanf("%d", &number); if (p = search(root, number)) { printf("%d is found\n", number); } else printf("Not found\n"); break; case'q': printf("Byebye!\n"); dispose(root); goto QUIT; default: printf("Wrong choice! Please try again\n"); break; } } QUIT: return0; }
Position DoubleRotateWithLeft(Position k3){ k3->left = SingleRotateWithRight(k3->left); return SingleRotateWithLeft(k3); }
Position DoubleRotateWithRight(Position k3){ k3->right = SingleRotateWithLeft(k3->right);
return SingleRotateWithRight(k3); }
Position Rotate(Position k){ if (Height(k->left) - Height(k->right) == 2) { if (Height(k->left->left) >= Height(k->left->right)) { k = SingleRotateWithLeft(k); } else { k = DoubleRotateWithLeft(k); } }
if (Height(k->right) - Height(k->left) == 2) { if (Height(k->right->right) >= Height(k->right->left)) { k = SingleRotateWithRight(k); } else { k = DoubleRotateWithRight(k); } }
return k; }
AVLtree insert(AVLtree root, int data){ if (root == NULL) { /*if the tree is empty then create a node*/ root = (node*)malloc(sizeof(node)); root->left = root->right = NULL; root->data = data; root->height = 0; }
A B-tree of order m is an m-way tree (i.e., a tree where each node may have up to m children) in which:
the number of keys in each non-leaf node is one less than the number of its children and these keys partition the keys in the children in the fashion of a search tree
all leaves are on the same level
all non-leaf nodes except the root have at least ceil(m / 2) children
the root is either a leaf node, or it has from two to m children