Xem Nhiều 2/2023 #️ Drawing Sentence Syntax Trees – Amy Reynolds # Top 9 Trend | Maiphuongus.net

Xem Nhiều 2/2023 # Drawing Sentence Syntax Trees – Amy Reynolds # Top 9 Trend

Cập nhật thông tin chi tiết về Drawing Sentence Syntax Trees – Amy Reynolds mới nhất trên website Maiphuongus.net. Hy vọng nội dung bài viết sẽ đáp ứng được nhu cầu của bạn, chúng tôi sẽ thường xuyên cập nhật mới nội dung để bạn nhận được thông tin nhanh chóng và chính xác nhất.

Now that you’ve learned about X-bar structure and determining constituency, you should be able to draw syntax trees. However, there are all sorts of different types of phrases and ways that they can connect, and you have a sentence you need to draw a tree for. What to do!? This page is designed to help guide you through drawing syntactic trees.

We will walk through how to make trees for the following sentences:

Amy bakes pies. 

Amy bakes pies in the summer.

Amy bakes pies for her friends.

Amy thinks that she will bake pies.

Step 1: The IP and CP phrases

As noted above, we automatically expect the head of the Inflection Phrase (IP) to contain the overall tense of the clause, denoted here by the (+/-) PAST feature. Other words that can appear in the head position of the IP include modal (e.g. could, should, would, might, etc.) auxiliaries. However, regardless of whether there is an actual word within the sentence, you should always show the complete IP structure, especially including I.

Step 2: Adding the Subject and Predicate

Step 3: Add other specifiers, complements and modifiers to the phrases

Now that we have the subject and predicate inserted into the structure, it is important to next consider what happens to the rest of the words and phrases left in the sentence. For instance, for the sentence Amy bakes pies, we have Amy and bakes covered, but how does pies attach to the sentence?

We would say that in the summer is a modifier of the VP. Why? Here, it is not adding additional information about the pies, instead, it is telling us when the baking is taking place. We specifically know that it must be a modifier rather than a complement, because the verb does not require that additional information about the baking — not like it requires the direct object pies. Since in the summer is a modifier of the VP, we add another higher up V’ node, so that it can be a sister to the lower V’. Hence, we get the following structure:

Now if we changed the sentence to Amy bakes pies for her friends, should the PP for her friends be in the same location? There are two possibilities here: either Amy is baking for her friends, and what she is baking is pies; or there are pies for her friends that Amy is baking. In the first instance, it is the action (baking), that is being modified, and for her friends in that instance would be a modifier of the VP, as in the summer was in the structure above. In the second instance, the object that Amy is baking is pies for her friends. If that were the case,  for her friends would not be a modifier of the VP and instead would be a modifier of the NP, as shown in the structure below:

The slight differences in meaning between the two possible structures of the same sentence can be captured if we think about corresponding questions that could be asked. If you asked What does Amy bake for her friends? (where the PP is modifying the VP), an appropriate answer could be Pies, not cupcakes, where you are answering simply with nouns, no additional phrases added (because nothing else is branching from that NP). On the other hand, if you asked What does Amy bake? for this sentence, an appropriate answer could be Pies for her friends, not cupcakes for her family, which shows that the prepositional phrases are acting as modifiers distinguishing who the objects in question are for, not who she is baking for. In that instance, the PP would be a modifier of the NP, not the VP.

Step 4: Add CPs if there are any

Within your sentence, there may be multiple clauses. If that is the case, then you can expect a Complementizer Phrase to show up. The basic structure for a CP that occurs lower in the sentences’ tree is exactly like that CP that contains the entire sentence, described above. There are two types of CPs that can occur within a larger CP phrase: CPs for complement clauses, and CPs for relative clauses. Depending on the CP type, it will attach to the larger sentence in different ways. Because CPs for relative clauses show movement, they will be covered in the Drawing Question Syntax Trees. For now, we are going to cover the CPs that are complement clauses. How these in particular attach to the larger tree should be easy to remember: the CP for a complement clause should always occur as a complement to the phrase it is attaching to. Let’s consider the sentence Amy thinks that she will bake her pies. We already know the basic structure for Amy thinks. What about that she will bake her pies? This is what we call a complement clause, which contains a ‘mini-sentence’ of sorts — this same clause could stand alone as the sentence She will bake her pies.

