标签:
<pre class="code"><span style="font-family: %value; font-size: 14px;">03-树1. List Leaves (25) Given a tree, you are supposed to list all the leaves in the order of top down, and left to right. Input Specification: Each input file contains one test case. For each case, the first line gives a positive integer N (<=10) which is the total number of nodes in the tree -- and hence the nodes are numbered from 0 to N-1. Then N lines follow, each corresponds to a node, and gives the indices of the left and right children of the node. If the child does not exist, a "-" will be put at the position. Any pair of children are separated by a space. Output Specification: For each test case, print in one line all the leaves‘ indices in the order of top down, and left to right. There must be exactly one space between any adjacent numbers, and no extra space at the end of the line. Sample Input: 8 1 - - - 0 - 2 7 - - - - 5 - 4 6 Sample Output: 4 1 5 </span></pre> <pre class="code"><span style="font-family: %value; font-size: 14px;"><span style="color: #008000;">/*</span><span style="color: #008000;"> 思路:需要动态的建立二叉树,然后层次遍历输出叶结点,但是有一个问题是要防止指针丢失,因此我在程序中用一个数组维护各个结点的指针;最后树建完以后,还要想办法找到数根,我采取的措施是:遍历维护结点指针的数组,对其左、右结点值求和,这样除了根结点以外,所有结点都被计入总和中了,利用这一信息便可以找到根节点的位置。 </span><span style="color: #008000;">*/</span><span style="color: #000000;"> #include</span><stdio.h><span style="color: #000000;"> #include</span><<span style="color: #0000ff;">string</span>.h><span style="color: #000000;"> #include</span><<span style="color: #0000ff;">malloc</span>.h> <span style="color: #0000ff;">#define</span> MAXN 10<span style="color: #000000;"> typedef </span><span style="color: #0000ff;">struct</span><span style="color: #000000;"> tnode { </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> data; </span><span style="color: #0000ff;">struct</span> tnode *<span style="color: #000000;"> left; </span><span style="color: #0000ff;">struct</span> tnode *<span style="color: #000000;"> right; }TNODE, </span>*<span style="color: #000000;">PTNODE; typedef </span><span style="color: #0000ff;">struct</span><span style="color: #000000;"> qnode { PTNODE data; </span><span style="color: #0000ff;">struct</span> qnode *<span style="color: #000000;"> next; }QNODE, </span>*<span style="color: #000000;">PQNODE; typedef </span><span style="color: #0000ff;">struct</span><span style="color: #000000;"> queue { PQNODE front; PQNODE rear; }QUEUE, </span>*<span style="color: #000000;">PQUEUE; PQUEUE MakeQueue(); </span><span style="color: #0000ff;">void</span><span style="color: #000000;"> Add(PQUEUE, PTNODE pTNode); PTNODE Delete(PQUEUE pQ); </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> IsEmpty(PQUEUE pQ); PTNODE arr[MAXN</span>+<span style="color: #800080;">1</span><span style="color: #000000;">]; </span><span style="color: #0000ff;">void</span> Dispose(<span style="color: #0000ff;">int</span> i, <span style="color: #0000ff;">char</span> left, <span style="color: #0000ff;">char</span><span style="color: #000000;"> right); PTNODE FindRoot(</span><span style="color: #0000ff;">int</span> n); <span style="color: #008000;">//</span><span style="color: #008000;"> n个节点中找根节点</span> <span style="color: #0000ff;">int</span> main(<span style="color: #0000ff;">void</span><span style="color: #000000;">) { </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> n, i, isFirst; </span><span style="color: #0000ff;">char</span><span style="color: #000000;"> left, right; PTNODE root, head; scanf(</span><span style="color: #800000;">"</span><span style="color: #800000;">%d</span><span style="color: #800000;">"</span>, &<span style="color: #000000;">n); memset(arr, </span><span style="color: #800080;">0</span>, <span style="color: #0000ff;">sizeof</span><span style="color: #000000;">(arr)); </span><span style="color: #0000ff;">for</span>(i = <span style="color: #800080;">0</span>; i < n; i++<span style="color: #000000;">) { scanf(</span><span style="color: #800000;">"</span><span style="color: #800000;"> %c %c</span><span style="color: #800000;">"</span>, &left, &<span style="color: #000000;">right); Dispose(i, left, right); } root </span>=<span style="color: #000000;"> FindRoot(n); </span><span style="color: #008000;">//</span><span style="color: #008000;"> printf("%d\n", root->data); </span><span style="color: #008000;">//</span><span style="color: #008000;"> test </span><span style="color: #008000;">//</span><span style="color: #008000;"> 层次遍历</span> PQUEUE pQ =<span style="color: #000000;"> MakeQueue(); Add(pQ, root); isFirst </span>= <span style="color: #800080;">1</span><span style="color: #000000;">; </span><span style="color: #0000ff;">while</span>(!<span style="color: #000000;">IsEmpty(pQ)) { head </span>=<span style="color: #000000;"> Delete(pQ); </span><span style="color: #0000ff;">if</span>(head->left == NULL && head->right ==<span style="color: #000000;"> NULL) { </span><span style="color: #0000ff;">if</span><span style="color: #000000;">(isFirst) isFirst </span>= <span style="color: #800080;">0</span><span style="color: #000000;">; </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> putchar(</span><span style="color: #800000;">‘</span> <span style="color: #800000;">‘</span><span style="color: #000000;">); printf(</span><span style="color: #800000;">"</span><span style="color: #800000;">%d</span><span style="color: #800000;">"</span>, head-><span style="color: #000000;">data); } Add(pQ, head</span>-><span style="color: #000000;">left); Add(pQ, head</span>-><span style="color: #000000;">right); } </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">0</span><span style="color: #000000;">; } </span><span style="color: #0000ff;">void</span> Dispose(<span style="color: #0000ff;">int</span> i, <span style="color: #0000ff;">char</span> left, <span style="color: #0000ff;">char</span> right) <span style="color: #008000;">//</span><span style="color: #008000;"> 当前节点,左、右节点哪个没有建哪个</span> <span style="color: #000000;">{ </span><span style="color: #0000ff;">if</span>(arr[i] ==<span style="color: #000000;"> NULL) arr[i] </span>= (PTNODE)<span style="color: #0000ff;">malloc</span>(<span style="color: #0000ff;">sizeof</span><span style="color: #000000;">(TNODE)); arr[i]</span>->data =<span style="color: #000000;"> i; </span><span style="color: #0000ff;">if</span>(left == <span style="color: #800000;">‘</span><span style="color: #800000;">-</span><span style="color: #800000;">‘</span><span style="color: #000000;">) arr[i]</span>->left =<span style="color: #000000;"> NULL; </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> { </span><span style="color: #0000ff;">if</span>(arr[left-<span style="color: #800000;">‘</span><span style="color: #800000;">0</span><span style="color: #800000;">‘</span>] ==<span style="color: #000000;"> NULL) arr[left</span>-<span style="color: #800000;">‘</span><span style="color: #800000;">0</span><span style="color: #800000;">‘</span>] = (PTNODE)<span style="color: #0000ff;">malloc</span>(<span style="color: #0000ff;">sizeof</span><span style="color: #000000;">(TNODE)); arr[i]</span>->left = arr[left-<span style="color: #800000;">‘</span><span style="color: #800000;">0</span><span style="color: #800000;">‘</span><span style="color: #000000;">]; } </span><span style="color: #0000ff;">if</span>(right == <span style="color: #800000;">‘</span><span style="color: #800000;">-</span><span style="color: #800000;">‘</span><span style="color: #000000;">) arr[i]</span>->right =<span style="color: #000000;"> NULL; </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> { </span><span style="color: #0000ff;">if</span>(arr[right-<span style="color: #800000;">‘</span><span style="color: #800000;">0</span><span style="color: #800000;">‘</span>] ==<span style="color: #000000;"> NULL) arr[right</span>-<span style="color: #800000;">‘</span><span style="color: #800000;">0</span><span style="color: #800000;">‘</span>] = (PTNODE)<span style="color: #0000ff;">malloc</span>(<span style="color: #0000ff;">sizeof</span><span style="color: #000000;">(TNODE)); arr[i]</span>->right = arr[right-<span style="color: #800000;">‘</span><span style="color: #800000;">0</span><span style="color: #800000;">‘</span><span style="color: #000000;">]; } </span><span style="color: #0000ff;">return</span><span style="color: #000000;">; } PTNODE FindRoot(</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> n) { </span><span style="color: #0000ff;">int</span> i, sum = <span style="color: #800080;">0</span><span style="color: #000000;">; </span><span style="color: #0000ff;">for</span>(i = <span style="color: #800080;">0</span>; i < n; i++<span style="color: #000000;">) { </span><span style="color: #0000ff;">if</span>(arr[i]-><span style="color: #000000;">left) sum </span>+= arr[i]->left-><span style="color: #000000;">data; </span><span style="color: #0000ff;">if</span>(arr[i]-><span style="color: #000000;">right) sum </span>+= arr[i]->right-><span style="color: #000000;">data; } </span><span style="color: #0000ff;">return</span> arr[n*(n-<span style="color: #800080;">1</span>)/<span style="color: #800080;">2</span> - sum]; <span style="color: #008000;">//</span><span style="color: #008000;"> 这里用了一点技巧</span> <span style="color: #000000;">} PQUEUE MakeQueue() { PQUEUE pQ </span>= <span style="color: #0000ff;">malloc</span>(<span style="color: #0000ff;">sizeof</span><span style="color: #000000;">(QUEUE)); pQ</span>->front = pQ->rear =<span style="color: #000000;"> NULL; </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> pQ; } </span><span style="color: #0000ff;">void</span><span style="color: #000000;"> Add(PQUEUE pQ, PTNODE pTNode) { </span><span style="color: #0000ff;">if</span>(pTNode ==<span style="color: #000000;"> NULL) </span><span style="color: #0000ff;">return</span><span style="color: #000000;">; PQNODE p </span>= (PQNODE)<span style="color: #0000ff;">malloc</span>(<span style="color: #0000ff;">sizeof</span>(QNODE)); <span style="color: #008000;">//</span><span style="color: #008000;"> 注意正规的程序应该检查申请堆内存是否成功</span> p->data =<span style="color: #000000;"> pTNode; p</span>->next =<span style="color: #000000;"> NULL; </span><span style="color: #0000ff;">if</span>(pQ->front ==<span style="color: #000000;"> NULL) pQ</span>->front = pQ->rear =<span style="color: #000000;"> p; </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> { pQ</span>->rear->next =<span style="color: #000000;"> p; pQ</span>->rear =<span style="color: #000000;"> p; } } PTNODE Delete(PQUEUE pQ) { PTNODE p; PQNODE pTemp; </span><span style="color: #0000ff;">if</span>(pQ->front ==<span style="color: #000000;"> NULL) </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> NULL; </span><span style="color: #0000ff;">if</span>(pQ->rear == pQ-><span style="color: #000000;">front) pQ</span>->rear =<span style="color: #000000;"> NULL; p </span>= pQ->front-><span style="color: #000000;">data; pTemp </span>= pQ-><span style="color: #000000;">front; pQ</span>->front = pQ->front-><span style="color: #000000;">next; </span><span style="color: #0000ff;">free</span><span style="color: #000000;">(pTemp); </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> p; } </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> IsEmpty(PQUEUE pQ) { </span><span style="color: #0000ff;">return</span> pQ->front == NULL ? <span style="color: #800080;">1</span> : <span style="color: #800080;">0</span><span style="color: #000000;">; }</span></span></pre> <p> </p> <pre class="code"><span style="font-family: %value; font-size: 14px;">03-树2. Tree Traversals Again (25) An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example, suppose that when a 6-node binary tree (with the keys numbered from 1 to 6) is traversed, the stack operations are: push(1); push(2); push(3); pop(); pop(); push(4); pop(); pop(); push(5); push(6); pop(); pop(). Then a unique binary tree (shown in Figure 1) can be generated from this sequence of operations. Your task is to give the postorder traversal sequence of this tree. </span></pre> <p><img src="http://images0.cnblogs.com/blog2015/672107/201506/201900488733317.png" /></p> <pre class="code"><span style="font-family: %value; font-size: 14px;">Input Specification: Each input file contains one test case. For each case, the first line contains a positive integer N (<=30) which is the total number of nodes in a tree (and hence the nodes are numbered from 1 to N). Then 2N lines follow, each describes a stack operation in the format: "Push X" where X is the index of the node being pushed onto the stack; or "Pop" meaning to pop one node from the stack. Output Specification: For each test case, print the postorder traversal sequence of the corresponding tree in one line. A solution is guaranteed to exist. All the numbers must be separated by exactly one space, and there must be no extra space at the end of the line. Sample Input: 6 Push 1 Push 2 Push 3 Pop Pop Push 4 Pop Pop Push 5 Push 6 Pop Pop Sample Output: 3 4 2 6 5 1 </span></pre> <pre class="code"><span style="font-family: %value; font-size: 14px;"><span style="color: #008000;">/*</span><span style="color: #008000;"> 思路:先用一个真实的栈模拟输入,据此中序建树,然后执行后序遍历输出结果即可 </span><span style="color: #008000;">*/</span><span style="color: #000000;"> #include</span><stdio.h><span style="color: #000000;"> #include</span><<span style="color: #0000ff;">malloc</span>.h><span style="color: #000000;"> typedef </span><span style="color: #0000ff;">struct</span><span style="color: #000000;"> tnode { </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> data; </span><span style="color: #0000ff;">struct</span> tnode *<span style="color: #000000;"> left; </span><span style="color: #0000ff;">struct</span> tnode *<span style="color: #000000;"> right; }TNODE, </span>*<span style="color: #000000;">PTNODE; typedef </span><span style="color: #0000ff;">struct</span><span style="color: #000000;"> snode { PTNODE data; </span><span style="color: #0000ff;">struct</span> snode *<span style="color: #000000;"> next; }SNODE, </span>*<span style="color: #000000;">PSNODE; PSNODE MakeStack(); </span><span style="color: #0000ff;">void</span> Push(PSNODE *<span style="color: #000000;"> pS, PTNODE data); PTNODE Pop(PSNODE </span>*<span style="color: #000000;"> pS); </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> IsEmpty(PSNODE pS); PTNODE Dispose(PTNODE </span>* lastPTNode, <span style="color: #0000ff;">int</span><span style="color: #000000;"> numberNode); </span><span style="color: #0000ff;">void</span><span style="color: #000000;"> PostOrderTraverse(PTNODE pTNode); </span><span style="color: #0000ff;">int</span> first = <span style="color: #800080;">1</span>; <span style="color: #008000;">//</span><span style="color: #008000;"> 后序遍历中是否是第一个输出节点的标记</span> <span style="color: #0000ff;">int</span> main(<span style="color: #0000ff;">void</span><span style="color: #000000;">) { </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> n, numberNode; PTNODE root, lastPTNode, currentPTNode; PSNODE pS; </span><span style="color: #0000ff;">char</span> str[<span style="color: #800080;">10</span><span style="color: #000000;">]; pS </span>=<span style="color: #000000;"> MakeStack(); scanf(</span><span style="color: #800000;">"</span><span style="color: #800000;">%d</span><span style="color: #800000;">"</span>, &<span style="color: #000000;">n); scanf(</span><span style="color: #800000;">"</span><span style="color: #800000;">%s%d</span><span style="color: #800000;">"</span>, str, &<span style="color: #000000;">numberNode); </span><span style="color: #008000;">//</span><span style="color: #008000;"> 建立数根root,并将之压栈</span> root = lastPTNode = (PTNODE)<span style="color: #0000ff;">malloc</span>(<span style="color: #0000ff;">sizeof</span><span style="color: #000000;">(TNODE)); root</span>->data =<span style="color: #000000;"> numberNode; root</span>->left = root->right =<span style="color: #000000;"> NULL; Push(</span>&<span style="color: #000000;">pS, root); </span><span style="color: #0000ff;">while</span>(scanf(<span style="color: #800000;">"</span><span style="color: #800000;">%s</span><span style="color: #800000;">"</span>, str) !=<span style="color: #000000;"> EOF) { </span><span style="color: #0000ff;">if</span>(str[<span style="color: #800080;">1</span>] == <span style="color: #800000;">‘</span><span style="color: #800000;">u</span><span style="color: #800000;">‘</span><span style="color: #000000;">) { scanf(</span><span style="color: #800000;">"</span><span style="color: #800000;">%d</span><span style="color: #800000;">"</span>, &<span style="color: #000000;">numberNode); currentPTNode </span>= Dispose(&<span style="color: #000000;">lastPTNode, numberNode); Push(</span>&<span style="color: #000000;">pS, currentPTNode); } </span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span>(str[<span style="color: #800080;">1</span>] == <span style="color: #800000;">‘</span><span style="color: #800000;">o</span><span style="color: #800000;">‘</span><span style="color: #000000;">) { lastPTNode </span>= Pop(&<span style="color: #000000;">pS); } } PostOrderTraverse(root); </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">0</span><span style="color: #000000;">; } PSNODE MakeStack() { PSNODE p </span>= (PSNODE)<span style="color: #0000ff;">malloc</span>(<span style="color: #0000ff;">sizeof</span><span style="color: #000000;">(SNODE)); p</span>->data =<span style="color: #000000;"> NULL; </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> p; } </span><span style="color: #0000ff;">void</span> Push(PSNODE *<span style="color: #000000;"> pS, PTNODE data) { PSNODE p </span>= (PSNODE)<span style="color: #0000ff;">malloc</span>(<span style="color: #0000ff;">sizeof</span><span style="color: #000000;">(SNODE)); p</span>->data =<span style="color: #000000;"> data; p</span>->next = *<span style="color: #000000;">pS; </span>*pS =<span style="color: #000000;"> p; } PTNODE Pop(PSNODE </span>*<span style="color: #000000;"> pS) { PTNODE data; PSNODE pTemp; </span><span style="color: #0000ff;">if</span>(IsEmpty(*<span style="color: #000000;">pS)) </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> NULL; data </span>= (*pS)->data; <span style="color: #008000;">//</span><span style="color: #008000;"> 优先级!</span> pTemp = *<span style="color: #000000;">pS; </span>*pS = (*pS)-><span style="color: #000000;">next; </span><span style="color: #0000ff;">free</span><span style="color: #000000;">(pTemp); </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> data; } </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> IsEmpty(PSNODE pS) { </span><span style="color: #0000ff;">return</span> pS == NULL ? <span style="color: #800080;">1</span> : <span style="color: #800080;">0</span><span style="color: #000000;">; } PTNODE Dispose(PTNODE </span>* lastPTNode, <span style="color: #0000ff;">int</span><span style="color: #000000;"> numberNode) { PTNODE pTemp; pTemp </span>= (PTNODE)<span style="color: #0000ff;">malloc</span>(<span style="color: #0000ff;">sizeof</span><span style="color: #000000;">(TNODE)); pTemp</span>->data =<span style="color: #000000;"> numberNode; pTemp</span>->left = pTemp->right =<span style="color: #000000;"> NULL; </span><span style="color: #0000ff;">if</span>((*lastPTNode)->left == NULL) <span style="color: #008000;">//</span><span style="color: #008000;"> 注意运算符优先级</span> (*lastPTNode)->left =<span style="color: #000000;"> pTemp; </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> (</span>*lastPTNode)->right =<span style="color: #000000;"> pTemp; </span>*lastPTNode =<span style="color: #000000;"> pTemp; </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> pTemp; } </span><span style="color: #0000ff;">void</span><span style="color: #000000;"> PostOrderTraverse(PTNODE pTNode) { </span><span style="color: #0000ff;">if</span><span style="color: #000000;">(pTNode) { PostOrderTraverse(pTNode</span>-><span style="color: #000000;">left); PostOrderTraverse(pTNode</span>-><span style="color: #000000;">right); </span><span style="color: #0000ff;">if</span><span style="color: #000000;">(first) first </span>= <span style="color: #800080;">0</span><span style="color: #000000;">; </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> putchar(</span><span style="color: #800000;">‘</span> <span style="color: #800000;">‘</span><span style="color: #000000;">); printf(</span><span style="color: #800000;">"</span><span style="color: #800000;">%d</span><span style="color: #800000;">"</span>, pTNode-><span style="color: #000000;">data); } </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> ; }</span></span></pre> <pre class="code"><span style="font-family: %value; font-size: 14px;">04-树3. Root of AVL Tree (25) 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. </span></pre> <p><img src="http://images0.cnblogs.com/blog2015/672107/201506/201927126709124.png" /></p> <pre class="code"><span style="font-family: %value; font-size: 14px;"> Now given a sequence of insertions, you are supposed to tell the root of the resulting AVL tree. Input Specification: Each input file contains one test case. For each case, the first line contains a positive integer N (<=20) which is the total number of keys to be inserted. 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, print ythe root of the resulting AVL tree in one line. Sample Input 1: 5 88 70 61 96 120 Sample Output 1: 70 Sample Input 2: 7 88 70 61 96 120 90 65 Sample Output 2: 88 </span></pre> <pre class="code"><span style="font-family: %value; font-size: 14px;">// 很直白的考察AVL建树,不过多赘述了~<br />#include<stdio.h><span style="color: #000000;"> #include</span><<span style="color: #0000ff;">malloc</span>.h><span style="color: #000000;"> typedef </span><span style="color: #0000ff;">struct</span><span style="color: #000000;"> node { </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> data; </span><span style="color: #0000ff;">struct</span> node *<span style="color: #000000;"> left; </span><span style="color: #0000ff;">struct</span> node *<span style="color: #000000;"> right; </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> height; }NODE, </span>*<span style="color: #000000;">PNODE; PNODE Insert(PNODE pNode, </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> data); </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> Height(PNODE pNode); </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> MaxHeight(PNODE pLeft, PNODE pRight); PNODE SingleRightRotation(PNODE A); PNODE DoubleRightLeftRotation(PNODE A); PNODE SingleLeftRotation(PNODE A); PNODE DoubleLeftRightRotation(PNODE A); </span><span style="color: #0000ff;">int</span> main(<span style="color: #0000ff;">void</span><span style="color: #000000;">) { </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> n, data; PNODE avlRoot; scanf(</span><span style="color: #800000;">"</span><span style="color: #800000;">%d</span><span style="color: #800000;">"</span>, &<span style="color: #000000;">n); scanf(</span><span style="color: #800000;">"</span><span style="color: #800000;">%d</span><span style="color: #800000;">"</span>, &<span style="color: #000000;">data); avlRoot </span>=<span style="color: #000000;"> Insert(NULL, data); n</span>--<span style="color: #000000;">; </span><span style="color: #0000ff;">while</span>(n--<span style="color: #000000;">) { scanf(</span><span style="color: #800000;">"</span><span style="color: #800000;">%d</span><span style="color: #800000;">"</span>, &<span style="color: #000000;">data); avlRoot </span>=<span style="color: #000000;"> Insert(avlRoot, data); } printf(</span><span style="color: #800000;">"</span><span style="color: #800000;">%d\n</span><span style="color: #800000;">"</span>, avlRoot-><span style="color: #000000;">data); </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">0</span><span style="color: #000000;">; } PNODE Insert(PNODE pRoot, </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> data) { </span><span style="color: #0000ff;">if</span>(pRoot ==<span style="color: #000000;"> NULL) { pRoot </span>= (PNODE)<span style="color: #0000ff;">malloc</span>(<span style="color: #0000ff;">sizeof</span><span style="color: #000000;">(NODE)); pRoot</span>->data =<span style="color: #000000;"> data; pRoot</span>->left = pRoot->right =<span style="color: #000000;"> NULL; pRoot</span>->height = <span style="color: #800080;">0</span><span style="color: #000000;">; } </span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span>(data > pRoot-><span style="color: #000000;">data) { pRoot</span>->right = Insert(pRoot-><span style="color: #000000;">right, data); </span><span style="color: #008000;">//</span><span style="color: #008000;"> 判断是否要旋转</span> <span style="color: #0000ff;">if</span>(Height(pRoot->right) - Height(pRoot->left) == <span style="color: #800080;">2</span><span style="color: #000000;">) { </span><span style="color: #008000;">//</span><span style="color: #008000;"> 判断是右右旋转还是右左旋转</span> <span style="color: #0000ff;">if</span>(data > pRoot->right->data) <span style="color: #008000;">//</span><span style="color: #008000;"> 右右旋转</span> <span style="color: #0000ff;">return</span> SingleRightRotation(pRoot); <span style="color: #008000;">//</span><span style="color: #008000;"> 旋转完了返回修正部分的根节点,并将其返回给上一次调用它的父节点</span> <span style="color: #0000ff;">else</span> <span style="color: #008000;">//</span><span style="color: #008000;"> 右左旋转</span> <span style="color: #0000ff;">return</span><span style="color: #000000;"> DoubleRightLeftRotation(pRoot); } } </span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span>(data < pRoot-><span style="color: #000000;">data) { pRoot</span>->left = Insert(pRoot-><span style="color: #000000;">left, data); </span><span style="color: #0000ff;">if</span>(Height(pRoot->left) - Height(pRoot->right) == <span style="color: #800080;">2</span><span style="color: #000000;">) { </span><span style="color: #0000ff;">if</span>(data < pRoot->left->data) <span style="color: #008000;">//</span><span style="color: #008000;"> 左左旋转</span> <span style="color: #0000ff;">return</span><span style="color: #000000;"> SingleLeftRotation(pRoot); </span><span style="color: #0000ff;">else</span> <span style="color: #008000;">//</span><span style="color: #008000;"> 左右旋转</span> <span style="color: #0000ff;">return</span><span style="color: #000000;"> DoubleLeftRightRotation(pRoot); } } pRoot</span>->height = MaxHeight(pRoot->left, pRoot->right) + <span style="color: #800080;">1</span><span style="color: #000000;">; </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> pRoot; } </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> Height(PNODE pNode) { </span><span style="color: #0000ff;">if</span>(pNode ==<span style="color: #000000;"> NULL) </span><span style="color: #0000ff;">return</span> -<span style="color: #800080;">1</span><span style="color: #000000;">; </span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">return</span> pNode-><span style="color: #000000;">height; } </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> MaxHeight(PNODE pLeft, PNODE pRight) { </span><span style="color: #0000ff;">if</span>(pLeft == NULL && pRight ==<span style="color: #000000;"> NULL) </span><span style="color: #0000ff;">return</span> -<span style="color: #800080;">1</span><span style="color: #000000;">; </span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span>(pLeft ==<span style="color: #000000;"> NULL) </span><span style="color: #0000ff;">return</span> pRight-><span style="color: #000000;">height; </span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span>(pRight ==<span style="color: #000000;"> NULL) </span><span style="color: #0000ff;">return</span> pLeft-><span style="color: #000000;">height; </span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span>(pLeft->height > pRight-><span style="color: #000000;">height) </span><span style="color: #0000ff;">return</span> pLeft-><span style="color: #000000;">height; </span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">return</span> pRight-><span style="color: #000000;">height; } PNODE SingleRightRotation(PNODE A) </span><span style="color: #008000;">//</span><span style="color: #008000;"> 右右旋转,返回旋转部分的根节点</span> <span style="color: #000000;">{ PNODE B </span>= A-><span style="color: #000000;">right; A</span>->right = B-><span style="color: #000000;">left; B</span>->left =<span style="color: #000000;"> A; </span><span style="color: #008000;">/*</span><span style="color: #008000;"> 可以代替下面两行: A->height = Height(A->left) + 1; // 这里没有通过比较更新A的高度,因为我觉得其高度不会变,因为BL不会大于AL(否则在之前它们就不平衡了),更具体的原因可以在我之前的博文"AVL树旋转特性"中找到 B->height = A->height + 1; // 与上述理由类似 </span><span style="color: #008000;">*/</span><span style="color: #000000;"> A</span>->height = MaxHeight(A->left, A->right) + <span style="color: #800080;">1</span><span style="color: #000000;">; B</span>->height = MaxHeight(B->left, A) + <span style="color: #800080;">1</span><span style="color: #000000;">; </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> B; } PNODE DoubleRightLeftRotation(PNODE A) { </span><span style="color: #008000;">//</span><span style="color: #008000;"> 注意到只需要左左旋转然后右右旋转,两者组合起来就是右左旋转</span> A->right = SingleLeftRotation(A->right); <span style="color: #008000;">//</span><span style="color: #008000;"> 以B为根进行左左旋转</span> <span style="color: #0000ff;">return</span> SingleRightRotation(A); <span style="color: #008000;">//</span><span style="color: #008000;"> 以A为根进行右右旋转</span> <span style="color: #000000;">} PNODE SingleLeftRotation(PNODE A) { PNODE B </span>= A-><span style="color: #000000;">left; A</span>->left = B-><span style="color: #000000;">right; B</span>->right =<span style="color: #000000;"> A; A</span>->height = MaxHeight(A->left, A->right) + <span style="color: #800080;">1</span><span style="color: #000000;">; B</span>->height = MaxHeight(B->left, A) + <span style="color: #800080;">1</span><span style="color: #000000;">; </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> B; } PNODE DoubleLeftRightRotation(PNODE A) { A</span>->left = SingleRightRotation(A-><span style="color: #000000;">left); </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> SingleLeftRotation(A); }</span></span></pre> <p> </p> <pre class="code"><span style="font-family: %value; font-size: 14px;">04-树4. Search in a Binary Search Tree (25) To search a key in a binary search tree, we start from the root and move all the way down, choosing branches according to the comparison results of the keys. The searching path corresponds to a sequence of keys. For example, following {1, 4, 2, 3} we can find 3 from a binary search tree with 1 as its root. But {2, 4, 1, 3} is not such a path since 1 is in the right subtree of the root 2, which breaks the rule for a binary search tree. Now given a sequence of keys, you are supposed to tell whether or not it indeed correspnds to a searching path in a binary search tree. Input Specification: Each input file contains one test case. For each case, the first line gives two positive integers N and M (<=100) which are the total number of sequences, and the size of each sequence, respectively. Then N lines follow, each gives a sequence of keys. It is assumed that the keys are numbered from 1 to M. Output Specification: For each sequence, print in a line "YES" if the sequence does correspnd to a searching path in a binary search tree, or "NO" if not. Sample Input: 3 4 1 4 2 3 2 4 1 3 3 2 4 1 Sample Output: YES NO NO </span></pre> <pre class="code"><span style="font-family: %value; font-size: 14px;">/* 思路:按照给定的数据建树,只要当前结点是上一个节点的孩子,则继续这个过程,如果到达输入末尾,则说明路径存在,否则路径不存在,PS:由于这个思路执行起来更快,所以选择了它,其实解决这个问题的时候,我首先想到的办法是:根据输入建树,然后最后输入的那个结点,如果查找的过程中经过了所有结点,那么路径存在,否则路径不存在 */ #include<stdio.h> #include<malloc.h> typedef struct node { int data; struct node * left; struct node * right; }NODE, *PNODE; PNODE Insert(PNODE root, int data); // 返回插入的节点,如果节点已经存在,返回空指针 PNODE Find(PNODE root, int data); void Delete(PNODE root); int main(void) { int flag; // 当前节点是否是上一个节点的孩子的标记变量 PNODE curNode, preNode, root; int i, n, m, data; scanf("%d%d", &n, &m); root = NULL; while(n--) { Delete(root); // 删除上一次建的树 scanf("%d", &data); root = NULL; // 注意一定要置空,否则用上次释放的root做根会出现访问非法的问题 root = Insert(root, data); // 首先建立根节点 preNode = root; flag = 1; for(i = 1; i < m; i++) { scanf("%d", &data); if(flag) { Insert(root, data); curNode = Find(root, data); if(preNode->left != curNode && preNode->right != curNode) flag = 0; preNode = curNode; } } if(flag) printf("YES\n"); else printf("NO\n"); } return 0; } PNODE Insert(PNODE root, int data) { if(root == NULL) { PNODE pNode = (PNODE)malloc(sizeof(NODE)); pNode->data = data; pNode->left = pNode->right = NULL; return pNode; } if(data > root->data) root->right = Insert(root->right, data); // 由于插入的时候需要记住它的父节点,因此这里有赋值语句 else if(data < root->data) root->left = Insert(root->left, data); else // 数据相等的话直接退出,因为二叉搜索树不允许有相同的键值 return NULL; return root; } PNODE Find(PNODE root, int data) { if(root == NULL) return NULL; if(data > root->data) return Find(root->right, data); else if(data < root->data) return Find(root->left, data); else return root; } void Delete(PNODE root) { if(root == NULL) return ; Delete(root->right); // 删除右子树 root->right = NULL; // 注意到这里需要赋值语句用来修改节点的右指针 Delete(root->left); root->left = NULL; free(root); // 释放根节点的空间 } </span></pre> <pre class="code"><span style="font-family: %value; font-size: 14px;">04-树5. Complete Binary Search Tree (30) A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less than the node‘s key. The right subtree of a node contains only nodes with keys greater than or equal to the node‘s key. Both the left and right subtrees must also be binary search trees. A Complete Binary Tree (CBT) is a tree that is completely filled, with the possible exception of the bottom level, which is filled from left to right. Now given a sequence of distinct non-negative integer keys, a unique BST can be constructed if it is required that the tree must also be a CBT. You are supposed to output the level order traversal sequence of this BST. Input Specification: Each input file contains one test case. For each case, the first line contains a positive integer N (<=1000). Then N distinct non-negative integer keys are given in the next line. All the numbers in a line are separated by a space and are no greater than 2000. Output Specification: For each test case, print in one line the level order traversal sequence of the corresponding complete binary search 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. Sample Input: 10 1 2 3 4 5 6 7 8 9 0 Sample Output: 6 3 8 1 5 7 9 0 2 4 </span></pre> <pre class="code"><span style="font-family: %value; font-size: 14px;"><span style="color: #008000;">/*</span><span style="color: #008000;"> 首先,这个题真的很有意思:~ 思路: 把输入数据放入数组冒泡排序 用类似层次遍历的方式建立一颗有N个节点的完全二叉树,每次malloc一个节点num加一 采用中序遍历的方式用一个全局变量将所用的节点标号 层次遍历,根据标号(对应数组下标)放入数值(为了更好的达到练习效果,我直接先序了),同时输出 </span><span style="color: #008000;">*/</span><span style="color: #000000;"> #include</span><stdio.h><span style="color: #000000;"> #include</span><<span style="color: #0000ff;">malloc</span>.h> <span style="color: #0000ff;">#define</span> MAXN 1000 <span style="color: #0000ff;">int</span> Arr[MAXN + <span style="color: #800080;">10</span><span style="color: #000000;">]; </span><span style="color: #0000ff;">int</span> G_NUM; <span style="color: #008000;">//</span><span style="color: #008000;"> 定义成全局变量方便创建完全二叉树</span> <span style="color: #0000ff;">int</span> G_COUNT = <span style="color: #800080;">0</span>; <span style="color: #008000;">//</span><span style="color: #008000;"> 记录已经创建的树的节点的个数</span> <span style="color: #0000ff;">int</span> G_NUMBER = <span style="color: #800080;">0</span>; <span style="color: #008000;">//</span><span style="color: #008000;"> 节点标号,左-中-右依次递增 </span> <span style="color: #0000ff;">void</span> BbSort(<span style="color: #0000ff;">int</span> Arr[], <span style="color: #0000ff;">int</span><span style="color: #000000;"> n); typedef </span><span style="color: #0000ff;">struct</span><span style="color: #000000;"> TNode { </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> data; </span><span style="color: #0000ff;">struct</span> TNode *<span style="color: #000000;"> left; </span><span style="color: #0000ff;">struct</span> TNode *<span style="color: #000000;"> right; }TNODE, </span>*<span style="color: #000000;">PTNODE; typedef </span><span style="color: #0000ff;">struct</span><span style="color: #000000;"> QNode { PTNODE data; </span><span style="color: #0000ff;">struct</span> QNode *<span style="color: #000000;"> next; }QNODE, </span>*<span style="color: #000000;">PQNODE; </span><span style="color: #008000;">//</span><span style="color: #008000;"> 树操作</span> <span style="color: #000000;">PTNODE CreateCBT(); </span><span style="color: #0000ff;">void</span> LevelOrderTraversal(PTNODE pRoot); <span style="color: #008000;">//</span><span style="color: #008000;"> 用层次遍历的方式建立完全二叉树</span> <span style="color: #0000ff;">void</span> InOrderTraversal(PTNODE pRoot); <span style="color: #008000;">//</span><span style="color: #008000;"> 用中序遍历的方式给节点编号</span> <span style="color: #0000ff;">void</span> PreOrderTraversal(PTNODE pRoot); <span style="color: #008000;">//</span><span style="color: #008000;"> 用先序遍历的方式给节点赋值</span> <span style="color: #0000ff;">void</span> LevelOrderTraversal2(PTNODE pRoot); <span style="color: #008000;">//</span><span style="color: #008000;"> 输出层次遍历结果</span> typedef <span style="color: #0000ff;">struct</span><span style="color: #000000;"> Queue { PQNODE front; PQNODE rear; }QUEUE, </span>*<span style="color: #000000;">PQUEUE; </span><span style="color: #008000;">//</span><span style="color: #008000;"> 队列操作</span> <span style="color: #000000;">PQUEUE CreatQueue(); </span><span style="color: #0000ff;">void</span><span style="color: #000000;"> EnQueue(PQUEUE pQueue, PTNODE pTNode); PTNODE DeQueue(PQUEUE pQueue); </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> IsEmpty(PQUEUE pQueue); </span><span style="color: #0000ff;">int</span> main(<span style="color: #0000ff;">void</span><span style="color: #000000;">) { </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> i; PTNODE pRoot; scanf(</span><span style="color: #800000;">"</span><span style="color: #800000;">%d</span><span style="color: #800000;">"</span>, &<span style="color: #000000;">G_NUM); </span><span style="color: #0000ff;">for</span>(i = <span style="color: #800080;">0</span>; i < G_NUM; i++<span style="color: #000000;">) scanf(</span><span style="color: #800000;">"</span><span style="color: #800000;">%d</span><span style="color: #800000;">"</span>, &<span style="color: #000000;">Arr[i]); BbSort(Arr, G_NUM); pRoot </span>=<span style="color: #000000;"> CreateCBT(); LevelOrderTraversal(pRoot); </span><span style="color: #008000;">//</span><span style="color: #008000;"> 层次遍历的方式建立完全二叉树</span> <span style="color: #000000;"> InOrderTraversal(pRoot); </span><span style="color: #008000;">//</span><span style="color: #008000;"> 中序遍历标号 </span><span style="color: #008000;">//</span><span style="color: #008000;"> 下面两行代码其实可以用一个层次遍历来代替, 就是给节点赋值的同时输出该值,当然按照AC的角度理解的话,可以不用赋值直接输出</span> PreOrderTraversal(pRoot); <span style="color: #008000;">//</span><span style="color: #008000;"> 先序遍历给节点赋值</span> <span style="color: #000000;"> LevelOrderTraversal2(pRoot); </span><span style="color: #008000;">//</span><span style="color: #008000;"> 层次遍历的同时输出节点的值</span> <span style="color: #0000ff;">return</span> <span style="color: #800080;">0</span><span style="color: #000000;">; } </span><span style="color: #0000ff;">void</span> BbSort(<span style="color: #0000ff;">int</span> Arr[], <span style="color: #0000ff;">int</span><span style="color: #000000;"> n) { </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> temp, outer, inner, flag; flag </span>= <span style="color: #800080;">1</span><span style="color: #000000;">; </span><span style="color: #0000ff;">for</span>(outer = n-<span style="color: #800080;">1</span>; outer > <span style="color: #800080;">0</span> && flag; outer--<span style="color: #000000;">) { flag </span>= <span style="color: #800080;">0</span><span style="color: #000000;">; </span><span style="color: #0000ff;">for</span>(inner = <span style="color: #800080;">0</span>; inner < outer; inner++<span style="color: #000000;">) { </span><span style="color: #0000ff;">if</span>(Arr[inner] > Arr[inner+<span style="color: #800080;">1</span><span style="color: #000000;">]) { temp </span>=<span style="color: #000000;"> Arr[inner]; Arr[inner] </span>= Arr[inner+<span style="color: #800080;">1</span><span style="color: #000000;">]; Arr[inner</span>+<span style="color: #800080;">1</span>] =<span style="color: #000000;"> temp; flag </span>= <span style="color: #800080;">1</span><span style="color: #000000;">; } } } } PTNODE CreateCBT() { </span><span style="color: #0000ff;">if</span>(G_COUNT ==<span style="color: #000000;"> G_NUM) </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> NULL; PTNODE root </span>= (PTNODE)<span style="color: #0000ff;">malloc</span>(<span style="color: #0000ff;">sizeof</span><span style="color: #000000;">(TNODE)); root</span>->left = root->right =<span style="color: #000000;"> NULL; G_COUNT</span>++<span style="color: #000000;">; </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> root; } </span><span style="color: #0000ff;">void</span><span style="color: #000000;"> LevelOrderTraversal(PTNODE pRoot) { PTNODE pTNode; PQUEUE pQueue </span>=<span style="color: #000000;"> CreatQueue(); EnQueue(pQueue, pRoot); </span><span style="color: #0000ff;">while</span>(!<span style="color: #000000;">IsEmpty(pQueue)) { pTNode </span>=<span style="color: #000000;"> DeQueue(pQueue); pTNode</span>->left =<span style="color: #000000;"> CreateCBT(); pTNode</span>->right =<span style="color: #000000;"> CreateCBT(); EnQueue(pQueue, pTNode</span>-><span style="color: #000000;">left); EnQueue(pQueue, pTNode</span>-><span style="color: #000000;">right); } } </span><span style="color: #0000ff;">void</span><span style="color: #000000;"> LevelOrderTraversal2(PTNODE pRoot) { </span><span style="color: #0000ff;">int</span> flag = <span style="color: #800080;">0</span><span style="color: #000000;">; PTNODE pTNode; PQUEUE pQueue </span>=<span style="color: #000000;"> CreatQueue(); EnQueue(pQueue, pRoot); </span><span style="color: #0000ff;">while</span>(!<span style="color: #000000;">IsEmpty(pQueue)) { pTNode </span>=<span style="color: #000000;"> DeQueue(pQueue); </span><span style="color: #0000ff;">if</span><span style="color: #000000;">(flag) putchar(</span><span style="color: #800000;">‘</span> <span style="color: #800000;">‘</span><span style="color: #000000;">); </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> flag </span>= <span style="color: #800080;">1</span><span style="color: #000000;">; printf(</span><span style="color: #800000;">"</span><span style="color: #800000;">%d</span><span style="color: #800000;">"</span>, pTNode-><span style="color: #000000;">data); EnQueue(pQueue, pTNode</span>-><span style="color: #000000;">left); EnQueue(pQueue, pTNode</span>-><span style="color: #000000;">right); } } </span><span style="color: #0000ff;">void</span><span style="color: #000000;"> InOrderTraversal(PTNODE pRoot) { </span><span style="color: #0000ff;">if</span><span style="color: #000000;">(pRoot) { InOrderTraversal(pRoot</span>-><span style="color: #000000;">left); pRoot</span>->data =<span style="color: #000000;"> G_NUMBER; G_NUMBER</span>++<span style="color: #000000;">; InOrderTraversal(pRoot</span>-><span style="color: #000000;">right); } } </span><span style="color: #0000ff;">void</span><span style="color: #000000;"> PreOrderTraversal(PTNODE pRoot) { </span><span style="color: #0000ff;">if</span><span style="color: #000000;">(pRoot) { pRoot</span>->data = Arr[pRoot-><span style="color: #000000;">data]; PreOrderTraversal(pRoot</span>-><span style="color: #000000;">left); PreOrderTraversal(pRoot</span>-><span style="color: #000000;">right); } } PQUEUE CreatQueue() { PQUEUE pQ </span>= (PQUEUE)<span style="color: #0000ff;">malloc</span>(<span style="color: #0000ff;">sizeof</span><span style="color: #000000;">(QUEUE)); pQ</span>->front = pQ->rear =<span style="color: #000000;"> NULL; </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> pQ; } </span><span style="color: #0000ff;">void</span><span style="color: #000000;"> EnQueue(PQUEUE pQueue, PTNODE pTNode) { </span><span style="color: #0000ff;">if</span>(pTNode ==<span style="color: #000000;"> NULL) </span><span style="color: #0000ff;">return</span><span style="color: #000000;">; PQNODE pQNode</span>= (PQNODE)<span style="color: #0000ff;">malloc</span>(<span style="color: #0000ff;">sizeof</span><span style="color: #000000;">(QNODE)); pQNode</span>->data =<span style="color: #000000;"> pTNode; pQNode</span>->next =<span style="color: #000000;"> NULL; </span><span style="color: #0000ff;">if</span>(pQueue->front ==<span style="color: #000000;"> NULL) pQueue</span>->front = pQueue->rear =<span style="color: #000000;"> pQNode; </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> { pQueue</span>->rear->next =<span style="color: #000000;"> pQNode; pQueue</span>->rear =<span style="color: #000000;"> pQNode; } } PTNODE DeQueue(PQUEUE pQueue) { PTNODE pTNode; </span><span style="color: #0000ff;">if</span>(pQueue->front ==<span style="color: #000000;"> NULL) </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> NULL; </span><span style="color: #0000ff;">if</span>(pQueue->rear == pQueue-><span style="color: #000000;">front) pQueue</span>->rear =<span style="color: #000000;"> NULL; PQNODE pQNode </span>= pQueue-><span style="color: #000000;">front; pQueue</span>->front = pQueue->front-><span style="color: #000000;">next; pTNode </span>= pQNode-><span style="color: #000000;">data; </span><span style="color: #0000ff;">free</span><span style="color: #000000;">(pQNode); </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> pTNode; } </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> IsEmpty(PQUEUE pQueue) { </span><span style="color: #0000ff;">if</span>(pQueue->front ==<span style="color: #000000;"> NULL) </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">1</span><span style="color: #000000;">; </span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">return</span> <span style="color: #800080;">0</span><span style="color: #000000;">; }</span></span></pre> <p> </p> <pre class="code"><span style="font-family: %value; font-size: 14px;">05-树6. Path in a Heap (25) Insert a sequence of given numbers into an initially empty min-heap H. Then for any given index i, you are supposed to print the path from H[i] to the root. Input Specification: Each input file contains one test case. For each case, the first line gives two positive integers N and M (<=1000) which are the size of the input sequence, and the number of indices to be checked, respectively. Given in the next line are the N integers in [-10000, 10000] which are supposed to be inserted into an initially empty min-heap. Finally in the last line, M indices are given. Output Specification: For each index i in the input, print in one line the numbers visited along the path from H[i] to the root of the heap. The numbers are separated by a space, and there must be no extra space at the end of the line. Sample Input: 5 3 46 23 26 24 10 5 4 3 Sample Output: 24 23 10 46 23 10 26 10 </span></pre> <pre class="code"><span style="font-family: %value; font-size: 14px;">#include<stdio.h> <span style="color: #008000;">//</span><span style="color: #008000;"> 堆常见的操作有Creat、Insert、Delete、IsFull、IsEmpty这里只关心AC,因此省略了不相干操作的实现</span> #include<<span style="color: #0000ff;">malloc</span>.h> <span style="color: #0000ff;">#define</span> CAPACITY 1000 <span style="color: #008000;">//</span><span style="color: #008000;"> 堆的最大容量</span> <span style="color: #0000ff;">#define</span> MIN -10001 <span style="color: #008000;">//</span><span style="color: #008000;"> 堆数组中下标为0的元素-哨兵的应赋予的初值</span><span style="color: #000000;"> typedef </span><span style="color: #0000ff;">struct</span><span style="color: #000000;"> Heap { </span><span style="color: #0000ff;">int</span> *<span style="color: #000000;"> arr; </span><span style="color: #0000ff;">int</span> size; <span style="color: #008000;">//</span><span style="color: #008000;"> 当前元素的个数</span> <span style="color: #0000ff;">int</span> capacity; <span style="color: #008000;">//</span><span style="color: #008000;"> 最大容量</span> }HEAP, *<span style="color: #000000;">PHEAP; PHEAP CreateHeap(</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> capacity); </span><span style="color: #0000ff;">void</span> Insert(PHEAP pHeap, <span style="color: #0000ff;">int</span><span style="color: #000000;"> data); </span><span style="color: #0000ff;">void</span> PrintPath(PHEAP pHeap, <span style="color: #0000ff;">int</span> i); <span style="color: #008000;">//</span><span style="color: #008000;"> 从下标为i的位置开始打印其父节点的值,直到到达数根为止</span> <span style="color: #0000ff;">int</span> main(<span style="color: #0000ff;">void</span><span style="color: #000000;">) { </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> n, m, data, i; PHEAP pHeap; scanf(</span><span style="color: #800000;">"</span><span style="color: #800000;">%d%d</span><span style="color: #800000;">"</span>, &n, &<span style="color: #000000;">m); pHeap </span>=<span style="color: #000000;"> CreateHeap(CAPACITY); </span><span style="color: #0000ff;">while</span>(n--<span style="color: #000000;">) { scanf(</span><span style="color: #800000;">"</span><span style="color: #800000;">%d</span><span style="color: #800000;">"</span>, &<span style="color: #000000;">data); Insert(pHeap, data); } </span><span style="color: #0000ff;">while</span>(m--<span style="color: #000000;">) { scanf(</span><span style="color: #800000;">"</span><span style="color: #800000;">%d</span><span style="color: #800000;">"</span>, &<span style="color: #000000;">i); PrintPath(pHeap, i); printf(</span><span style="color: #800000;">"</span><span style="color: #800000;">\n</span><span style="color: #800000;">"</span><span style="color: #000000;">); } </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">0</span><span style="color: #000000;">; } PHEAP CreateHeap(</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> capacity) { PHEAP pHeap </span>= (PHEAP)<span style="color: #0000ff;">malloc</span>(<span style="color: #0000ff;">sizeof</span><span style="color: #000000;">(HEAP)); pHeap</span>->arr = (<span style="color: #0000ff;">int</span> *)<span style="color: #0000ff;">malloc</span>(<span style="color: #0000ff;">sizeof</span>(<span style="color: #0000ff;">int</span>) * (capacity+<span style="color: #800080;">1</span><span style="color: #000000;">)); pHeap</span>->arr[<span style="color: #800080;">0</span>] = MIN; <span style="color: #008000;">//</span><span style="color: #008000;"> arr[0]做哨兵,方便查找操作</span> pHeap->size = <span style="color: #800080;">0</span><span style="color: #000000;">; pHeap</span>->capacity =<span style="color: #000000;">capacity; </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> pHeap; } </span><span style="color: #0000ff;">void</span> Insert(PHEAP pHeap, <span style="color: #0000ff;">int</span><span style="color: #000000;"> data) { </span><span style="color: #0000ff;">int</span> position = ++pHeap->size; <span style="color: #008000;">//</span><span style="color: #008000;"> 应该先判堆是否满的,因为测试数据不会导致堆满,因此判断堆是否满的操作省略了</span> <span style="color: #0000ff;">for</span>(; data < pHeap->arr[position/<span style="color: #800080;">2</span>]; position /= <span style="color: #800080;">2</span><span style="color: #000000;">) pHeap</span>->arr[position] = pHeap->arr[position/<span style="color: #800080;">2</span>]; <span style="color: #008000;">//</span><span style="color: #008000;"> 向下(数根)过滤节点</span> <span style="color: #000000;"> pHeap</span>->arr[position] =<span style="color: #000000;"> data; } </span><span style="color: #0000ff;">void</span> PrintPath(PHEAP pHeap, <span style="color: #0000ff;">int</span> i) <span style="color: #008000;">//</span><span style="color: #008000;"> 从下标为i的位置开始打印其父节点的值,直到到达数根为止</span> <span style="color: #000000;">{ </span><span style="color: #0000ff;">int</span> isFirst = <span style="color: #800080;">1</span><span style="color: #000000;">; </span><span style="color: #0000ff;">while</span><span style="color: #000000;">(i) { </span><span style="color: #0000ff;">if</span><span style="color: #000000;">(isFirst) isFirst </span>= <span style="color: #800080;">0</span><span style="color: #000000;">; </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> printf(</span><span style="color: #800000;">"</span> <span style="color: #800000;">"</span><span style="color: #000000;">); printf(</span><span style="color: #800000;">"</span><span style="color: #800000;">%d</span><span style="color: #800000;">"</span>, pHeap-><span style="color: #000000;">arr[i]); i </span>/= <span style="color: #800080;">2</span><span style="color: #000000;">; } }</span></span></pre> <pre class="code"><span style="font-family: %value; font-size: 14px;">05-树7. File Transfer (25) We have a network of computers and a list of bi-directional connections. Each of these connections allows a file transfer from one computer to another. Is it possible to send a file from any computer on the network to any other? Input Specification: Each input file contains one test case. For each test case, the first line contains N (2<=N<=104), the total number of computers in a network. Each computer in the network is then represented by a positive integer between 1 and N. Then in the following lines, the input is given in the format: I c1 c2 where I stands for inputting a connection between c1 and c2; or C c1 c2 where C stands for checking if it is possible to transfer files between c1 and c2; or S where S stands for stopping this case. Output Specification: For each C case, print in one line the word "yes" or "no" if it is possible or impossible to transfer files between c1 and c2, respectively. At the end of each case, print in one line "The network is connected." if there is a path between any pair of computers; or "There are k components." where k is the number of connected components in this network. Sample Input 1: 5 C 3 2 I 3 2 C 1 5 I 4 5 I 2 4 C 3 5 S Sample Output 1: no no yes There are 2 components. Sample Input 2: 5 C 3 2 I 3 2 C 1 5 I 4 5 I 2 4 C 3 5 I 1 3 C 1 5 S Sample Output 2: no no yes yes The network is connected. </span></pre> <pre class="code"><span style="font-family: %value; font-size: 14px;"><span style="color: #008000;">/*</span><span style="color: #008000;"> 写的这个代码有点自注释的感觉了,因此不写思路,它们都藏在我的代码里:~ </span><span style="color: #008000;">*/</span><span style="color: #000000;"> #include</span><stdio.h> <span style="color: #008000;">//</span><span style="color: #008000;"> 经测试,路径压缩和高度差合并两个任取一个实现之就能AC,但是毫无疑问,两者皆实现能够保证最快的速度因而更保险</span> #include<<span style="color: #0000ff;">string</span>.h> <span style="color: #0000ff;">#define</span> MAXN 10000<span style="color: #000000;"> typedef </span><span style="color: #0000ff;">struct</span> <span style="color: #0000ff;">set</span><span style="color: #000000;"> { </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> data; </span><span style="color: #0000ff;">int</span> parent; <span style="color: #008000;">//</span><span style="color: #008000;"> 采用数组下标的方式指示它的父节点</span> <span style="color: #000000;">}SET; SET </span><span style="color: #0000ff;">set</span>[MAXN+<span style="color: #800080;">10</span>]; <span style="color: #008000;">//</span><span style="color: #008000;"> 方便起见,0号单元空着了,每个集合的根节点的parent置为一个负数</span> <span style="color: #0000ff;">int</span> Find(SET <span style="color: #0000ff;">set</span>[], <span style="color: #0000ff;">int</span> element); <span style="color: #008000;">//</span><span style="color: #008000;"> 查找元素属于哪个集合,返回元素所在集合的根,未找到则返回-1</span> <span style="color: #0000ff;">void</span> Union(SET <span style="color: #0000ff;">set</span>[], <span style="color: #0000ff;">int</span> element1, <span style="color: #0000ff;">int</span> element2); <span style="color: #008000;">//</span><span style="color: #008000;"> 将两个元素合并到一个集合中,方便起见,元素值放到对应下标中</span> <span style="color: #0000ff;">int</span> SetCount(SET <span style="color: #0000ff;">set</span>[], <span style="color: #0000ff;">int</span> n); <span style="color: #008000;">//</span><span style="color: #008000;"> 统计n个元素构成的集合总数</span> <span style="color: #0000ff;">int</span> main(<span style="color: #0000ff;">void</span><span style="color: #000000;">) { </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> n, element1, element2, setCount; </span><span style="color: #0000ff;">char</span><span style="color: #000000;"> ch; memset(</span><span style="color: #0000ff;">set</span>, <span style="color: #800080;">0</span>, <span style="color: #0000ff;">sizeof</span>(<span style="color: #0000ff;">set</span><span style="color: #000000;">)); scanf(</span><span style="color: #800000;">"</span><span style="color: #800000;">%d</span><span style="color: #800000;">"</span>, &<span style="color: #000000;">n); </span><span style="color: #0000ff;">while</span>(scanf(<span style="color: #800000;">"</span><span style="color: #800000;"> %c</span><span style="color: #800000;">"</span>, &ch) && ch != <span style="color: #800000;">‘</span><span style="color: #800000;">S</span><span style="color: #800000;">‘</span><span style="color: #000000;">) { scanf(</span><span style="color: #800000;">"</span><span style="color: #800000;">%d%d</span><span style="color: #800000;">"</span>, &element1, &<span style="color: #000000;">element2); </span><span style="color: #0000ff;">if</span>(ch == <span style="color: #800000;">‘</span><span style="color: #800000;">C</span><span style="color: #800000;">‘</span><span style="color: #000000;">) { </span><span style="color: #0000ff;">if</span>(Find(<span style="color: #0000ff;">set</span>, element1) != -<span style="color: #800080;">1</span> && Find(<span style="color: #0000ff;">set</span>, element1) == Find(<span style="color: #0000ff;">set</span><span style="color: #000000;">, element2)) printf(</span><span style="color: #800000;">"</span><span style="color: #800000;">yes\n</span><span style="color: #800000;">"</span><span style="color: #000000;">); </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> printf(</span><span style="color: #800000;">"</span><span style="color: #800000;">no\n</span><span style="color: #800000;">"</span><span style="color: #000000;">); } </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> Union(</span><span style="color: #0000ff;">set</span><span style="color: #000000;">, element1, element2); } setCount </span>= SetCount(<span style="color: #0000ff;">set</span><span style="color: #000000;">, n); </span><span style="color: #0000ff;">if</span>(setCount == <span style="color: #800080;">1</span><span style="color: #000000;">) printf(</span><span style="color: #800000;">"</span><span style="color: #800000;">The network is connected.\n</span><span style="color: #800000;">"</span><span style="color: #000000;">); </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> printf(</span><span style="color: #800000;">"</span><span style="color: #800000;">There are %d components.\n</span><span style="color: #800000;">"</span><span style="color: #000000;">, setCount); </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">0</span><span style="color: #000000;">; } </span><span style="color: #0000ff;">int</span> Find(SET <span style="color: #0000ff;">set</span>[], <span style="color: #0000ff;">int</span><span style="color: #000000;"> element) { </span><span style="color: #0000ff;">int</span> root =<span style="color: #000000;"> element; </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> higher; </span><span style="color: #0000ff;">if</span>(<span style="color: #0000ff;">set</span>[element].data != element) <span style="color: #008000;">//</span><span style="color: #008000;"> 因为set数组初始为0,而给定的最小输入是2,因此元素不在集合中的话,这个条件成立</span> <span style="color: #0000ff;">return</span> -<span style="color: #800080;">1</span><span style="color: #000000;">; </span><span style="color: #008000;">//</span><span style="color: #008000;"> for(; set[element].parent > 0; element = set[element].parent) </span><span style="color: #008000;">//</span><span style="color: #008000;"> 没有路径压缩,直接超时 </span><span style="color: #008000;">//</span><span style="color: #008000;"> ; </span><span style="color: #008000;">//</span><span style="color: #008000;"> return element;</span> <span style="color: #0000ff;">for</span>(; <span style="color: #0000ff;">set</span>[root].parent > <span style="color: #800080;">0</span>; root = <span style="color: #0000ff;">set</span><span style="color: #000000;">[root].parent) ; </span><span style="color: #0000ff;">while</span>(<span style="color: #0000ff;">set</span>[element].parent > <span style="color: #800080;">0</span><span style="color: #000000;">) { higher </span>= <span style="color: #0000ff;">set</span><span style="color: #000000;">[element].parent; </span><span style="color: #0000ff;">set</span>[element].parent =<span style="color: #000000;"> root; element </span>=<span style="color: #000000;"> higher; } </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> root; } </span><span style="color: #0000ff;">void</span> Union(SET <span style="color: #0000ff;">set</span>[], <span style="color: #0000ff;">int</span> element1, <span style="color: #0000ff;">int</span> element2) <span style="color: #008000;">//</span><span style="color: #008000;"> 高度小的树并到高度大的树上,|parent|代表树的高度(以集合元素个数作为度量标准)</span> <span style="color: #000000;">{ </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> root1, root2; </span><span style="color: #0000ff;">if</span>(Find(<span style="color: #0000ff;">set</span>, element1) == -<span style="color: #800080;">1</span>) <span style="color: #008000;">//</span><span style="color: #008000;"> 如果元素不在集合中,则将其单独构成一个只包含其本身的集合</span> <span style="color: #000000;"> { </span><span style="color: #0000ff;">set</span>[element1].data =<span style="color: #000000;"> element1; </span><span style="color: #0000ff;">set</span>[element1].parent = -<span style="color: #800080;">1</span><span style="color: #000000;">; } </span><span style="color: #0000ff;">if</span>(Find(<span style="color: #0000ff;">set</span>, element2) == -<span style="color: #800080;">1</span><span style="color: #000000;">) { </span><span style="color: #0000ff;">set</span>[element2].data =<span style="color: #000000;"> element2; </span><span style="color: #0000ff;">set</span>[element2].parent = -<span style="color: #800080;">1</span><span style="color: #000000;">; } root1 </span>= Find(<span style="color: #0000ff;">set</span><span style="color: #000000;">, element1); root2 </span>= Find(<span style="color: #0000ff;">set</span><span style="color: #000000;">, element2); </span><span style="color: #0000ff;">if</span>(root1 !=<span style="color: #000000;"> root2) { </span><span style="color: #0000ff;">if</span>(<span style="color: #0000ff;">set</span>[root1].parent < <span style="color: #0000ff;">set</span>[root2].parent) <span style="color: #008000;">//</span><span style="color: #008000;"> 如果root1的高度高,就把root2挂到root1上,同时修改root1的高度</span> <span style="color: #000000;"> { </span><span style="color: #0000ff;">set</span>[root1].parent += <span style="color: #0000ff;">set</span><span style="color: #000000;">[root2].parent; </span><span style="color: #0000ff;">set</span>[root2].parent =<span style="color: #000000;"> root1; } </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> { </span><span style="color: #0000ff;">set</span>[root2].parent += <span style="color: #0000ff;">set</span><span style="color: #000000;">[root1].parent; </span><span style="color: #0000ff;">set</span>[root1].parent =<span style="color: #000000;"> root2; } </span><span style="color: #008000;">//</span><span style="color: #008000;"> set[root2].parent = root1; </span><span style="color: #008000;">//</span><span style="color: #008000;">没有高度控制的代码,在if(root1 != root2)里仅此一行</span> <span style="color: #000000;"> } </span><span style="color: #0000ff;">return</span><span style="color: #000000;">; } </span><span style="color: #0000ff;">int</span> SetCount(SET <span style="color: #0000ff;">set</span>[], <span style="color: #0000ff;">int</span><span style="color: #000000;"> n) { </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> i, count; count </span>= <span style="color: #800080;">0</span><span style="color: #000000;">; </span><span style="color: #0000ff;">for</span>(i = <span style="color: #800080;">1</span>; i <= n; i++<span style="color: #000000;">) { </span><span style="color: #0000ff;">if</span>(<span style="color: #0000ff;">set</span>[i].parent <= <span style="color: #800080;">0</span>) <span style="color: #008000;">//</span><span style="color: #008000;"> 小于零的元素代表添加到集合中的一个根,等于零的元素代表未添加到集合中的元素,它独自构成一个集合</span> count++<span style="color: #000000;">; } </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> count; }</span></span></pre> <p> </p> <p>(XP_JIANG).</p>PAT树_层序遍历叶节点、中序建树后序输出、AVL树的根、二叉树路径存在性判定、奇妙的完全二叉搜索树、最小堆路径、文件路由
标签:
原文地址:http://www.cnblogs.com/xpjiang/p/4580351.html