Address
:
[go:
up one dir
,
main page
]
Include Form
Remove Scripts
Session Cookies
Open navigation menu
Close suggestions
Search
Search
en
Change Language
Upload
Sign in
Sign in
Download free for days
0 ratings
0% found this document useful (0 votes)
38 views
35 pages
Kachra
Uploaded by
Pritpal Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here
.
Available Formats
Download as PDF or read online on Scribd
Download
Save
Save Kachra For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
0 ratings
0% found this document useful (0 votes)
38 views
35 pages
Kachra
Uploaded by
Pritpal Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here
.
Available Formats
Download as PDF or read online on Scribd
Carousel Previous
Carousel Next
Download
Save
Save Kachra For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
Download now
Download
You are on page 1
/ 35
Search
Fullscreen
2020 2019 HM mary search #1625; tacks, Queues, Linked lists, Trees, Binary search Programming in C, An heaps, Graphs Programming in C, Arrays, Stacks, Queues, Linked lists, Trees, Binary search tr€eS, heaps, Graphs 3 Programming in C, Linked lists, Graphs, Binary search trees Pointers, Link list, Structure, Tree, Quewe, Queue, Pointers, Arrays, Recursion, Se Tree traversal, Binary searchinked lis manipulates information. is the study of how information is can be utilised. Data struc- rganising in computer's memory d efficiently. Examples of ures are arrays, linked lists, sand hash tables. Different are suited to different kinds are highly specialised to spe- types of data structures are used pplications, which are specialised task. Data structures provide a large amounts of data efficiently, such id Internet indexing services. Data ned to manage a large amount of data. purpose so that it can be accessed and pROGRAMMING AND DATA STRUCTURES in C; functions, recursion parameter passing, scope, bind. res, binary search trees, binary heaps. worked in an appropriate manner. Usually, efficient data structures are a key in designing efficient algorithms. In programming, efficient data structures are used to desig: efficient programs, both in terms of time and memory In the following text, the programming part is discussed followed by data structure. BASIC TERMINOLOGY This section defines the basic terms used i programming. computer Programming Languages ‘A computer performs the tasks commanded by the user. The command is given through a sequence of lines of code that are understood by the computer. The art ofwh a code (or a program ordi nas programmer Niklaus Wirth, PROGRAMS = DATA 4 ALGORITHMS Programming language has two important pil ‘ax (refers to grammatical representation) and (refers to the meanin, be correc hould A correct. program m syntactic view as well as ftom The different kinds of translators check only the syntactic representation and the programmer takes care of the semantic representation, There are dif- ferent generations of programming languages: 1. First Generation/1GL/(Machine level pro- gramming): The program is written in a sequence of Os and Is (binary number system), Such a. language is more closer to the machine and the brograms are machine dependent and difficult to debug. The only advantage is that there is no need for translator software (as the program is already in machine code) and execution is very fast. 2. Second Generation /2GL/(Assembly level Programming): The program is written using instructions based on mnemonics and hexadecimal number system. Such a Janguage is also machine- dependent. Assembler is the translator used to convert assembly level code to machine level code. ‘More the programs are reader friendly, easier is it to debug them. is 8. Third Generation /SGL/(High-k gramming): The program ‘is English-like sta an spa 1. Procedural Languages: languages which follow a certain pro are of three different types-: Object-oriented languages: tions, objects are used. The tions are combined in de of the important featu Encapsulation, Polymo:n entity of a program ns with other the fundamental nf OOP are as an object. It does not will define the object of operations performed It is used to provide essential »gram without presenting, the na database system, only al the information regarding creat s hidden, s used to place data and fune- he process of forming a new class lass. The new class is known as the the existing class is known as the ses the concept of reusability of code. phism: It is used to provide different functions to the existing operators or is @ branch of polymorphism. ing operator or function is made to new data type, then it is known as gramming approach that revolutionized concept, proposed by E. Dijkstra’. stra submitted the famous article in highlighting the drawbacks of using the nt. He advocated that program written of GOTO statement (or JUMP state- pre stable, Nowadays, more and more following such kind of approach in Other notable attributes of struc- ing are: analysi ‘smaller sub-problems. easic TeRmnoLocy 129 2, Modularization: Dividing the program into small independent modules, Data Types It constitutes the type of values a variable can take and 4 set of operations that can be applied to those values. Data types in programming language can be broadly bool array A large problem is sub- [Data type} (Derived dal | aes function pointer Jenumerat ‘Table 1| Basic data types, their stunsigned ton, flo dout long double © language is a w sakly typed language so it allows implicit and explic type conversions (Fig. 2). The com- piler promotes each term in a binary ex sion to the highest precision operand. Control Flow Statements Programs consist of several statements and execution is not limited to a linear sequence of statements. During its execution, a program may repeat segments of code, 3,648 t 2,147.48:al the condition is satisfied then the state- e the parenthesis {} will be executed. If the condition is true then the state- ween if and else will be executed. If it the statement after else will be exe- nested if, one or more if-else block(s) lie parent if statement. the condition is true then the state- if and else if will be executed. If it the condition in else if is checked, and it will be executed. It is also known as matching case state- is used to test for equality against a list Jt matches the value in variable ‘ose inside the switch. If it matches t ill be ‘Basic TERMINOLOGY 131vee) frown the Array handles siunilar types of data. For example, stor ny, tasers of 100 students Aw array is set of bomnoyencns elermente, which ses comtignums mewory locations, The lrwest, abdreas car rempents 0 the Sire semen and the highest address to the laet chernent, Decloring an Array Array can te declared we Sollome, type arreyiene (arraysize)s where wusnbered ue rvsy age in Sigg leer, an anny bs alae iw array, Supine, he 110) anh he ie Of Si ie A ss (addres of aged) I be 100% td 0 OM Importont Things to Remember Buppowe yor dente, Por exazuphes fro1n uer|0) 10 wer|9) axe: arr(\3), ete. are nied, show error but grain execution, — “The size of theArray’ Hs the base 1b, (ub, —1 1000 ment = 3 199} (299 141 (5))~Ba+(4—1b1)* We 0) 14991) = BA+ { (250-50) #202 sider the following, array: of a[250)/499] by row major order, { array, Ib is the lower {each element 1" is declared as follows: row, bp is the lower he upper bound for row and uldress, Nr is the number of 1) and Ne is the number of 1). lowing array: bytes BA + (i = Ib) * ¢ 1000 + (397 = 1) #3 1000 + 396 * 3 2188 500) +(3-1b2) +e +(499~299)) #5, = 0+ (200*202+200)*5 = 203000 . asic Termmovocy — 133 Function and Recursion A funetion is a group of statements, which is used to perform a task and recursion is a programming tech- nique, which is used to express operations in terms of themselves, Function A function is a set of statements that together perform a task. Every C program has at least one function which is inain(), and programs can have additional functions. In other words, a function is a self-contained block, which takes some input and gives output after processing (Fig. 5). A function is known with various names such as ‘a method or a sub-routine or a procedure, te. Input 2 t Funetion F enh f(z) Figure 5A view of function Im intavg (inta, intb, intc) ( float result; result = (a+b+c)/3; return result;5. Return statement: xecuted, then the funet Guition is already respective language is called tion, These funetions are or particular language language, printf(), scanf(), able in stdio.h header file peciormed by that fonction, Reters type: A famction returns a value. Thenneral recursive func sum() function is called itself in else fequal to 0 then the function returns the the argument, but if nis not equal to np calls itself. Let n = 5 initially, then xd to the function and the value of decreased by 1 until the condition is fend, when n becomes 0, the value of nm js from 5 to 1. sum(2) 2+ sum(1) 2414+ sum(0) 24140 241 3 following advantages: calling of funetions is avoided. the function calls anywhere in_ the sAsic TERMINOLOGY — 135 Pointers A pointer is a variable which stores the address of some other variables. The detlaration of a pointer is done as, follows: bata t}pe *variable_nane; I pointer to an integer */ + address of variable b is stored Address of variable b is: 8d", &b)7 "Address of variable b is: %d, p)é printf ("Value of variable b is: td", *P) /* *p displays the value at stored address */ ‘The operating system may stop to access memory and canse the program to crash. To avoid such problem, pointers should always be initialized before use. These are initialized using free memory in the following way: 1. Memory allocation to pointers: int *p = malloc (sizeof (*p)); 2. De-allocating memory of pointer: free (p)i Pointer to pointer: ‘ia ‘Address |-——*] Address int *p _/* pointer to an integer */ int **ptr /* pointer to pointer */ int b = 207 p= sb; /* address of variable b is stored in p */ ptr = 6p; /* address of pointer is stored in ptr */ To print value of b: *p = 207 spr = 207Output New Va . Decrementing a pointer: Decrementing pointer to a data variable will cause its value to be decremented by size of variable because memory required to store a variable (jnteger) vat compiler to compiler. Example (address - I= ~ gives an addres: . Adding an integer value with pointer: In C Programming, itis legal to add any integer number to pointer variable. pir +n = (ptr) + (n * size of data type); Hinelude
Ant main() oo int n, *ptel, *pEEae ptrl = &n7 ptr? = ptel + 27 printé ("Difference 4s perl) i arn ( Output Difference is 2 ptrl = 200 d ptr2 = 200+2* 4= ptr2-ptrl = (208-2 Both pointers numer! and technicall:Fhe program, variable i have been deleted fion of demoFun() function but still the to access the memory location of i because js not overwritten by another variable. It ne reason that dangling pointer consti- fined behaviour. But when we perform tion, complier gives some garbage value any data to that Hpointer p does not get tion. Passing nis types of parameter-passing techniques jing languages. The parameters passed ye program requirement. ‘According to the ‘ng, the passing parameter depends om ing function calls the called function. Parameter-Passing Technique fa function, we pass the to the "and it is by default: passed by the value ‘alling function. This is ealled ‘eall by int ye (int x, easic TeRMmoLocy 137 exchange (a, b); print£("\na = $d b = #4", a, b): ge(int x, int y) Before calling the function exchange() values of a and bare 10 and 20, respectively. After calling exchange(), # and y also receive 10 and 20 respectively. In exchange(, we are interchanging values of zand y. So, the new values of x and y are 20 and 10, respectively. After successful ‘execution of the function exchange(), control transfers to the main funetion, but in the main function, values of a weg remain unchanged, that i, 10 and 20, respectively. So, the output of the above program is as follows: a=10 b=20 x=20 y=10 an10 b=20 In the cal-by-value mechanism, only the formal argu ment (variables receiving value in called function from Calling function, that is, z and y in the above program) fgets changed and do not reflect in actual argument (vari ‘Sble in function call, that is, @ and 6 in function call to exchange(a, b))- Call-by-Reference Parameter-Passing Technique In call by reference, the parameter is passed through the factual address of the value, The address of the variable the execution of the program. to access and modify the value of the variable by using a pointer. In case of any changes in the parameter inside the ‘there is a change of the actual parameter. For the declaration of variable iene follows: ae tht eaaaunx > Lowation nam apf Location. val ie 1000 Address a igure 6 Memory map incall by eee: He number. ka Fi 6 able nae to represent ‘The selection of address for a par piler dependent and is always 01 fhe vale 20 can be accessed by using Vi find address 1000. The symbol ‘& is used the ‘address of. For example, expression address of variable x, which “An adres is always postive, we use operator For magne integer. 736 gpetal operator 1a C used as seseat address Iti also colled indirection operator, For example, *(&), which means value at addres(4ex), that i, value at address (1000). The exa ie gen bolow returns the address of a Yasable, he ee se of the variable and the value of variable using indirection operator. in the above case rettrns happens to be 1000. Output 10 b declaration of a variable: ‘ran that «is going to hold integer sans it is going to hold address ( tion) of intoger type of variable, We ate of variables a, b to function exchange ing we are using tO Generally, we use call: not need the changes made in callip pot called function. But if we re Teflect in the called function, we use| ca = [Also if we want to return m called function from the calling by-reference value, which is n Consider the not m simply= | Unions to represent a record (e.g, library). A ta type in C, which is used to store im the same memory location. Table son of the structures and union. ison between structure and union 1. To define union, the ‘union’ keyword is used. When a union variable is created, the compiler allocates ‘memory for that variable equal to the ‘maximum size of ‘union element. 3, Individual members of the union share the ‘memory allocated. 4, Only one member can be initialised at a time. 5. Only one member can bbe accessed at a time. Basic TERMINOLOGY 139) an, feb, mar, apr, mayy juny oct, nov, dech; month; Scoping The scope is a part of a program where the identifier may have direct access, and beyond that seope, a vari able cannot be accessed. In programming, variables can be declared in the following three ways: 1, Inside the main function 2. Outside the main function 3. Function parameter definition Local Variables A local variable is that variable which is declared in the body of the main function. It can be used only by the function in which it declares, Other function has no information about local variable. Formal parameter of & function is also treated as a local variable to that fune- tion. In the following example, num, sum, r are local variables, EZ finclude
int main (){ /* Local Variable Declaration*/ int num, sum=0, x3 print€("Enter a number: ")7 scanf ("%d", gum) : ‘while (num) ( r=num$107 num-nun/1! ) printf ("Sum of digits of number: return 0; ) a", sum): ‘Variables num, sum and rare local to the function main(), and cannot be accessible outside the main function. fo ams “ Be rice Global Variables Global variables are those ‘outside the variables wil140 Sino Enter a numbe Le (num) ( renumb0) m of digits of nunber Variables num, sum are local to the function main(), and cannot be accessible outside the main function. But the variable ris declared outside the main as a. global variable, it can be accessed by any funetion in this program. able 3 ‘Class Jefault value, seope and life of thea Storage, default value, seope and stornge classes ‘Auto ‘PU registers Memory Garbage Garbage value value Local to Local to, blo lock: Until ‘Until blockof late binding ithe executable image is small ss of late binding: ftable is dependent on the shared ule is shared, it must be reentrant safe pes is an object with a specification It is independent of implementation ions such as push an item onto fan item from the stack, ask if the implementation may be as array or tions such as add to the end of the nm the beginning of the queue, ask ‘empty; implementation may be as list or heap. jure: Operations such as insert an item is in the structure and delete mntation may be as array, linked collection of items in which the removed in order. In stack, the the top of the stack, Only one ele- ‘on the top, is removed from the stack is called last-in first-out ‘x can be implemented by linked stack 141 The basic operations on stack are as follows 1, PUSH: To add an element to the stack. 2. POP: To remove an element from the stack. PUSH Operation on Stack When we try to add elements to the stack then the operation is called PUSH operation on stack (Fig, 7). The element is placed only on the top of the stack and then the stack position gets increased byl D oc |— D Cc B PUSH B A A STACK BEFORE STACK AFTER Figure 7| PUSH operation on stack Code for PUSH Operation void push(int token) ‘ char a; int top = <1, int Stack(10)+ printf("stack full") ;}—> is Full Condition retur check } do ‘ printf ("\nEnter the token to be inserted:"); scanf ("$d", etoken) 7 top=top+l; stack(top]=token; print£("do you want to continue insertion YIN"): a-getch (); , while (am=!y")? ) POP Operation on Stack Whenever we try to remove then the operation is (Fig. 8).PACK INITIALLY POP igure 8| POP operation on stack. Code for POP Operation The following is the code of POP function in which we use the return statement to send the result back to the: ling function , testack[top]? top=top-1;sae 43 “The given expression into postfix, Expression = (A + B) * (C — D) be Bxpresien a Oe (A+ B)* (C- D) Empty Initial A+ B)*(C-D) ( Push +B)*(C-D) C A Print B)*(C-D) cs 2 Push )*(C- D) (+ AB Print *(C-D) AB+ Pop until ( and print (c-D) a ABE Push c-D) % ‘AB+ Push -D) =¢ AB+C Print D) (- AB+C Push ) "H AB+CD Print End 7 AB+CD- Pop until ( rsion 53. Apply algorithm infix to postix on the expression ‘that comes from step 2. to pret: oo 4. And finally reverse the output of step 3. ven infix expression. a “(as °)’ and every ‘)’ as “(7 wert the given expression into postfix: + BO) * D+ BS the infix expression. + DY) CB+ A( every “(’ as)’ and every ‘)’ as ‘( +D*(C°B+ A) Erg SE + D*(C*B+ A) “B+ D*(C°B+ A) E+D*(O*B+ +D* (CB + A)Table a ++ + Empty Queue is a special abstract data type storage structure, ‘The access to clements in a queue is restricted, arrays. Enqueue and dequeue are two used for the insertion of element i of an element from a Continued Output DOB" A SE*DCB*At SB’DCB A+++lowing types: ue or linear queue: In this data erations, such as, insertion and dele- t the rear and front of the list, respec Insertion, t Rear 0 | sapaeae cue: A. circular queue aan xies are treated as circular (Fig. 10). jc follows the last node. I re 10| Circular queue. vue: It contains items which have priority (Fig. 11). When an element joved from a priority queue; the item est priority is removed first. 30,40 highest priority 11,22, 39, next priority 88, 99, least priority J++ NULL 11| Priority quene. (double-ended queue): In_ this, deletion occur at both the ends, that rear of the queue (Pig. 3.12). Deletion Cot Rear 12] Doubleended queue. unkeD ust 145 Applications of Queues Quenes have many applicatio in computer systems: 1. Jobs in a single-processor computer 2. Print spooling 3, Information packets in computer networks rye Linked list is a dynamic data structure whose length can be increased or decreased at runtime. Linked list basically consists of memory blocks that are located at random memory locations. Node of a linked list consists of two parts, one is value or data and the other is pointer to the next node. Basic Operations Basic operations of a singly-linked list are as follows: 1. Create: Creates an empty node of linked list. 2. Insert: Inserts a new element at the end of the list. 3. Delete: Deletes any node from the list 4, Find: Finds any node in the list. 5. Print: Prints the lst. Funetions of these operations are listed as follows: 1. Insert: It takes the start node and data to be inserted as arguments. The new node is inserted fat the end of the linked lis, til it reaches the last node. Allocate the memory for the new node and put data in it, Store the address in the next field of the new node as NULL. Delete: It takes the start node (as pointer) and data to be deleted as arguments. Reach the node for which the node next to it has to be deleted. ‘If that node points to NULL (ie. pointer next = NULL) then the element to be deleted is not present in the list. Else, the pointer points to a node, whose next node has to be removed, declare a temporary node (temp), which points to the deleted node. Store the address Se node next to the tem-start=temp->m then the key is found. pa condition hi arch (pointer ieee tother ) found return 0, else return 1 else start node (as pointer) j then there is ieee valugay free (temp) return 17 4. Display: Function takes the as ait argument, If pointer = NULL loment in the list, Else, print the da ata) and move to the ne he print function with the niode (pointer node by recursively callin pointer —» next sent as an argument inked List Implementation Linked list is innplemented as follo 1, Node Structure of Linked List: ructnode *temp, *New Node; New_Node=(structnode *) malloc (sii New Node: New_Node: ‘temp-Start;jon of nodes and edges. When we con- th the help of edges then it constructs ing has the properties of a tree is called the root node. ery other node(n) in a tree is ted by an edge from another node(y), le(y) is called the parent node of m. jue path from start node to end node, ot node tree data structure. In a binary tree, tree can have at most two children. iised to construct binary search trees An example of a binary tree is shown, GQ () & OOO figure 13| Binary tree. and definitions: We will formally and its components, ic is an important part of a tree. It name, which we call the ‘key’. Tt may tional information. ecge is another important part of a wets two nodes to show a relation them. Every node (except the root) with exactly one incoming edge from Rach node may have several outgo- Toot of the tree does not have incoming, is an ordered list of nodes, which are edges. For example, 1 + 7 + 6 = trees 147 6. Children: It is a set of nodes ($), which have incoming edges from the same node. In Fig. 13, 2 and 6 are the children of node 7. 7. Parent: It is the parent of all the nodes that are connected to the outgoing edges. In Fig, 13, node 5 is parent of node 9, 8. Sibling: The nodes in the tree having the same parent: are said to be siblings. The nodes 2 and 6 are siblings in the tree shown in Fig. 13 9. Subtree: It is a set of nodes and edges comprised of a parent: and all the descendants of that parent. 10. Leaf node: It is a node that has no children, For example, node 15 and node 11 are the leaf node in Fig. 13. 11. Level: The level of a node (n) is the number of ‘edges on the path from the root node to (n). For example, the level of node 4 in Fig. 13 is three. 12. Height: It is equal to the maximum level of any node in the tree. The height of the tree in Fig. 13 is three, Types of Binary Trees Binary trees are of the following types: 1. Strictly binary tree: Figure 14 shows the Strictly binary tree. It has the following properties: © If in a tree, non-leaf nodes have exactly two children then that tree is called strictly binary tree. «In strictly binary tree, every leaf nodes have degree 0 and every non-leaf nodes have degree 2. © Astrictly binary tree with nleafnodes has (2n— 1) total number of nodes. Thus, strictly binary tree always have odd number of nodes. » ‘ 2, Complete the completeFigure 18 |, Gemplete bane) toe - Almost complete binary tree: A binary tree of depth d is an almost complete binary tree (see Fig. 16) if ‘+ Bach leaf in the tree is at either level d or level a-1. + For any node ny in the tree with a right descen- dant at level d. all the left descendants of ny, that are leaves are also at level d. + An almost complete binary tree with N leaves that is not strictly binary has 2’ nodes, Tree Applications "The following are common a0 2. 8 4. Manipulate hierarehiei Make information easy Manipulate sorted |ees 149) Va=( hive leftmost subtree starting at the ——(c, ds (ee), (ah e)} b, 6, de} and B= (a, B), (a &), (a), (by Os i bcos stata ee Following are the basic terms used for graphs (see Fig. 19): 1. Undirected graphs: It consists of a set V of vertices and a set E of edges, each of them connecting to two different vertices, 2. Directed graph: In this, edges provide a connection from one vertex to another, but not necessarily in the opposite direction 8, Adjacent vertices: Connected by an edge. 4, Degree of a vertex: It is defined as the number of adjacent vertices 5. Path: It is defined as a sequence of vertices such that consecutive vertices are adjacent 6. Simple path: Path with no repeated vertex. 7. Cycle: A simple path, where the first and last vertex are same 8. Connected graph: When any two vertices are connected by some path. (Consider the given tree and find the inor= » stove tees ann 9. Subgraph: When a subset of vertices and edges traversal of the above tree will output: aap ast 3.4, 5, 6,7, 9, 17, 20, 22 10. Tree: A connected graph without cycles. Traversal: 11. Balanced binary tree: A binary tree in which no leaf is at much greater depth than any other leaf. traversal of the above tree will output: MOOR talanced! binary search trees are red- 9,4, 8, 6,5, 7, 17, 22, 20 lack trees, AVL trees, etc. Examples of non-binary a balanced search trees are B trees, B+ trees, ete. raversal: it traversal of the above tree will output: be Traversing a Graph 3,5, 7, 6, 4, 20, 22, 17, 9 ‘There are two types of traversal techniques for graphs B th first traversal: It is a level-by-level ‘of an ordered tree. It starts with vertex « raverses to neighbours of vertex i, then (V, 8) is composed of V (set of vertices) a cfivectslc cote lis), wher edges connec the ieee? of vertex fad eo oa. Te see Fig, 18, Awooh G= (Vv, m), Figure Aisconneeted and tree graphs,——— fabficegdh} (xi)Choose the vertex with small path from source to the chosen same for other vertices. Eeeeees 158 Spanning Tree A spanning tree of a graph Gis a tree which contains all the vertices of the graph G. See Fig. 21. | | NZ je @—® Figure 21| Spanning tree of a graph. ‘Minimum Spanning Tree Minimum spanning tree in an undirected connected weighted graph is a spanning tree with minimum weight. Prim’s algorithm: 1. Begin with all the vertices. 2, Choose and draw any vertex. 3. Find the edge of least weight joining a drawn vertex to a vertex not currently drawn. Draw the weighted edge and the corresponding new vertex. “4. Repeat step 3 until all the vertices are connected, ‘then STOP. feees 157 Cn MF Chae Figure 22| AVL tree. AVL has the following properties: 1. Balance Factor of a Node: It is the difference in heights of its two subtrees (Fy ~ Jy.)- Balanced Node: Node with [BF |< 1;if [BF |>1, the node is unbalanced. 3. Balance Factor of Binary Trees: It corre- " sponds to the balance factor of its root node. * Tree is ‘left-heavy’ if BF < —1 x © Tree is ‘equal-height’ if BF = 0 Ho. b,c, de fg), V— U= fp + Tree is ‘right-heavy’ if BF > +1 ‘Complete* Single-Right rotation: Rotor Pig. 24 Original tree Single-Right rotation @ X, Xn Ys Ziy Zs Zn Figure 24| Single Right rotation © Double left-right rotation: Refer Fig. 25. Original tree _ After Single-Left rotation _ Double Left-Right rotatIMPORTANT FORMULAS 189) equal to the root element. This binary heap (see Fig: 27) is known as min-heap. In addition, if all the elements are = son ty be Hinary heap if all the Jesser or equal tothe root element, this is called maxc-heap. hy are either greater than or 1. The height of a binary heap is O(log n). The runtime of deleteMin, insert and remove is O(log n). 2. The root node is the highest priority element. To remove the highest/lowest priority object (root), heap data structure is preferred. Priority queue is the best example to be implemented. 3. The running time of heap sort is O(nlog 7). Figure 27 | Binary heap. IMPORTANT FORMULAS Issertion/Deletion in array: O(n) where Ib is the'lower bound of array and ub is the ‘Split /Merge in array: O(n) Hime complexity of Push(): O(1) ‘Time complexity of Pop(): O(1) ‘Time complexity of Enqueue(): O(1) ‘Time complexity of Dequene(): O(1) ‘Number of elements in queue = rear —front +1, if rear = front rear —front +n, otherwiseEnqueue(): O(1) exity of Dequene(): O(1) elements in queue if rear = front front +n, otherwise x of nodes in full binary tree: 2**!—1 Number of leaf nodes in full binary tree: 2* Number of waste pointers in complete binary tree of nodes: n+ 1 Complexity of inorder, preorder and postorder tree al: O(n) trave im number of moves for tower of Hanoi: i Number of unique binary trees: gat n+1 One-dimensional arrays Tn one dimension, an array ‘A’ is di as: Alb... ... ub] 15. BU aa ey where Ib is the'lower bound of array and ub is the upper bound of array Suppose we want to calculate the ith element address, then Address (arr{)) = BA + (i= Ib) * where BA is the base address of array and Ib is the lower bound of array and ¢ is the size of each element ‘Two-dimensional arrays In two dimensions, an array ‘A’ is declared as: Ally... ... ubylby «.. .-- ubs] where 1b, is the lower bound for row, Ib, is the lower bound for column, ub; is the upper bound for row and tby is the upper bound for column. Row major order ‘Address (arr{j{j) = BA + [i - Ib) * Ne + (Gj Ib,)] *¢ Column major order ‘Address (arr[il[j]) = BA + [j ~ Ib,) * Nr+ (i- Ib) te where BA is the base address, Nr is the number of rows = (Iby — Iby + 1), and Ne is the number of cohumns = (ub — ub, +1).
You might also like
Coding 3
PDF
No ratings yet
Coding 3
127 pages
Chapter 1 - Introduction To Computer Programing
PDF
No ratings yet
Chapter 1 - Introduction To Computer Programing
66 pages
Final Programming in C
PDF
No ratings yet
Final Programming in C
325 pages
Algorithm 1 & 2 - by MR Benabderrezak
PDF
No ratings yet
Algorithm 1 & 2 - by MR Benabderrezak
165 pages
C Programming Lecture Notes Final
PDF
No ratings yet
C Programming Lecture Notes Final
520 pages
C Programming Module 1 C Programming Basic
PDF
100% (1)
C Programming Module 1 C Programming Basic
33 pages
C & C++ Lecture by Earul Sir
PDF
No ratings yet
C & C++ Lecture by Earul Sir
65 pages
Programming in C
PDF
No ratings yet
Programming in C
689 pages
Computer Programming
PDF
No ratings yet
Computer Programming
53 pages
CS3 Tutorial
PDF
No ratings yet
CS3 Tutorial
390 pages
C and CPP Complete Notes Img Oct 2023
PDF
No ratings yet
C and CPP Complete Notes Img Oct 2023
19 pages
C Prog Handouts
PDF
100% (1)
C Prog Handouts
16 pages
C Language Overview
PDF
No ratings yet
C Language Overview
35 pages
Documentation: Preprocessor
PDF
No ratings yet
Documentation: Preprocessor
7 pages
Computer Programming
PDF
No ratings yet
Computer Programming
53 pages
Lecture 2
PDF
No ratings yet
Lecture 2
40 pages
Unit 1 INTRODUCTION
PDF
No ratings yet
Unit 1 INTRODUCTION
62 pages
C Demo
PDF
No ratings yet
C Demo
197 pages
Isp Ques Ans
PDF
No ratings yet
Isp Ques Ans
15 pages
0 All Slides PFIEV2017
PDF
No ratings yet
0 All Slides PFIEV2017
768 pages
Presentation 11677 Content Document 20231014034322PM
PDF
No ratings yet
Presentation 11677 Content Document 20231014034322PM
77 pages
Computer Programming
PDF
No ratings yet
Computer Programming
9 pages
C++ Semester1
PDF
No ratings yet
C++ Semester1
59 pages
Programming in C
PDF
No ratings yet
Programming in C
167 pages
1 Introduction
PDF
No ratings yet
1 Introduction
62 pages
Studentnotes C
PDF
No ratings yet
Studentnotes C
29 pages
Lect-03-Variables and Datatypes
PDF
No ratings yet
Lect-03-Variables and Datatypes
31 pages
PPS - Unit 3
PDF
No ratings yet
PPS - Unit 3
98 pages
CTSD Workshop Content
PDF
No ratings yet
CTSD Workshop Content
50 pages
C Programming LECTURE (Revised) - Nov-2017
PDF
No ratings yet
C Programming LECTURE (Revised) - Nov-2017
15 pages
1 Programming in C
PDF
33% (3)
1 Programming in C
554 pages
CPL Program
PDF
No ratings yet
CPL Program
5 pages
C and C++
PDF
No ratings yet
C and C++
42 pages
Crash Preparation Bootcamp
PDF
No ratings yet
Crash Preparation Bootcamp
11 pages
PF Lecture 1&2
PDF
No ratings yet
PF Lecture 1&2
43 pages
CP Lec Finals Reviewer
PDF
No ratings yet
CP Lec Finals Reviewer
15 pages
Programming and Data Structures
PDF
No ratings yet
Programming and Data Structures
74 pages
C Programming
PDF
No ratings yet
C Programming
52 pages
C Solution 2005
PDF
No ratings yet
C Solution 2005
15 pages
Foc Unit-5
PDF
No ratings yet
Foc Unit-5
12 pages
Programming in C and C++: Dr. M. Babul Islam
PDF
No ratings yet
Programming in C and C++: Dr. M. Babul Islam
35 pages
C Language
PDF
No ratings yet
C Language
72 pages
Pic 1 To 3 Chaptersslide Shows 1-59
PDF
100% (1)
Pic 1 To 3 Chaptersslide Shows 1-59
59 pages
Introduction To C
PDF
No ratings yet
Introduction To C
14 pages
C Syllabus
PDF
No ratings yet
C Syllabus
7 pages
C Basopr Ref
PDF
No ratings yet
C Basopr Ref
35 pages
Brief Introduction To C
PDF
No ratings yet
Brief Introduction To C
8 pages
C Programming I - Karl W Broman
PDF
No ratings yet
C Programming I - Karl W Broman
22 pages
Software Development Tutorial (BCS-IT) WEEK-1,2,3,4,5 Algorithm
PDF
No ratings yet
Software Development Tutorial (BCS-IT) WEEK-1,2,3,4,5 Algorithm
8 pages
Programming in C Unit-1
PDF
No ratings yet
Programming in C Unit-1
7 pages
Lesson Plan in Tle Computer Hardware and
PDF
No ratings yet
Lesson Plan in Tle Computer Hardware and
9 pages
Are The Names Used To Represent:, ,,, And: Keywords Cannot Be Used For
PDF
No ratings yet
Are The Names Used To Represent:, ,,, And: Keywords Cannot Be Used For
9 pages
C Language Tutorial
PDF
No ratings yet
C Language Tutorial
15 pages
Rai Business School: Assignment
PDF
No ratings yet
Rai Business School: Assignment
10 pages
Programming in C: by Vishal Vanaki
PDF
No ratings yet
Programming in C: by Vishal Vanaki
35 pages
Programming Lecture 1 2009
PDF
No ratings yet
Programming Lecture 1 2009
9 pages