Notice that the structure of that she will bake her pies is exactly like that of what we would make for the sentence she will bake her pies, except that the word that introduces the complement clause (that) occupies the head position of the CP. Overall, the entire CP attaches as a complement to the VP contained within the higher CP. In fact, it may be handy to remember that in English, at least, a complement clause will always attach to a VP as a complement — complement clauses do not attach to NPs. 

Now that you understand how to draw syntax trees for sentences, you are ready to learn how to draw trees for questions, as well.

How To Draw A Syntax Tree, Part 8: A Step

I’ve talked previously about all the different possibilities for what constitutes a syntax tree, but not about how you get from “here’s how you’re supposed to represent things” to actually drawing a tree yourself. And that’s a pity, because I’ve actually got a really nice series of steps that I’ve developed from teaching, TAing, and tutoring.

Here are the steps in a nutshell: I’m going to go through them one at a time.

1. Label parts of speech2. Label main constituentsStep 1: Label parts of speech 3. Draw tree

Note: I recommend doing all of these steps by hand if at all possible. You’ll see from the photos and gifs below that it was easier for me to break out the paper and markers than to try to represent this electronically. If you want to type up your tree later, here are some tools for doing so, but it’ll be much easier to type up from an existing handwritten draft rather than try to do the thinking and the typesetting as part of the same step. Pencil or even a chalkboard/whiteboard is often a good idea for tree-drafting, and you’ll get better at figuring out spacing as you practice more.

So what do these steps look like in practice? Note that rather like learning to ride a bicycle, the extended description looks long and complicated, but with practice many of the steps become automatic with time.

You’re going to start with your sentence just written out normally – feel free to use the version that you get printed on your test or assignment, or write it out for yourself on scrap paper. If you’re writing it out yourself, it’s a good idea to leave extra space between the words, for the sake of the next step.

One easy way to keep track of parts of speech is to learn a keyword or two that unambiguously belongs in each category, and then when you’re trying to figure out whether another word belongs to that category, you can say “could I replace it with this other word that I know the lexical category of?” Here’s a sample list:

Noun – cat, happinessVerb – see, laughAdjective – blue, happyAdverb – quicklyPreposition – inDeterminer – theComplementizer – if, whetherAuxiliary – couldDegree word – very, nearly

Step 2: Label main constituents

(Note: do not use “that” as a keyword, because it can be a determiner, as in “that cat”, or a complementizer, as in “I saw that the cat was purring”. Compare what happens when you substitute them: “the cat” but not “*if cat”, “*whether cat”, and “I saw whether the cat was purring” but not “*I saw the the cat was purring”. “Be” and “have” are sometimes verbs as in “I am/have a cat”, sometimes auxiliaries, as in “I was laughing/have laughed” and your course may have particular conventions distinguishing them from modal auxiliaries like can, could, may, might, shall, should, will, would.)

In general, this step is going to end up with essentially the same results regardless of what theory your course is using. Apart from the auxiliaries and degree words/modifiers things, the only other part where I’d expect variation is depending on how your course treats pronouns and maybe proper names, so just check whether you’re using NP, N, D, etc for them.

Why are we doing this step? Rather than rushing into drawing a tree right away, this step will make sure you get all the labels for each node correct and don’t forget anything. Move on to step 2 once all words are labelled.

Here’s a image of step 1: I labeled the words starting with the more obvious content words and then adding in the less obvious function words, but you can do it in any order that makes sense.

Once you’ve gotten everything labelled, you want to figure out the constituents and the main relationships between various parts of the sentence. Your goal for this step is to get every head inside its own phrase: for every N, you need to figure out what its NP is, for every V, you figure out its VP, etc. If you’re doing bar levels, you might label a couple of those at this step if you’ve got ambiguity or a whole lot of stuff inside a particular phrase, but for the routine bar levels you can just put them in at the next step.

BOTTOM-UP: Start by identifying the easy phrases that only have a word or two in them: AdjPs, AdvPs, and NPs are often fairly small. Then move on to slightly larger constituents, including PPs and more elaborate NPs (and DPs if you’re using them), then VPs and finally IPs and CPs, if necessary.

TOP-DOWN: Start by identifying the verb(s): if you’re drawing a sentence, there should be at least one of them.

What did YouTube do? [show that the cat played the piano] – VP

What did YouTube show? [that the cat played the piano] – CP

What did the cat do? [play the piano] – VP

What did YouTube show the cat played? [the piano] – NP

What was played? [the piano] – NP

Where did the cat play the piano? [YouTube] – NP

Where did the cat play the piano? *[YouTube showed] -not a constituent

Who played the piano? [the cat] – NP

