1 / 30

DIGITAL SEARCH TREES

DIGITAL SEARCH TREES. Definition. A digital search tree is a binary tree in which each node contains one element. The element-to-node assignment is determined by the binary representation of the element keys.

viola
Download Presentation

DIGITAL SEARCH TREES

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. DIGITAL SEARCH TREES

  2. Definition • A digital search tree is a binary tree in which each node contains one element. • The element-to-node assignment is determined by the binary representation of the element keys.

  3. We number the bits in the binary representation of a key from left to right beginning at one. Ex: bit one of 1000 is 1, and bits two , three , four are 0. • All keys in the left subtree of a node at level I have bit i equal to zero whereas those in the right subtree of nodes at this level have bit i = 1.

  4. Digital Search Tree Assume fixed number of bits Not empty => Root contains one dictionary pair (any pair) All remaining pairs whose key begins with a 0 are in the left subtree. All remaining pairs whose key begins with a 1 are in the right subtree. Left and right subtrees are digital subtrees on remaining bits.

  5. This digital search tree contains the keys 1000,0010,1001,0001,1100,0000 1000 0010 1001 0001 1100 0000

  6. Example • Start with an empty digital search tree and insert a pair whose key is 0110 0110

  7. Now , insert a pair whose key is 0010 0110 0010

  8. Example • Now , insert a pair whose key is 1001 0110 1001 0010

  9. Example • Now insert a pair whose key is 1011 0110 0110 0010 1001 0010 1001 1011

  10. Example • Now , insert a pair whose key is 0000 0110 0110 0010 1001 0010 1001 0000 1011 1011

  11. Search and Insert • The digital search tree functions to search and insert are quite similar to the corresponding functions for binary search trees. • The essential difference is that the subtree to move to is determined by a bit in the search key rather than by the result of the comparison of the search key and the key in the current node.

  12. Try to build the digital search tree • A 00001 • S 10011 • E 00101 • R 10010 • C 00011 • H 01000 • I 01001 • N 01110 • G 00111 • X 11000 • M 01101 • P 10000

  13. Digital Search Tree A S E C X R H I G N P M

  14. Practical • When we dealing with very long keys, the cost of a key comparison is high. We can reduce the number of key comparisons to one by using a related structure called Patricia • We shall develop this structure in three steps.

  15. First, we introduce a structure called a binary trie. • Then we transform binary tries into compressed binary tries. • Finally, from compressed binary tries we obtain Patricia.

  16. Binary Tries • A binary trie is a binary tree that has two kinds of nodes: branch nodes and element nodes. • A branch node has the two data members LeftChild and RightChild. It has no data member. • An element node has the single data member data. • Branch nodes are used to build a binary tree search structure similar to that of a digital search tree. This leads to element nodes

  17. A six-element binary trie 1100 0010 0000 0001 1000 1001

  18. Compressed binary trie • The binary trie contains branch nodes whose degree is one. By adding another data member, BitNumber , to each branch node, we can eliminate all degree-one branch nodes from the trie. The BitNumber data member of a branch node gives the bit number of the key that is to be used at this node.

  19. Binary trie with degree-one nodes eliminated 1 3 2 4 4 0010 1100 1000 1001 0000 0001

  20. Patricia • Compressed binary tries may be represented using nodes of a single type. The new nodes, called augmented branch nodes, are the original branch nodes augmented by the data member data. The resulting structure is called Patricia and is obtained from a compressed binary trie in the following way:

  21. (1)Replace each branch node by an augmented branch node. • (2)Eliminate the element nodes. • (3)Store the data previously in the element node in the data data members of the augmented branch nodes. Since every nonempty compressed binary trie has one less branch node than it has element nodes, it is necessary to add one augmented branch node. This node is called the head node . The remaining structure is the left subtree of the head node. The head node has BitNumber equal to zero. Its right-child data member is not used. The assignment of data to augmented branch node is less than or equal to that in the parent of the element node that contained this data . • (4)Replace the original pointers to element nodes by pointers to the respective augmented branch nodes.

  22. Patricia 0 1 3 2 4 4 1100 0000 0010 1001 0001 1000

  23. Patricia typedef struct patricia_tree *patricia; struct patricia_tree { int bit_number; element data; patricia left_child, right_child; }; patricia root;

  24. Patricia Search Patricia search(patricia t, unsigned k) { /*search the Patricia tree t; return the last node y encountered; if k = y ->data.key, the key is in the tree */ Patricia p, y; If (!t) return NULL; /* empty tree*/ y=t->left_child; p=t; while (y->bit_number > p->bit_number){ p=y; y=(bit(k, y->bit_number)) ? y->right_child : y->left_child; } return y; }

  25. Patricia的Insert void insert (patricia *t, element x){ /* insert x into the Patricia tree *t */ patricia s, p, y, z; int i; if (!(*t)) { /* empty tree*/ *t = (patricia)malloc(sizeof(patricia_tree)); if (IS_FULL(*t)) { fprintf(stderr, “The memory is full\n”) ; exit(1); } (*t)->bit_number = 0 (*t)->data = x; (*t)->left_child = *t; } y = search(*t,x.key); if (x.key == y->data.key) { fprintf(stderr, “The key is in the tree. Insertion fails.\n”); exit(1);}

  26. /* find the first bit where x.key and y->data.key differ*/ for(i = 1; bit (x.key,i) == bit(y->data.key,i); i++ ); /* search tree using the first i-1 bits*/ s = (*t)->left_child; p = *t; while (s->bit_number > p->bit_number && s->bit_number < 1){ p = s; s = (bit(x.key,s->bit_number)) ? s->right_child : s->left_child;} /* add x as a child of p */ z = (patricia)malloc(sizeof(patricia_tree)); if (IS_FULL(z)) { fprintf(stderr, “The memory is full\n”); exit(1); } z->data = x; z->bit_number = i; z->left_child = (bit(x.key,i)) ? s: z; z->right_child = (bit(x.key,i)) ? z : s; if (s == p->left_child) p->left_child = z; else p->right_child = z;

  27. 0 0 t 0 1 1 4 (a)1000 inserted (b)0010 inserted (c)1001 inserted 1000 1000 0010 0010 1000 1001

  28. 0 0 1 1 2 3 2 4 4 (d)1100 inserted (e)0000 inserted 1000 1000 0010 0010 0000 1100 1100 1001 1001

  29. 0 1 3 2 4 4 (f)0001 inserted 1000 0010 0000 1100 0001 1001

  30. THE END

More Related