Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Graphs: Graphs are discrete data structures consisting of vertices and edges that connect these vertices. A graph is depicted in diagrammatic form as a set of dots for the vertices, joined by lines or curves for the edges.
Adjacent vertices of Graph: The vertices which are connected together by edges are called adjacent to each other. In the following graph.
Simple Cycle:
A cycle with no repeated vertices or edges aside from the necessary repetition of the start and end vertex
is a simple cycle.
Connected Graph: A graph is said to be connected if there is a path between every pair of distinct vertices of the graph.
Disconnected Graph: A graph is said to be disconnected if there is not a path between every pair of distinct vertices of the graph.
Multigraphs:
Graphs that have multiple edges connecting the same vertices are called multigraphs.
Loop: Whenever an edge starts from itself and ends on itself, then that edges is called a loop.
Directed Graph: A graph in which edges have some orientation, i.e., they are ordered pairs of vertices.
Directed weighted Graph: An oriented graph with weighted edges is known as a directed weighted Graph.
Undirected Graphs:
A graph in which edges have no orientation, i.e., they are not ordered pairs of vertices.
Adjacency Matrix: An adjacency matrix is a means of representing which vertices (or nodes) of a graph are adjacent to which other vertices.
Adjacency List: An adjacency list is the representation of all edges or arcs in a graph as a list that specifies the vertices that are adjacent to each vertex of the graph.
Operations on Binary Trees: 1. Iteration: There are several common orders in which the nodes can be visited, and each has useful properties that are exploited in algorithms based on binary trees: a) Pre-Order: Root first, children after. b) Post-Order: Children first, root after. c) In-Order: Left child, root, right child.
2. Insertion: Nodes can be inserted into binary trees in between two other nodes or added after an external node. In binary trees, a node that is inserted is specified as to which child it is. 3. Deletion: Deletion is the process whereby a node is removed from the tree. Only certain nodes in a binary tree can be removed.
ADT: createBT(root Item, left Tree, right Tree); setRootItem(new Item); attachLeft(new Item); attachRight(new Item; attachLeftSubTree(left Tree); attachRightSubTree(right Tree);
detachSubTree(left Tree); detachSubTree(right Tree); Operations on Binary Search Trees: 1. Searching: Searching a binary search tree for a specific value can be a recursive or iterative process. This explanation covers a recursive method. 2. Insertion: Insertion begins as a search would begin; if the root is not equal to the value, we search the left or right sub trees as before. Eventually, we will reach an external node and add the value as its right or left child, depending on the node's value.
3. Deletion: There are some possible cases to consider: a) Deleting a leaf (node with no children): Deleting a leaf is easy, as we can simply remove it from the tree. b) Deleting a node with one child: Remove the node and replace it with its child. 4. Sort: A binary search tree can be used to implement a simple but efficient sorting algorithm. 5. Traversal: Once the binary search tree has been created, its elements can be retrieved by three orders; in-order, preorder and post-order. 6. Make Empty: This operation is mainly for initialization. 7. Find Min and Find Max: These operations are used to find the maximum and minimum of the tree.
BALANCED TREES:
A self-balancing (or height-balanced) binary search tree is any node based binary search tree that automatically keeps its height (number of levels below the root) small in the face of arbitrary item insertions and deletions.
Operations on Balanced Trees: 1. Creating: The B-Tree-Create operation creates an empty b-tree by allocating a new root node that has no keys and is a leaf node. 2. Searching: The search operation on a b-tree is analogous to a search on a binary tree. Instead of choosing between a left and a right child as in a binary tree, a b-tree search must make an n-way choice. The correct child is chosen by performing a linear search of the values in the node. 3. Insertion: To perform an insertion on a b-tree, the appropriate node for the key must be located using an algorithm similar to B-Tree-Search. 4. Deletion: Deletion of a key from a b-tree is possible; however, special care must be taken to ensure that the properties of a b-tree are maintained.
N ARY TREES:
Trees in which all of the nodes of the tree are required to have exactly the same degree. It means that each node will have equal number of children or no children. A 3 ary tree
A 9 ary tree
int main (){ int a[5]={5,8,1,11,3}; int SK Index, SK=-1; cout<<"please enter the value for Search"; cin>>sk; for(i=0; i<5; i++){ if a[i]==SK; SKiIndex=i; break; } } if(SKIndex==-1) cout<<"value not found"; else cout<<a[SkIndex]; return -1;}
int binarySearch(const int list[], int listLength, int searchItem){ int first = 0; int last = listLength 1; int mid; bool found = false; while (first <= last && !found){ mid = (first + last) / 2; if (list[mid] == searchItem) found =true; else if (last[mid] > searchItem) last = mid 1; else first = mid + 1;} if (found) return mid; else return -1;}
IMPLEMENTATION OF CLASSES: Triangle Class: class Triangle { private: float base; float height; public: Triangle(); void set Base (float); float get Base (); void set Height (float); float get Height(); float Area; float Parimeter; bool isTriangle(float,float,float) void gettype(float,float,float) }; #include<iostream.h> #include"triangle.h" #include<iostream> Triangle:: Triangle() { base=height=0.0 } void Triangle :: set Base(float)
{ base=b; } float Triangle:: get Base() { return base; } void Triangle :: set Height(float) { height=h; } float triangle:: get height() { return height; } float Triangle:: Area() { return 1\2(Base * Height); } float Triangle:: Parimeter() { return a+b+c; } bool Triangle:: IsTriangle(float,float,float) { if(a+b>c||c+b>a||a+c>b)
{ cout<<"The Triangle is Valid"; else cout<<"InValid Triangle"; } void Triangle::gettype(float,float,float) { if(a==b&&b==c) cout<<"Equlaterial Triangle"; else if(a==b||b==c||a==c)) cout<<"Isosceles Triangle"; else cout<<"Scalene Triangle"; } include<iostream.h> triangle<iostream.h>
int main() { Triangle T; float h,b,area,parimeter,a,b,c; cout<<"Enter Height of Triangle"; cin>>h; T.set height(h); cout<<"Enter Base of Triangle"; cin>>w;
T.set base(b); cout<<"Enter three sides of Triangle"; cin>>a,b,c; area= T.area(); primeter= T.parimeter(); IsTriangle=T.isTriangle; gettype=T.gettype; cout<<"Area="<<T.area()endl; cout<<"Parimeter"<<T.preimeter()endl; cout<<"Is Triangle..."<<T.isTriangle()endl; couy<<"Type Of Triangle"<<T.gettype()endl; getch(); return0; }
Rectangular class: class Rectangle { private: float lenyh; float width; public: Rectangle(); void set Length (float); float get Length (); void set Width (float); float get Width(); float Area; float Parimeter; }; class Rectangle { private: float lenyh; float width; public: Rectangle(); void set Length (float); float get Length (); void set Width (float); float get Width();
int main() { Rectangle R; float l,w,area,parimeter; cout<<"Enter length of Rectangle"; cin>>l; R.set Length(l); cout<<"Enter width of Rectangle"; cin>>w; R.set width(w); area= R.area(); primeter= R.parimeter(); cout<<"Area="<<R.area()endl; cout<<"Parimeter"<<R.preimeter()endl; getch(); return0; }
//class name
//addition function
//subtraction function
//multiplicatrion function
//division function
//end of class arithmatic //starting of main function //declaration of object //decleration of local variables
getch(); return 0; }
class complex{ { private: int x, y; public: void setx(int a){ x=a; } //sets value for 1st private variable //2 private variables declared
//addition function
//subtraction function
cin>>d; cout<<endl;
C1.setvalue(a); C1.setvalue(b);
C2.setvalue(c); C2.setvalue(d); //printing values cout<<"The addition of two complex numbers = " <<C1.add()<<" + "<<C2.add()<<" i '<<endl;
getch(); return 0; }
//class start
int main(){ circle C; int x, y, rad; cout<<"Enter x co-ordinate = "; cin>>x; cout<<endl;
rad = sqrt((x*x)+(y*y));
C.setr(rad); C.print();
getch(); return 0; }