This is also the point where, if there’s any movement that’s going on, you’ll want to “restore” the affected word(s) back to their original position(s). You may be able to do this with the same questions that you used to figure out subject and object, or if you’re dealing with a question, try answering it (“What did the cat play?” → “The cat (did) play the piano”, so your restored sentence is “The cat did play what?”).

YouTube showed that the cat played the piano, and Vimeo did __ too. ( __ = show that the cat played the piano – VP)

YouTube showed it (it=that the cat played the piano – CP)

The cat played the piano, and the dog did __ too. ( __ = play the piano – VP)

YouTube showed that the cat played it. (it = the piano – NP)

It showed that the cat played the piano. (it = YouTube – NP)

YouTube showed that s/he played the piano (s/he = the cat – NP)

Fortunately, there are a series of questions you can ask in order to figure out which phrases contain other phrases, and they’re known as constituency tests. Your course may give you a list of constituency tests – if not, here are two not-very-overlapping lists. Two very common types are questions and substitution. I’ll demonstrate them both below for various constituents in the sentence “YouTube showed that the cat played the piano.”

Questions:

Substitution:

When you figure out that some string of words is a constituent, put square brackets around it and label what kind of constituent it is (NP, PP, VP, etc). I like to do this by hand because I can make the square brackets of different sizes or colours to keep track of them all. If you find it helpful, you can also use underlining, highlighting, or circles to group other elements. (Here’s an example of particularly enthusiastic colour usage.)

If we have an extra modifier of some sort, we can use the same tests to figure out what it modifies. For example, we can add several different prepositional phrases beginning with “in” to the sentence above:

YouTube showed that the cat played the piano in a blue shirt.YouTube showed that the cat played the piano in high definition.YouTube showed that the cat played the piano in the living room.

But [in something] doesn’t necessarily modify the same thing each time, so we need to ask questions about it:

What was in a blue shirt? The cat (playing), not YouTube or the piano.What was in high definition? YouTube’s showing, not the cat or the piano.What was in the living room? The piano (or maybe the cat playing) but not YouTube.

And based on the answers to these questions, we can mark up the sentence as follows:

YouTube showed that the cat [ played the piano in a blue shirt ]VP YouTube [ showed [ that the cat played the piano ]CP in high definition ]VPYouTube showed that the cat played [ the piano in the living room ]NP

Why are we doing this step? This step is where most of the thinking happens: the goal is to know exactly what you’re putting in your tree before any of the tree-drawing actually happens. Otherwise it’s tempting to just draw a general shape that looks kind of like the trees you’ve seen before without fully examining what each part is contributing, which will end up as a mess. No really. It will.

As you get more practised at drawing trees, this step will take less time and you can skip over the easy parts if you want. For example, if parts of speech are 100% intuitive to you, you may be able to go right to labeling the constituents. Or you may not need to draw brackets around all the AdjPs and NPs, you can just figure out the subject and object and any ambiguities. But even once you’re fairly good at tree-drawing, if you’re ever faced with an unexpectedly daunting sentence to draw, you can always fall back on step 1: label the words and step 2: label the constituents. Or, if you’re drawing a really high-stakes tree, for a test or an assignment, you may still want to proceed as methodically as possible to reduce your chances of making a silly mistake.

Note that I have not gotten into sentence-level projections (i.e. S or IP or TP, whichever you’re using). There’s a reason for that: first of all, that’s where there’s the most variance between course conventions. Secondly, this is the type of projection that may not have an obvious head (unless you have an Aux and your course is treating Aux as an I head). And thirdly, you automatically need an S/IP/TP to contain your entire sentence, so it’s not a particularly informative constituent to add since it will literally just contain the whole sentence. BUT, and this is important, you also need another S/IP/TP for every additional verb that you have in your sentence, because any verb and associated words (subject, object, etc) could also be a sentence by itself.

For example:

YouTube showed that the cat played the piano.

Contains two verbs, “showed” and “played” and thus two clauses, each of which could be a sentence with minor modifications:

The main (or matrix) clause: “YouTube showed…”

The subordinate (or embedded) clause: “That the cat played the piano”

So we also need to split these up. We know that “that” is a complementizer ©, so it creates a CP (or S’ if you’re using it), but inside the CP is an S/IP/TP (directly inside every CP, in the position of sister to the C, is an S/IP/TP).

Step 3: Draw tree

[S/IP/TP YouTube showed [CP that [S/IP/TP the cat played the piano ] ] ]

