170 likes | 394 Views
9. 12. 7. 14. 10. 4. 3. 8. 13. Binary Trees. • A binary tree is a linked structure that allows for a binary search strategy. • A binary tree consists of a finite number of nodes (structured data objects).
E N D
9 12 7 14 10 4 3 8 13 Binary Trees • A binary tree is alinked structure that allows for a binary search strategy. • A binary tree consists of a finite number of nodes (structured data objects). • Each node can point to either 0, 1, or (at most) 2 different nodes (structures). • Assume we have the following array of random integers: If We wished to find an element on the list: • We could perform a sequential search (easy but slow) • We could sort and do a binary search (fast but difficult to sort and maintain) • A binary tree provides a compromise
9 12 7 14 10 4 3 8 13 9 Setting Up a Binary Tree Assume that each element on our list: Is a node in our tree. The structure of each node will be: struct treetemplate { int value; struct treetemplate *left, *right; }; Take 1 element from the list (e.g., the first one), and set it in as the first (root) node The first element is now the root of the tree
12622 - 12623 12624-12627 12628-12631 9 NULL NULL Assume that this element were assigned location 12622 in RAM: Field: value Field: left Field: right Notice that the pointer fields (left and right) initially point to nothing To insert the remaining elements from our array onto the list: 1. Starting at the root, determine if the element is larger or smaller 2. Assume that larger elements will go the right, and smaller elements will go to the left. 3. Continue with the remaining elements on the list putting them in place in the tree.
12 7 14 10 4 3 8 13 9 12720 - 12721 12622 - 12623 12624-12627 12722-12725 12628-12631 12726-12721 9 12 NULL NULL 12720 NULL 9 12 Now add the next (second) element into the tree: Since 12 is greater than 9, go to the right Root If the second element (structure) were assigned to address 12720, RAM might appear as:
7 14 10 4 3 8 13 9 12 12622 - 12623 12624-12627 12628-12631 12730 - 12731 12720 - 12721 12732-12735 12722-12725 12736-12739 12726-12729 9 7 12 NULL NULL 12730 NULL NULL 12720 9 7 12 Now add the next (third) element into the tree: Since 7 is less than 9, go to the left If the third element were assigned to address 12730, RAM might appear as:
14 10 4 3 8 13 9 7 12 12622 - 12623 12624-12627 12628-12631 12756 - 12757 12730 - 12731 12720 - 12721 12732-12735 12758-12761 12722-12725 12762-12765 12736-12739 12726-12729 7 14 12 9 NULL 12730 NULL NULL 12756 NULL NULL 12720 9 7 12 14 Now add the next (fourth) element into the tree: Since 14 is greater than 9, go to the right Since 14 is greater than 12, go right If the fourth element were assigned to add-ress 12756, RAM might appear as:
10 4 3 8 13 9 7 14 12 12622 - 12623 12624-12627 12628-12631 12824 - 12825 12720 - 12721 12756 - 12757 12730 - 12731 12758-12761 12826-12829 12732-12735 12722-12725 12726-12729 12762-12765 12830-12833 12736-12739 10 14 12 9 7 NULL 12824 12730 NULL NULL 12720 NULL NULL 12756 NULL 7 9 12 10 14 Now add the next (fifth) element into the tree: Since 10 is greater than 9, go to the right Since 10 is less than 12, go left If the fifth element were assigned to add-ress 12824, RAM might appear as:
4 3 8 13 9 7 14 10 12 7 9 3 4 10 13 12 8 14 Adding the remaining elements onto the list:
12730-12731 12900 -12901 13310 -12311 12824 -12825 12756 -12757 13300 -12301 12910 -12911 12622 -12623 12720 -12721 12302-12305 12312-12315 12758-12761 12722-12725 12624-12627 12912-12915 12826-12829 12902-12905 12732-12735 12628-12631 12316-12319 12916-12919 12736-12739 12762-12765 12830-12833 12726-12729 12906-12909 12306-12309 7 14 12 9 3 10 13 4 8 13310 12824 12910 NULL 12730 NULL NULL 12900 NULL NULL 12720 12756 13300 NULL NULL NULL NULL NULL 7 9 4 13 14 10 12 8 3 RAM might appear as:
Binary Trees • RECURSION • Recursion is a programming technique which allows a function to call itself. • Without recursion, traversing (moving about) binary trees becomes very difficult. Leaf