Anda di halaman 1dari 2

4.3.1 Binary Trees The simplest form of tree is a binary tree.

A binary tree consists of a node (called the root node) and left and right sub-trees. Both the sub-trees are themselves binary trees. You now have a recursively defined data structure. (It is also possible to defin e a list recursively: can you see how?)

A binary tree The nodes at the lowest levels of the tree (the ones with no sub-trees) are call ed leaves. In an ordered binary tree, the keys of all the nodes in the left sub-tree are less than that of the root, the keys of all the nodes in the right sub-tree are greater than that of the roo t, the left and right sub-trees are themselves ordered binary trees. Data Structure The data structure for the tree implementation simply adds left and right pointe rs in place of the next pointer of the linked list implementation. [Load the tre e struct.] The AddToCollection method is, naturally, recursive. [ Load the AddToCollection method.] Similarly, the FindInCollection method is recursive. [ Load the FindInCollection method.] Analysis Complete Trees Before we look at more general cases, let's make the optimistic assumption that we've managed to fill our tree neatly, ie that each leaf is the same 'distance' from the root. A complete tree This forms a complete tree, whose height is defined as the numbe r of links from the root to the deepest leaf.

Graph A graph data structure consists mainly of a finite (and possibly mutable) set of ordered pairs, called edges or arcs, of certain entities called nodes or vertic es. As in mathematics, an edge (x,y) is said to point or go from x to y. The nod es may be part of the graph structure, or may be external entities represented b y integer indices or references. A graph data structure may also associate to each edge some edge value, such as a symbolic label or a numeric attribute (cost, capacity, length, etc.).

Hash Function:

A Hash Function is a Unary Function that is used by Hashed Associative Container s: it maps its argument to a result of type size_t. A Hash Function must be dete rministic and stateless. That is, the return value must depend only on the argum ent, and equal arguments must yield equal results. Hash functions are mostly used in hash tables, to quickly locate a data record ( for example, a dictionary definition) given its search key (the headword). Speci fically, the hash function is used to map the search key to the index of a slot in the table where the corresponding record is supposedly stored. Division Method: Choose the number n larger then the number n of the keys in K. The number n is u ssualy choosen to be a prime number or a number with a small number of divisors. This frequently minimize the number of collisions. The hash function H is defin ed by H(k)=k(mod m) or H(k) = (mod m)+1

Midsquare method: The key k is squared then the hash function H is defined by H(k)=I Where I is obtained by deleting digits from both ends of K2 we emphasize that th e same positions of K2 must be used for all of the keys.

Folding Method: The key K is partitioned into a number of parts k1, k2, .kr where each part except possibily the last has the same number of digits as the required address. Then the parts are edit together ignoring the last carry that is H(k) = k1+k2+ kr

Binary tree A binary tree is made of nodes, where each node contains a "left" pointer, a "ri ght" pointer, and a data element. The "root" pointer points to the topmost node in the tree. The left and right pointers recursively point to smaller "subtrees" on either side. A null pointer represents a binary tree with no elements -- the empty tree. The formal recursive definition is: a binary tree is either empty ( represented by a null pointer), or is made of a single node, where the left and right pointers (recursive definition ahead) each point to a binary tree.