However, if you only have one verb in your sentence, then you also only have one clause, the main clause, so you can skip this part.

And here’s a static image of the final result (note that I’ve labelled both sides of the brackets for extra clarity, but you don’t have to):

Now, finally, we get to draw the tree!

A lot of people think that this is the step that you start at, but unless you’re dealing with a really, really easy sentence and you already have practice drawing trees, jumping right into drawing a tree will just leave you confused and not sure what to put where. It’s a lot better to be confused at the previous step, where you can pause and try a couple things and figure it out, rather than at this step, where you’ll be tempted to just draw something that looks basically like a tree and think “eh, good enough”. Trust me, despite the fact that the rules may look confusing and arbitrary to you when you’re starting out, they make good sense to your prof or TA, and it will be very obvious to them if you do that.

I’m going to point out at this stage that syntax trees are not something you can bs. However, this is in fact a great and wonderful thing, because it means that you can, with enough effort, check your own work and prove that your tree is correct (or fix it if it’s not), and you are not at the mercy of dubious judgement calls and whether or not the prof likes you. A correct syntax tree is correct, a wrong (portion of a) syntax tree is wrong, and although there are a few fudgey bits in the realm of “how much do we care that this thing is wrong?”, it is entirely possible to get a score of 100% in a syntax course or unit.

Syntax (and in fact linguistics in general, actually) is not one of those subjects where the prof artificially caps the maximum score at 80 or 85: if you draw a completely correct tree, or in general demonstrate a very thorough understanding of the topic, you will get full marks, and for every mark you lose, there is something very specific that you should have done differently, and most profs and TAs will tell you exactly what answer they were looking for. It’s very satisfying. (I mean, until you get to high-level courses where you realize that what a syntax tree should even look like is still a matter of theoretical debate, but in an intro course there is definitely still a right answer.)

But enough with the pep talk: how do you take your now-thoroughly-marked-up sentence and make it into a tree?

You’re going to start at the top. As tempting as it may be to draw your sentence along the bottom of the page and draw up from there, this is not how syntax trees are conventionally drawn. Why? Well, starting from the top gives you more symmetrically-sized branches rather than long branches on one side and cramped, tiny, hard-to-read ones on the other, as well as more space for movement arrows. It’s also how the vast majority of syntax trees are drawn, and it’s easier for everyone to interpret each other’s trees if we follow a few basic formatting conventions.

If you’re worried about spacing, note that English trees will branch more on the right than on the left, so starting your S/IP/TP about a third of the way over on the left side of the page at the very top is generally better than starting it in the middle. Or just find an empty classroom and practise on a large whiteboard/chalkboard where you don’t need to worry about wasting paper. Thanks to the miracles of modern technology, you can even take a photo of your whiteboard tree when you’re finished, which was definitely not a thing when I was an undergraduate.

(Related: do not, for the love of Chomsky, draw your syntax trees upside-down. That is, do not write the sentence along the top of the page and draw branches down from it. I’ve gotten one or two of these in every stack of syntax assignments I’ve marked, and while I do always try to mark it as much as possible, it takes me about three times as long to figure out what’s going on and hence makes me grumpy. And you don’t want a grumpy person marking your assignment. If you want to instead make your marker especially happy, I have gotten some delightfully entertaining drawings from students on tests and assignments over the years, so you know, feel free.)

So you’re going to start at the top of the page with your maximal sentence projection, S or IP or TP (that’s S’ or CP if you’ve got a question), and proceed down from there.

What’s in your subject? Put it all under that subject position.

Make sure to include bar levels if your course is using them.

Do you have anything modifying your verb? Put it under the VP (V’ if you’re using them).

What’s in your object? Put it all directly sister to your V.

Include bar levels again if relevant.

Do you have any embedded clauses? You’ve figured out what they’re embedded under in the previous step, now put them there. Be sure to include a CP (S’) and S/IP/TP before going into the subject, verb, etc of the embedded clause.

Is anything ambiguous? If you figured out an ambiguity in the previous step, there are a couple ways to proceed. If your course or assignment has mentioned ambiguity as an option, you should probably draw both trees and indicate which meaning each one corresponds to. If there’s a technical ambiguity but one interpretation is way more plausible than the other, and you haven’t been told to find ambiguities, you may be able to get away with just drawing the most plausible interpretation (although in that case, feel free to include a note to the marker). Draw ambiguities on separate trees though, not the same one.

If you need to draw movement, make sure you represent the original position and the moved position using whatever conventions your course is using (arrows, traces, etc).

