|
|
|
|
|
Data structures and algorithms (Binary Tree)
|
|
|
Data structures and algorithms A major area of the computer is the storage of data for efficient search and retrieval, the main memory of a computer is linear consisting of a sequence of memory cells that are numbered 0,1,2,3… in order. However one of the simplest forms of data structure is the one-dimensional or linear array accessed by using the element numbers. Data items such as a list of names are usually stored in arrays and efficient methods are sought to handle the data. If the list is long it would be an advantage to sort the list in-order to greatly reduce the time the user spends waiting for the retrieval of the search criteria compared to a search on an unsorted list. Several structures exist for the storage of data within the computers memory as listed above a one-dimensional array, two-dimensional array, stacks, queue (circular), linked lists, doubly linked lists, binary tree and heaps are just a few techniques in which algorithms have been based on the speed in which data is searched and stored in the computers memory. A heap is a saturated binary tree structure; the two branches of each node lead to nodes with lower numerical values. In the example below, the items in the heap are numbers, and the heap is maintained with larger numbers floating to the top. Each item is a member of an array, and we make use of array numbers in order to deal quickly and effectively with the heap. A heap works on the principle that items are interrelated: item n in the heap is a larger number than item 2n and item 2n+1. When all the items are in this relationship you have a valid heap, each item is like a stone resting firmly on two under it. Using weights could help use balance the tree for each specific item the calculation for each item in the tree will be its weight times its level number with the root being at level one. The binary tree is used for the basis of this assignment has the methods for searching and storage is more efficient than the one-dimensional array. Pointers are used to link together the memory addresses (A pointer is a variable that hols the memory address) bellow is a schematic representation were each location is equal to One byte. Declared variable called of type unsigned integer (age). 10110101 01110110 11110110 11101110 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 The unsigned integer age = 4bytes = 32 bits The following C# shows the key word struct which identifies the beginning of a structure definition and must be followed by the structure name, or tag. Fig 1 Struct tree { Char f_name ; Int age; } within the braces following the structure name is a list of the structure member variables. Fig 2 Struct tree { Char f_name; Int age; } Tree_Node; The example in fig2 shows that I have declared an instance of the tree structure The data structure below is the first part of the assignment, which sets up a structure called carreg. typedef struct carreg { char reg_num[7+1]; char Surname[25+1]; char Forename[20+1]; char Age_of_Owner[3+1]; char Make[20+1]; char Model[15+1]; char Colour[15+1]; char cubic_capacity[5+1]; struct carreg *next; // pointer ("link") to the next node int counter; }list_entry; To enable us to manipulate the structure members the following notation is used (->); this sign is used as shown in fig3. Fig3 Cout << list_entry->reg_num; print(display on screen) the reg_num. The following source code has been commented, to show what is happening within the procedure. //Assignment 1 - Andrew Trot man //Attempt at a binary tree structure #include #include #include #include #include #include typedef struct carreg { char reg_num[7+1]; char Surname[25+1]; char Forename[20+1]; char Age_of_Owner[3+1]; char Make[20+1]; char Model[15+1]; char Colour[15+1]; char cubic_capacity[5+1]; struct carreg *next; // pointer ("link") to the next node int counter; }list_entry; struct carreg *start=NULL; //pointer to first entry struct carreg *last=NULL; //pointer to last entry struct carreg *previous=NULL; //pointer to a previous item struct carreg *find(char *); void display_by_Reg(struct carreg *,int); void display_all(int); int enterdata(int); void Error(); int menu_select(void); void remove(struct carreg **,struct carreg **); void Storeage(struct carreg *,struct carreg **,struct carreg **); void show_all(struct carreg *,int); void view_by_Reg_list(int); void search(struct carreg **,struct carreg **); int global_counter; ///////////////////////////////////////////////////////////////////////////// void main(void) { int count =1; for(;;)//menu loop { switch(menu_select()) { case 1: enterdata(count); count++; global_counter=count; break; case 2:remove(&start,&last); global_counter--; break; case 3:view_by_Reg_list(global_counter); break; case 4:search(&start,&last); break; case 5:display_all(global_counter); break; case 6:exit(0); default : clrscr();cout << "Incorect Entry "; } clrscr(); } } //////////////////////////////////////////////////////////////////////////////// void display_by_Reg(struct carreg *info , int file_count) { for(file_count; file_count <= 0;file_count--) printf(" --------------------------------------------------------------- "); printf("%3s| %-20s |%-20s "," Entry No.
|
|
|
|
Still Can't Find What Your Looking For? Then Try a Essay Search! |