Preview

Array Representation

Good Essays
Open Document
Open Document
338 Words
Grammar
Grammar
Plagiarism
Plagiarism
Writing
Writing
Score
Score
Array Representation
Array Representation

A complete binary tree has a simple array representation. Suppose we number the nodes from left to right, beginning at the top and ending at the bottom. Then we can store the various data items in the corresponding elements of an array. For example

can be represented by the array

This in fact corresponds to the level order enumeration of the tree. Note that we only use an initial segment of the array. Provided the array is long enough, and we know the number of tree nodes, it doesn't matter how many unused components there are at the end.

* length[A]: the size of the array * heap-size[A]: the number of items stored into the array A * Note: heap-size[A] <= length[A] * The root of the tree is at A[1], i.e., the indexing typically begins at index 1 (not 0). A[0] can be reserved for the variable heap-size[A].
Heap is implemented as an array, but its operations can be grasped more easily by looking at the binary tree representation. The mapping between the array representation and binary tree representation is unambiguous. The array representation can be achieved by traversing the binary tree in level order.

Figure 1: Binary tree and array representation for the MaxHeap containing elements (that has the priorities) [16,14,10,8,7,9,3,2,4,1].
2.1 Routines to access the array
Lets consider the i:th node in a Heap that has the value A[i] PARENT(i) = i/2 | Return the index of the father node | LEFT(i) = 2i | Return the index of the left child | RIGHT(i) = 2i+1 | Return the index of the right child |
Example 1: In Figure 1, node i=3 (A[3]=10) has father at index PARENT(3) = 3/2 = 1 (A[1] = 16). In addition, its left and right children are at LEFT(3) = 2*3 = 6 and RIGHT(3) = 2*3 + 1 = 7 (A[6] = 9 andA[7] = 3,

You May Also Find These Documents Helpful

  • Good Essays

    Nt1420 Unit 6

    • 1145 Words
    • 5 Pages

    Question 6 A hash table is a data structure that uses an array as its internal storage container. Items are added to the array based on the integer generated by a hash function. A hash function produces an integer based on some properties of the object. In Java hash functions are encapsulated via the hashcode method in the Object class and that many classes override.…

    • 1145 Words
    • 5 Pages
    Good Essays
  • Good Essays

    ECET 370 Week 5 Lab 5

    • 650 Words
    • 3 Pages

    Exercise 1: Review of the Lecture Content Create a project using the ArrayList class and the Main class provided in DocSharing. The ArrayList class contains implementations of the first three search methods explained in this week's lecture: sequential, sorted, and binary search. The Main class uses these three methods. These programs test the code discussed in the lecture. Compile the project, run it, and review the code that is given carefully.…

    • 650 Words
    • 3 Pages
    Good Essays
  • Good Essays

    Cse 373 Final Note

    • 955 Words
    • 4 Pages

    An ordered or sorted binary tree, is a node based binary tree data structure that has the following properties:…

    • 955 Words
    • 4 Pages
    Good Essays
  • Good Essays

    Array Structure Paper

    • 833 Words
    • 4 Pages

    “An array is a collective name given to a group of similar quantities. These similar quantities could be percentage marks of 100 students, number of chairs in home, or salaries of 300 employees or ages of 25 students. Thus an array is a collection of similar elements. These similar elements could be all integers or all characters, and so on” (Thompson, 2007). Building an array can be confusing for the Bug Blasters can be very confusing for a programmer who does not fully understand how they work.…

    • 833 Words
    • 4 Pages
    Good Essays
  • Better Essays

    Dingo Bone Structure

    • 1145 Words
    • 5 Pages

    1. fossil record/genetic record: The two species that are completely different from the African Wild Dog are the Dingo and the Red Wolf. The first difference from the African Wild Dog, is that their scientific names are different. The second difference is that the African Wild Dog has a smaller body, fewer toes, and teeth structure in the jaw.…

    • 1145 Words
    • 5 Pages
    Better Essays
  • Powerful Essays

    Searching and Sorting Streams and Files Graphics GUI Components and Events Mouse, Keyboard, Sounds, and Images Big-O Analysis of Algorithms The Java Collections Framework Lists and Iterators Stacks and Queues Recursion Revisited Binary Trees Lookup Tables and Hashing Heaps and Priority Queues Design Patterns…

    • 3908 Words
    • 16 Pages
    Powerful Essays
  • Good Essays

    Comp 220

    • 1463 Words
    • 6 Pages

    Pointers also have the requirement that the pointer type must be of the same data type as the variable, or the data that it points to or holds the address of. The power of pointers also hints at the potential complexity of their use, which is why this lab is focused almost entirely on several different aspects and uses of pointers. The lab also introduces pointer arrays and pointers to pointers.…

    • 1463 Words
    • 6 Pages
    Good Essays
  • Powerful Essays

    System |Purposes |Examples |Uses | |Word processor |The word processor, Invented in the early 1900s an updated prototype of the typewriter, creates a document in digital format but goes one step further than the typewriter and stores the information. Word processors can track changes and make corrections as you type. |Microsoft word which widely used for in the business and personal world. Word perfect, Word pad, and Word open office writer |One of the biggest uses for me is writing my papers for class. The fast connivance to proof read and spell check save me time. Allows the user to save and share his document. Word processors gives the option of making comparisons of documents; in cases where several users may be working on the same documents, the comparison feature allows for differences in the documents to be easily viewed, making editing more streamline(brown). | |Hierarchical database |Hierarchal data can be organized data where each level of the data is organized, resembling a tree. Data can be arranged in a repeating series that ties or links the data together.Its designed to process and manipulate data in a number that takes advantage of the hierarchical(Prabhakran & Rajamani). |A family tree can be organized in a structured form. Prabhakaran & Rajamani). |This type of Data base is used mostly to save and retrieve information efficiently. | |Flat Database |A system where information can be stored. Flat files are simple data that files that take up very little space that other files.Flat files are like spread sheet where rows on the…

    • 1437 Words
    • 6 Pages
    Powerful Essays
  • Good Essays

    Over the brief time I’ve been driving, I found there to be a drastic difference in driving in the winter and driving in the summer. The winter weather brings many dangerous conditions to contend with on the road that the summer does not. Cold, wet, freezing winters are much harder to drive in than the dryer summer months.…

    • 303 Words
    • 2 Pages
    Good Essays
  • Powerful Essays

    Cen 254 Data Structure

    • 924 Words
    • 4 Pages

    keys 30, 40, 24, 58, 48, 26, 11, 13 (in this order). Draw the tree after each…

    • 924 Words
    • 4 Pages
    Powerful Essays
  • Good Essays

    Binary files play an important role in data storage because they are very compact for any given amount of data to be stored. Compactness of storage is critically important for any type of situation involving computers, so binary files are specific to a particular application. New types of data storage must be easy to create and maintain, not just compact. XML plays a part in a process of making data easier to structure and describe. Data structures that can be easily modified or adapted, together with processors that access data contained in those structures, make it easier to create, maintain, and modify data storage than when using binary files alone. (Watt, 2003)…

    • 703 Words
    • 3 Pages
    Good Essays
  • Good Essays

    Deciding which data structure to then write it in java was challenging cause you have to make sure that the steps are in the right sequence for the program to work.…

    • 605 Words
    • 3 Pages
    Good Essays
  • Good Essays

    Doubly Linklist

    • 555 Words
    • 3 Pages

    #include<stdio.h> #include<conio.h> #include<alloc.h> typedef struct dll { int data; struct dll *next; struct dll *prev; }node; //struct dll *head= NULL; node *getnode() { node *temp; temp=(node *)malloc(sizeof(node)); temp->next= NULL; temp->prev=NULL; return temp; } node *creation() { node *temp,*new1,*head; int f=1; char c; do { new1=getnode(); printf("\n\nEnter the data.\n\n"); scanf("%d",&new1->data); if(f==1) { head=new1; temp=head; f=0; } else { temp->next=new1; new1->prev=temp; temp=new1; } printf("\n\nDo you want to continue with creation.\n\n"); c=getche(); }while(c=='y'||c=='Y'); return head; } void display(node *head) { node *temp; temp=head; printf("\n\nThe list is...\n\n"); while(temp!=NULL) { printf("%d\n",temp->data); temp=temp->next; } } node *insertion(node *head) { node *new1,*temp; int c,value; char ch; do { new1=getnode(); printf("\n\nEnter the data to be insered.\n\n"); scanf("%d",&new1->data); printf("\n\nEnter your place of insertion.\n\n"); printf("\n1.Head.\n2.Intermediate.\n3.Tail.\n"); scanf("%d",&c); switch(c) { case 1: temp=head; new1->next=temp; temp->prev=new1; head=new1; break; case 2: temp=head; printf("\n\nEnter the value after which the insertion has to take place.\n\n"); scanf("%d",&value); while(temp->data!=value) { temp=temp->next; } temp->next->prev=new1; new1->next=temp->next; temp->next=new1; new1->prev=temp; break; case 3: temp=head; while(temp->next!=NULL) { temp=temp->next; } temp->next= new1; new1->next=…

    • 555 Words
    • 3 Pages
    Good Essays
  • Good Essays

    Attribute grammar

    • 591 Words
    • 3 Pages

    Exercise 6.6 of the text contains a grammar that describes binary trees where each node of the tree has an integer label. It discusses an ordering requirement that is the same one required by binary search trees. Solve that exercise.…

    • 591 Words
    • 3 Pages
    Good Essays
  • Better Essays

    c++ balaguruswamy

    • 1703 Words
    • 22 Pages

    Top of Form Technical Interview Questions Google Search C++ Interview Questions C# Interview Questions C Interview Questions ......... More pub-7003540323 ISO-8859-1 active GALT:#008000;G C ++ Aptitude Questions C Aptitude Questions 2 ......... More 3422992112 ISO-8859-1 Aptitude Interview Questions 1 en Bottom of Form Tutorials C Tutorial C++ Tutorial C Aptitude Questions and Answers…

    • 1703 Words
    • 22 Pages
    Better Essays

Related Topics