If you’re using S, S will branch into an NP (subject) and a VP (containing V, the object NP, and maybe more) plus maybe other things.

If you’re using IP or TP, IP or TP will branch into NP (subject) and I’ or T’, which will branch into I or T and VP (containing V, the object NP, and maybe other things). (Or these NPs might all be DPs, if you’re using the DP hypothesis. If you’ve never seen a DP, just ignore everything I’m saying about them.)

Did you include all the words from the target sentence in your tree?

Does the tree, left to right, read in the same order as your target sentence? (After any movement has happened.) If this is not the case, you need to fix something.

Do all the words have their part of speech labels that you established at the beginning? (Double-check the tests you used at the labelling step if you want to be really careful.)

Is each head inside its corresponding maximal projection? (N inside NP, V inside VP, etc.)

If you’re doing strict X’ theory, do you have at least one bar level between each head and its maximal projection? (N’ between N and NP, V’ between V and VP, etc.)

Do you have any triangles? (Your instructor may use triangles. This is why you should not.)

Anyway, this first part is pretty much always the same for any sentence, which is why we didn’t really bother map out this top projection in the previous step.

If you have more than one verb, is each verb inside its own VP which is inside its own S/IP/TP? Is each non-matrix verb’s S/IP/TP inside its own S’/CP?

Is there any movement? If it’s a question, what would the statement version sound like? If there are multiple verbs, what would each mini clause sound like separately? If there is passive, what would the active version sound like?

What complements do you have, if any? Especially complements of PPs (generally NPs/DPs), or NPs and AdjPs (often PPs beginning with “of”). Are they in the right positions? (In X’ theory, a complement is sister to the head, so complement of a noun → sister to N, complement of an adjective → sister to Adj, etc. In a theory without bar levels, a complement is simply inside the phrasal level, so complement of a noun → inside its NP, complement of a verb → inside its VP, etc.)

What modifiers do you have, if any? AdjPs, AdvPs, chúng tôi does each modifier modify? Is it in the right position to do so? (In X’ theory, a modifier is sister to the bar level of the modified element, so modifying a noun → sister to its N’, modifying a verb → sister to its V’, etc. In a theory without bar levels, a modifier is simply inside the phrasal level of the modified element, so modifying a noun → inside its NP, modifying a verb → inside its VP, etc. Note that if you don’t have bar levels, there is no difference between a complement and a modifier.)

If you’re in X’ theory, another way of phrasing the previous two questions is: for every PP, is it a complement or a modifier? Is it in the appropriate position for which one it is? (AdjPs are almost always modifiers, unless they’re after a verb: “the blue shirt” = modifier; “the shirt is blue” = complement. I can’t think of any examples where an AdvP would be a complement and not a modifier, but you may want to double check just in case. And an AdvP might modify several different things, so especially check that part.)

What’s the subject (including modifiers & complements) of each verb? Is it in the appropriate subject position that your course is using? (Under S or sister to I’ are the most likely.)

What’s the object (including modifiers & complements) of each verb? Is it sister to the verb?

Do you have any ambiguity? If so, have you represented it clearly?

Now you’re going to take the constituents that you figured out in the previous step and get them into the tree:

When you think you’ve finished your tree, here are a few common errors that you can check:

Basic errors:

More specific errors:

This is part 8 of a series on how to draw syntax trees. If you’re confused about why I keep referring to different course conventions rather than giving a specific answer, check out the remainder of the series below.

Here’s a gif of drawing the tree, again assuming you’re using a strict X-bar theory with IPs (note that the colours correspond to the colours of the brackets I used at the previous step – this is for your ease in following, it’s not a thing you need to do in drawing your own trees, as switching ink colours this often will probably make it harder).

And here’s the final tree as a still image:

Lastly, here are the brackets and the tree side-by-side, just for fun.

Notice that subjects are in spec, IP (sister to I’) of their respective IPs, and objects are in complement position of their respective verbs (sister to V). These trees assume you’re doing a strict X’ theory structure: if you’re using TPs or DPs, feel free to substitute them instead, and if you’re doing basic phrase structure rules, substitute S for IP and delete all the bar levels (just move everything up to attach directly to the phrase).

Giải Thuật Lập Trình · Cây Avl I — Avl Tree I

