Sei sulla pagina 1di 25

DEFINITIONS:

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 Path: A path with no repeated vertices is called a simple path.

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.

Undirected weighted Graph:


An unoriented graph with weighted edges is known as a undirected weighted Graph.

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.

REPORT: BINARY TREES:


A binary tree is a tree in which no node can have more than two children. It has many important uses not associated with searching. Binary trees are used to implement binary search trees and binary heaps.

ADT: createBt(); createBT(root); makeEmpty(); isEmpty(); getRoot Item();

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.

BINARY SEARCH TREES:


A binary search tree (BST), which may sometimes also be called an ordered or sorted binary tree, is a node based binary tree data structure which has the following properties: 1. The left sub tree of a node contains only nodes with keys less than the node's key. 2. The right sub tree of a node contains only nodes with keys greater than the node's key. 3. Both the left and right sub trees must also be binary search trees.

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

COMPREHENSIVE NOTES: Code of Linear Search:

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;}

Code for Binary Search:

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();

float Area; float Parimeter; }; #include<iostream.h> rectangle<iostream.h>

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; }

Arithmetic Class: #include<iostream.h>

#include<conio.h> using namespace std;

class arithmatic { private: int x, y; public: void setx(int a){ x=a; }

//class name

//2 private variables declared

//sets value for 1st private variable

void sety(int b){ y=b; }

//sets value for 2nd private variable

int getx(){ return x; }

//gets value for 1st private variable

int gety(){ return y; }

//gets value for 2nd private variable

int add(){ return x + y;

//addition function

int sub(){ return x - y; }

//subtraction function

int mul(){ return x * y; }

//multiplicatrion function

double div(){ return x / y; } }; int main(){ arithmatic A1; int a, b;

//division function

//end of class arithmatic //starting of main function //declaration of object //decleration of local variables

cout<<"Enter First number = "; cin>>a; cout<<endl;

//input 1st number

cout<<"Enter Second number = "; cin>>b; cout<<endl;

//input 2nd number

A1.setvalue(a); A1.setvalue(b); cout<<"Sum cout<<"Subtraction

//setting value and //calling of all functions = "<<A1.add(); = "<<A1.sub();

cout<<"Multiplication = "<<A1.mul(); cout<<"Division = "<<A1.div();

getch(); return 0; }

complex number class: #include<iostream.h> #include<conio.h> using namespace std;

class complex{ { private: int x, y; public: void setx(int a){ x=a; } //sets value for 1st private variable //2 private variables declared

void sety(int b){ y=b; }

//sets value for 2nd private variable

int getx(){ return x; }

//gets value for 1st private variable

int gety(){ return y; }

//gets value for 2nd private variable

int add(){ return x + y; }

//addition function

int sub(){ return x - y; } };

//subtraction function

//end of class complex

int main(){ complex C1, C2; int a, b, c, d;

//starting of main function //declaration of objects //decleration of local variables

cout<<"Enter real part of 1st number = "; cin>>a; cout<<endl;

//input 1st number

cout<<"Enter imaginary part of 1st number = "; cin>>c; cout<<endl;

cout<<"Enter real part of 2nd number = "; cin>>b; cout<<endl;

//input 2nd number

cout<<"Enter imaginary part of 2nd number = ";

cin>>d; cout<<endl;

C1.setvalue(a); C1.setvalue(b);

//set all values of private fields

C2.setvalue(c); C2.setvalue(d); //printing values cout<<"The addition of two complex numbers = " <<C1.add()<<" + "<<C2.add()<<" i '<<endl;

cout<<"The subtraction of two complex numbers = " <<C1.sub()<<" - "<<C2.sub()<<" i '<<endl;

getch(); return 0; }

Circle Class: #include<iostream.h> #include<conio.h> #include<math.h> using namespace std;

class circle{ private: int r; public: void setr(int a){ r=a; }

//class start

//private variable declared

int gertr(){ return r; }

double circum(){ return 2 * 3.14 * r; }

double area(){ return 3.14 * r * r; } void print(){

cout<<"Circumference of circle is = "<<circum()<<endl; cout<<"Area of circle = "<<area()<<endl; } };

int main(){ circle C; int x, y, rad; cout<<"Enter x co-ordinate = "; cin>>x; cout<<endl;

cout<<"Enter y co-ordinate = "; cin>>y; cout<<endl;

rad = sqrt((x*x)+(y*y));

C.setr(rad); C.print();

getch(); return 0; }

Potrebbero piacerti anche