Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 38
M way Search Tree
M way search Tree
• A tree with maximum of m children is known as M way search Tree. • M way search tree are generalised versions of binary search trees. • An m way search tree ,tree may be an empty tree if tree is non empty, it satisfies the following properties. • Each nodes has at most m child nodes. • If a node has k child nodes where k<=m then the node can have only (k-1) keys k1,k2….. Ex. Of m way search tree • 10 60 100 200 40 120 80 90 70 170 180 190 Types of binary trees • Complete binary trees- a complete binary tree is a type of binary tree which satisfies to Properties: • A. First every level in a complete binary tree except the last one must be completely filled. • Second, all the notes in the complete binary tree must appear left as much as possible. • In a complete binary tree the number of notes at level n is Nodes. • Also the total number of nodes in a complete binary tree of depth d is equal to the sum of all nodes present at each level between 0 and d. Extended binary tree • Extended binary trees are also known as 2T trees or strict binary tree. • A binary tree is said to be an extended binary tree if and only if every node in the tree has either zero children or two children. • In an extended binary tree nodes having two children were known as internal nodes. If Strictly binary tree has N leaf nodes then the number of internal notes will be N-1. • Notes having no children are known as external nodes. Perfect binary tree • A perfect binary tree is a type of binary tree in which every internal node has exactly two child nodes and all the leaf nodes are the same level all the internal nodes have a degree of two. B Tree • The keys and data can be stored in both the Internal and leaf nodes. • Leaf node cannot store using link list. • A B- tree is a self balanced search tree with multiple keys in every node and more than 2 Children for every node. • The leaf nodes store pointers rather than actual records. • These tree waste space. • Searching becomes difficult in B trees as data cannot be found in leaf node. B tree • All leaf nodes are at the bottom level or at the same level. • Every node in a B tree can have at most M children. • The root node can have at least two children if it is not a leaf node and it can obviously have no children if it is a leaf node. • Each node in a B tree can have at least m/2 children except the root node and the leaf node. • Each leaf node must contain at least ceil (m/2-1) keys. Ex of B tree • 30,45,75,10,80,40,90,6,15,22,33,9,5,12,77,43,37,3,11,49 B Trees • The data and keys can only be stored in the leaf node. • At the bottom leaf nodes are linked. • B tree is an n array tree with a variable but often large number of children per node.A B tree consists of a root, internal nodes and leaf. the root maybe either a leaf or a node with two or more children. • The leaf nodes of the tree store actual records rather their pointers. • The records can be accessed either sequently or randomly. • These trees do not waste space. • Searching is easy because all data is found in leaf nodes. • Similarly deletion is also very simple as it will only take place in the leaf nodes. B Graph • A graph is a collection of some vertices (nodes) and edges that connect these vertices. • A graph G can be defined as an ordered set of vertices and edges (V,E) ,where V(G) represents the set of vertices and E(G) represents the set of edges that connect these vertices.V(G) ={A,B,C,D,P,Q} represents the set of vertices. • And E(G)={(A,B),(B,D),(D,C),(C,A),(C,Q),(Q,D),(A,P) ,(P,C) } represents the set of edges. Definitions • Degree of vertex/node-The degree of a node is the total number of edges incident to that particular node. Here the degree of the node B is 3 ,as three edges are incident of the node B.A vertex of degree one is called pendant vertex. • In-degree of a node- The in- degree of a node is equal to the number of edges arriving at a particular node. • Out degree of a node-The out degree is equal to the number of edges leaving that particular node. • Isolated node/vertex-A node having zero edges is known as the isolated node. The degree of such a node is zero. • Pendant node/vertex- A node having one edge is known as pendant node. The degree of such a node is one. Loop • If an edge has a starting and ending point at the same node, that is e=(A,A) ,then it is known as loop. • Multi Graph –a graph G(V,E) is known as a multi graph if it contains either a loop, parallel edges or both. • Cycle- It is a path containing one or more edges which starts from a particular node and also terminates at the same node. • Cyclic graph-A graph which has cycle in it is known as a cyclic graph. • Acyclic graph- A graph without any cycle is known as an acyclic graph. Graph Representation • Sequential representation of graph. Using Adjacency Matrix • Linked representation of graph using Adjacency list • Adjacency matrix representation- An adjacency matrix is used to represent the information of the nodes which are adjacent to one another.The two nodes will only be adjacent when there is an edge connecting those nodes. • An adjacency matrix representation of a graph cannot contain information of parallel edges. Adjacency list representation • The adjacency matrix representation has some major drawbacks. First ,it was very difficult to insert and delete the nodes in/from the graph as the size of the matrix needs to be changed accordingly, which is a very time consuming process. • In Adjacency list representation , every node is linked to its list of all the other nodes which are adjacent to it.Adjacency list representation makes it easier to add or delete nodes. Graph traversal Techniques • Breadth First Search(BFS) • Depth First Search(DFS) • Breadth first search is a traversal technique that uses the queue as an auxiliary data structure for traversing all member nodes of a graph. • In this technique ,first we will select any node in the graph as a starting node, and then we will take all the nodes adjacent to the starting node. • FIFO(queue) • Work on shallowest nodes • Complete • Time complexity O(V+E) Ex of BFS Depth First Search • Depth first search is another traversal technique that the stack as an auxiliary data structure for traversing all the member nodes of a graph. • In this technique , we first select any node in the graph as a starting node, and then we travel along a path which begins from the starting node. • We will visit the adjacent node of the starting node, and again the adjacent node of the previous node, and so on. • LIFO(stack) • Work on deepest node • Incomplete • Time complexity O(V+E) DFS Hashing • Hashing is the process of mapping keys to their appropriate locations in the hash table. It is the most effective technique of searching the values in an array or in a hash table. • Hash Table: • A Hash table is a data structure which supports one of the efficient searching techniques, that is Hashing. • A hash table is an array in which the data is accessed through a special index called a key. • In a hash table, keys are mapped to the Array positions by a hash function. • A hash function is a function, or we can say that it is a mathematical formula, which when applied to a key, produces an integer which is used as an index to find a key in the hash table. • Thus, a value stored Ina hash table can be searched in O(1) time with the help of a hash function. Hash functions • A hash function is a mathematical formula which when applied to a key ,Produces an integer which is used as an index to find a key in the hash table. • Characteristics of the hash function • There are four main characteristics of hash functions which are: • The hash function uses all the input data. • The hash function must generate different hash values. • The hash value is fully determined by the data being hashed. • The hash function must distribute the keys uniformly across the entire hash table. Different types of hash functions • Division method- • In the division method, a key K is mapped into one of the m slots by taking the remainder of k divided by m. In simple terms, we can say that this method divides an integer,say x by m and then uses the reminders so obtained. It is the simplest method of hashing.The hash function is given by: • h(k)=k mod m • h(k) Address • K key • m no. Of slots Mid Square method • In the mid square method, we will calculate the square of the given key. After getting the number, we will extract some digits from the middle of that number as an address. • For example , if key k=5025 • Then k. =25250625 • Thus h(5025)=50 Folding method • In the folding method, we will break the key into pieces such that each piece has the same number of digits except the last one, which may have fewer digits as compare to other pieces. Now, these individual pieces are added. We will ignore the carry if it exists. Hence, the hash value is formed. • For example: if m=100 and the key k=12345678, then the indices will vary from 0 to 99 and thus, each piece of the key must have two digits. Therefore, the given key will be broken into four pieces, that is 12,34,56 and 78.Now we will add all these ,that is 12+34+56+78=180 • Thus, the hash value will be 80 (ignore the last carry) Load factor • Load factor is defined as m/n where n is the total size of the hash table and m is the preferred number of entries which can be inserted before a increment in size of the underlying data structure is required.