Trong bài trước, chúng ta đã tìm hiểu sơ bộ về cây nhị phân tìm kiếm, tìm kiếm trên cây và định nghĩa hai loại cây: AVL và cây đỏ đen. Trong bài này ta tìm hiểu cách chèn thêm một khóa vào cây AVL mà vẫn đảm bảo các tính chất của cây AVL sau khi chèn. Bài tiếp theo, chúng ta sẽ tìm hiểu cách xóa một nút khỏi cây AVL.

Một số khái niệm như chiều cao $height(v)$ của một nút $v$, bạn đọc xem lại post trước. Mình chỉ nhắc lại tính chất cân bằng của cây AVL ở đây.

Cây AVL: Mọi nút trong $v$ trong một cây AVL đều thỏa mãn:

  Trong post trước, ta đã chứng minh rằng cây AVL có $n$ nút trong sẽ có chiều cao $O(log n)$. Ở đây ta vẫn giữ quy ước các nút lá là Null.

Chèn vào cây AVL

Giả sử khóa ta muốn chèn vào trong cây AVL là một khóa mới, i.e, không có khóa nào trong cây có cùng giá trị. Điều này có thể được đảm bảo bằng cách kiểm tra xem khóa đã tồn tại trong cây hay chưa trước khi thực hiện chèn. Sau khi chèn một khóa, ta phải đảm bảo hai điều kiện:

Cây nhị phân sau khi chèn vẫn là cây AVL, i.e, phương trình $(1)$ thỏa mãn tại mọi nút của cây.

Để đảm bảo điều kiện (1), ta luôn chèn một khóa mới vào vị trí sao cho nút mới chèn là nút lá của cây. Tạm thời gác lại điều kiện (2), giả mã chèn vào cây như sau:

UnbalancedInsert(node $r$, key $k$):     if $r = $ Null         return MakeNode$(k)$     if $k[r]$ < $k$         $R[r] leftarrow $ UnbalancedInsert($R[r], k$)     else         $L[r] leftarrow $ UnbalancedInsert($L[r], k$)     $height(r) leftarrow max(height(L[r]), height(R[r])) + 1$     return $r$

Figure 1: (a) một cây AVL. Node có kí hiệu ${., +, -}$ lần lượt tương ứng với trường hợp chiều cao con phải bằng chiều cao con trái, chiều cao con phải lớn hơn chiều cao con trái 1 đơn vị và chiều cao con phải nhỏ hơn chiều cao con trái 1 đơn vị. (b) cây thu được sau khi chèn khóa 15. Chú ý sự thay đổi của 3 kí hiệu ${.,+,-}$. (c) cây thu được sau khi chèn thêm khóa 12. Hai nút tô màu hồng và có dấu $++$ là nút bị mất cân bằng, chiều cao con phải lớn hơn chiều cao con trái 2 đơn vị tại các nút này.

Phép chèn như trên đảm bảo điều kiện (1) nhưng sau khi chèn, cây mới có thể vi phạm điều kiện cân bằng . Ví dụ trong Figure 1(c), sau khi chèn, các nút $10,7$ đều vi phạm điều kiện cân bằng. Do đó, ta cần một cơ chế để đảm bảo tính cân bằng sau khi chèn. Để hiểu các cơ chế này, trước hết, ta nghiên cứu cấu trúc của các nút bị mất cân bằng. Ta có nhận xét:

Observation 1: Các nút bị mất cân bằng là các nút nằm trên đường đi từ nút mới chèn tới nút gốc.

  Gọi $X$ là nút mới chèn. Gọi $A$ là nút bị mất cần bằng gần $X$ nhất. Gọi $h$ là chiều cao của nút $A$ trước khi chèn $X$. Không mất tính tổng quát, ta giả sử $X$ được chèn vào cây con phải của $A$. Ta có:

Observation 2: Trước khi chèn $X$, $height(L[A]) = h-2$ và $height(R[A]) = h-1$.

  Gọi $B$ là nút con phải của $A$. Theo Observation 2, $B not= $ Null. Bổ đề sau cho chúng ta biết cấu trúc của cây tại nút $B$.

Lemma 1: Trước khi chèn $X$, $height(L[B]) = height(R[B]) = h-2$.

  Chứng minh: Giả sử $height(L[B])$ < $height(R[B])$. Nếu $X$ được vào cây con phải của $B$ thì nút $B$ sẽ bị mất cân bằng, trái với giả thiết $A$ là nút mất cân bằng gần $X$ nhất. Nếu $X$ được chèn vào cây con trái của $B$ thì chiều cao của $B$ không thay đổi sau khi chèn $X$. Như vậy, $A$ sẽ không bị mất cân bằng. Trái với giả thiết $A$ bị mất cân bằng.

Trường hợp $height(R[B])$ < $height(L[B])$ chứng minh tương tự.

Xem Figure 2 min họa cấu trúc của cây tại $A$ và nút con phải $B$. Figure 2: Cấu trúc của cây nhị phân tại $A$ và $B$.

Ta xét hai trường hợp.

TH1: $X$ được chèn vào cây con phải của $B$.

Xem minh họa trong Figure 3(a). Trường hợp này ta thực hiện quay trái (left rotation) tại $A$: ta gán con trái của $B$ làm con phải của $A$ và gán $A$ làm con trái của $B$ và cho nút cha của $A$ trỏ vào $B$. Xem minh họa trong Figure 3(b). Figure 3: (a) nút $A$ bị mất cân bằng (lệch về bên phải) sau khi chèn $X$ vào cây con phải của $B$. (b) phép quay trái tại $A$ để thự hiện cân bằng. Chú ý chiều cao của $A,B$ và sự thay đổi các kí hiệu ${.,+,-}$ sau khi quay.

Ta có:

Observation 3: Phép quay trái đảm bảo tính tìm kiếm của cây nhị phân sau khi quay. Ngoài ra, sau khi quay trái tại $A$, nút $B$ có chiều cao $h$ (bằng chiều cao ban đầu của $A$). Do đó, điều kiện (2) được đảm bảo sau phép quay.

Giả mã:

  Code C:

Node *left_rotate(Node *A){ Node *B = A-&gt;right; A-&gt;right = B-&gt;left; B-&gt;left = A; A-&gt;height = max(height(A-&gt;left), height(A-&gt;right)) + 1; B-&gt;height = max(height(B-&gt;left), height(B-&gt;right)) + 1; return B; }

TH2: $X$ được chèn vào cây con trái của $B$.

Gọi $C$ là nút con trái của $B$. Lập luận tương tự như Lemma 1, ta có:

Lemma 2: Trước khi chèn $X$, $height(L[C]) = height(R[C]) = h-3$.

  Xem minh họa Lemma 2 trong Figure 4(a). Như vậy, sau khi chèn $X$, cây con trái hoặc phải của $C$ sẽ có chiều cao $h-2$, tùy thuộc $X$ bị chèn vào cây con nào.

Trong trường hợp này, ta thực hiện phép quay kép trái (doubly left rotation) tại $A$ như sau: (a) gán con trái của $C$ làm con phải của $A$, (b) gán con phải của $C$ làm con trái của $B$, (c) gán $A$ làm con trái của $C$ và (d) gán $B$ làm con phải của $C$. Xem minh họa trong Figure 4(b).

Figure 4:(a) nút $A$ bị mất cân bằng (lệch về bên phải) sau khi chèn $X$ vào cây con trái của $B$. Trong hình trên, ta giả sử $X$ được chèn vào cây con trái của $C$. Nếu $X$ được chèn vào cây con phải của $C$ thì thao tác quay vẫn không thay đổi. (b) phép quay kép trái tại $A$ để thự hiện cân bằng. Chú ý chiều cao của $A,B, C$ và sự thay đổi các kí hiệu ${.,+,-}$ sau khi quay.

Observation 4: Phép quay kép trái đảm bảo tính tìm kiếm của cây nhị phân sau khi quay. Ngoài ra, sau khi quay trái tại $A$, nút $C$ có chiều cao $h$ (bằng chiều cao ban đầu của $A$) và $A,B$ đều có chiều cao $h-1$. Do đó, điều kiện (2) được đảm bảo sau phép quay.

Giả mã:

Node * double_left_rotate(Node *A){ Node *B = A-&gt;right; Node *C = B-&gt;left; A-&gt;right = C-&gt;left; B-&gt;left = C-&gt;right; C-&gt;left = A; C-&gt;right = B; B-&gt;height = max(height(B-&gt;left), height(B-&gt;right)) + 1; A-&gt;height = max(height(A-&gt;left), height(A-&gt;right)) + 1; C-&gt;height = max(height(C-&gt;left), height(C-&gt;right)) + 1; return C; }

Thủ tục chèn đầy đủ như sau:

Insert(node $r$, key $k$):     if $r = $ Null         return MakeNode$(k)$     if $k[r]$ < $k$         $R[r] leftarrow $ Insert($R[r], k$)         if $height(R[r]) = height(L[r])+2$             $B leftarrow R[r]$             if $height(R[B]) = height(L[B]) + 1$                 return LeftRotate$(r)$                     $ll$ TH1 $ gg$             else                 return DoubleLeftRotate$(r)$             $ll$ TH2 $ gg$     else         $L[r] leftarrow $ Insert($L[r], k$)         if $height(L[r]) = height(R[r])+2$             $B leftarrow L[r]$             if $height(L[B]) = height(R[B]) + 1$                 return RightRotate$(r)$             else                 return DoubleRightRotate$(r)$     $height(r) leftarrow max(height(L[r]), height(R[r]))+1$     return $r$

MakeNode(key $k$):     node $u leftarrow$ an empty node     $k[u] leftarrow k$     $L[u] leftarrow R[u] leftarrow $ Null     $height(u) leftarrow 1$     return $u$

Giả mã của RightRotate và DoubleRightRotate coi như bài tập cho bạn đọc. Code đây đủ thủ tục chèn trong C:

typedef struct Node{ int key; int value; struct Node *left, *right; int height; } Node; Node *insert(Node *root, int k, int value){ if(root == null) return create_node(k, value); if(root-&gt;key &lt; k){ root-&gt;right = insert(root-&gt;right, k, value); if(height(root-&gt;right) == height(root-&gt;left) + 2){ Node *b = root-&gt;right; if(height(b-&gt;right) == height(b-&gt;left) + 1){ return left_rotate(root); }else { return double_left_rotate(root); } } }else{ root-&gt;left = insert(root-&gt;left, k, value); if(height(root-&gt;left) == height(root-&gt;right) + 2){ Node *b = root-&gt;left; if(height(b-&gt;left) == height(b-&gt;right) + 1){ return right_rotate(root); } else { return double_right_rotate(root); } } } root-&gt;height = max(height(root-&gt;left), height(root-&gt;right))+1; return root; }

Acknowledgement: Cám ơn bạn Nam Nguyen đã chỉ ra một số lỗi sai rất tinh vi trong phiên bản trước của bài vết.

Code đầy đủ: AVL-tree Tham khảo

[1] G. Adelson-Velsky and E. Landis. An Algorithm for the Organization of Information. Proceedings of the USSR Academy of Sciences (in Russian). 146: 263–266. English translation by Myron J. Ricci in Soviet Math. Doklady, 3:1259–1263, 1962. [2] D. Knuth. The Art of Computer Programming, Volume 3: Sorting and Searching, Third Edition. Addison-Wesley, 1997.

Related

How To Draw Activity Diagram?

How to Draw Activity Diagram?

Activity diagram is a kind of UML diagram that shows flow of control from activity to activity. It shows concurrency, branch, control flow and object flow. Furthermore, swimlane is used for partitioning actions based on the participants involved.

Creating activity diagram

Perform the steps below to create a UML activity diagram in Visual Paradigm.

In the New Diagram window, select Activity Diagram.

Enter the diagram name and description. The Location field enables you to select a model to store the diagram.

Creating swimlane

Inserting partition to swimlane

A partition is inserted.

Creating initial node

Creating action

Move your mouse pointer over the source shape.

Press on the Resource Catalog button and drag it out.

Release the mouse button at the place where you want the action to be created.

A new action will be created and is connected to the source shape with a control flow. Enter its name and press Enter to confirm editing.

Working with scenario

A scenario is a diagram formed by the internal interaction of a sequence of action, modeled by their sub-diagrams. With scenario, you can produce a diagram which presents an overview of an execution path in activity diagram, so as to know how user and system communicate with each other in order to complete the flow.

Producing scenario from activity diagram

NOTE: A path is a continuous flow of actions in the diagram, with an initial node placed at the beginning of the actions. Multiple paths are obtained by determining the existence of decision nodes within the flow.

Name the scenario. Add description if necessary.

The actions being involved in the flow are listed in the Path table. For actions that have sub-diagram(s), pick up the sub-diagram in Diagram column or just create a new one. You may, however, leave it unspecified which cause that action to be ignored when producing scenario.

Updating scenario

Related Resources

The following resources may help you to learn more about the topic discussed in this page.

Bạn đang xem bài viết Drawing Sentence Syntax Trees – Amy Reynolds trên website Maiphuongus.net. Hy vọng những thông tin mà chúng tôi đã chia sẻ là hữu ích với bạn. Nếu nội dung hay, ý nghĩa bạn hãy chia sẻ với bạn bè của mình và luôn theo dõi, ủng hộ chúng tôi để cập nhật những thông tin mới nhất. Chúc bạn một ngày tốt lành!