1 / 29

Binary Trees

Binary Trees. Chapter 8. Definition. "A structure with a unique starting node (the root) in which each node is capable of having two child nodes and in which a unique path exists from the root to every other node". M. M is the root H is the left child of M

lali
Télécharger la présentation

Binary 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. Binary Trees Chapter 8

  2. Definition "A structure with a unique starting node (the root) in which each node is capable of having two child nodes and in which a unique path exists from the root to every other node"

  3. M M is the root H is the left child of M X is the right child of M M is the parent of H and X H is the root of the left subtree of M X is the root of the right subtree of M The descendants of H are D, B, F, and L The ancestors of F are D, H, and M H X D L P B F V Q W T

  4. M A leaf node is a node with no children B, F, L, T, and W are all leaves The degree of a node is its number of children The leaves are all degree zero nodes X, P, and Q are all degree one nodes M, H, D, and V are all degree two nodes H X D L P B F V Q W T

  5. M The level of a node is its distance from the root The level of the root, M, is zero The level of X is one The level of T is five The height of the tree is five H X D L P B F V Q W T

  6. M In a binary search tree the key value in any node is : greater than the key value in any nodes in its left subtree less than the key value in any nodes in its right subtree Is this a binary search tree? H X D L P B F V Q W T

  7. A tree traversal is a systematic way of traveling through the tree visiting each node exactly once. Consider the following tree: B A C Considering only traversals in which the left child is visited before the right child, there are three possible traversals A B C B A C A C B Now, note when root is visited relative to the children

  8. A tree traversal is a systematic way of traveling through the tree visiting each node exactly once. Consider the following tree: B A C Considering only traversals in which the left child is visited before the right child, there are three possible traversals A B C root is in between children B A C A C B

  9. A tree traversal is a systematic way of traveling through the tree visiting each node exactly once. Consider the following tree: B A C Considering only traversals in which the left child is visited before the right child, there are three possible traversals A B C root is in between children so called inorder traversal B A C A C B

  10. A tree traversal is a systematic way of traveling through the tree visiting each node exactly once. Consider the following tree: B A C Considering only traversals in which the left child is visited before the right child, there are three possible traversals A B C root is in between children so called inorder traversal B A C root is before children A C B

  11. A tree traversal is a systematic way of traveling through the tree visiting each node exactly once. Consider the following tree: B A C Considering only traversals in which the left child is visited before the right child, there are three possible traversals A B C root is in between children so called inorder traversal B A C root is before children so called preorder traversal A C B

  12. A tree traversal is a systematic way of traveling through the tree visiting each node exactly once. Consider the following tree: B A C Considering only traversals in which the left child is visited before the right child, there are three possible traversals A B C root is in between children so called inorder traversal B A C root is before children so called preorder traversal A C B root is after children

  13. A tree traversal is a systematic way of traveling through the tree visiting each node exactly once. Consider the following tree: B A C Considering only traversals in which the left child is visited before the right child, there are three possible traversals A B C root is in between children so called inorder traversal B A C root is before children so called preorder traversal A C B root is after children so called postorder traversal

  14. M Preorder traversal of this tree is: M, H, D, B, F, L, X, P, V, Q, T, W Inorder traversal of this tree is: B, D, F, H, L, M, P, Q, T, V, W, X Postorder traversal of this tree is: B, F, D, L, H, T, Q, W, V, P, X, M H X D L P B F V Q W T

  15. Binary Search Tree Search Algorithm Search (TreeType tree, KeyType searchkey) if (tree is empty) value is not in tree else if (searchkey < root value) Search (left subchild, searchkey) else if (searchkey > root value) Search (right subchild, searchkey) else value is in root

  16. M Search (TreeType tree, KeyType searchkey) if (tree is empty) value is not in tree else if (searchkey < root value) Search (left subchild, searchkey) else if (searchkey > root value) Search (right subchild, searchkey) else value is in root H X D L P B F V Q W T

  17. Binary Search Tree Insert Algorithm Insert (TreeType tree, KeyType newkey) if (tree is empty) put newkey here else if (newkey < root value) Insert (left subchild, newkey) else if (newkey > root value) Insert (right subchild, newkey) else value is already in tree

  18. M Insert (TreeType tree, KeyType newkey) if (tree is empty) put newkey here else if (newkey < root value) Insert (left subchild, newkey) else if (newkey > root value) Insert (right subchild, newkey) else value is already in tree H X D L P B F V Q W T

  19. Binary Search Tree Delete Algorithm Delete (TreeType tree, KeyType deletekey) if (tree is empty) value is not in tree else if (deletekey < root value) Delete (left subchild, searchkey) else if (deletekey > root value) Delete (right subchild, searchkey) else value is in root so delete it

  20. Binary Search Tree Delete Algorithm Delete (TreeType tree, KeyType deletekey) if (tree is empty) value is not in tree else if (deletekey < root value) Delete (left subchild, searchkey) else if (deletekey > root value) Delete (right subchild, searchkey) else value is in root so delete it • But this is a little complicated! • (Depends on how many children are hanging off of this root.)

  21. M Case 1: Deleting a leaf Easy! Example: Consider deleting L H X D L P B F V Q W T

  22. M Case 1: Deleting a leaf Easy! Example: Consider deleting L H X D P B F V Q W T

  23. M Case 2: Deleting a degree 1 node Somewhat easy! Similar to deleting a node from a linked list Example: Consider deleting P H X D L P B F V Q W T

  24. M Case 2: Deleting a degree 1 node Somewhat easy! Similar to deleting a node from a linked list Example: Consider deleting P H X D L V B F Q W T

  25. M Case 3: Deleting a degree 2 node Most complicated, but not too bad. Requires 3 steps: 1. Find immediate predecessor 2. Copy predecessor value to node to be deleted 3. Delete predecessor Example: Consider deleting H H X D L P B F V Q W T

  26. M Case 3: Deleting a degree 2 node Most complicated, but not too bad. Requires 3 steps. 1. Find immediate predecessor* 2. Copy predecessor value to node to be deleted 3. Delete predecessor Example: Consider deleting H H X D L P B F V Q W T * Note: immediate predecessor is node visited just before this node in an inorder traversal. Find it by taking one link to the left, then travel down right links until last node in path is reached.

  27. M Case 3: Deleting a degree 2 node Most complicated, but not too bad. Requires 3 steps. 1. Find immediate predecessor 2. Copy predecessor value to node to be deleted 3. Delete predecessor Example: Consider deleting H F X D L P B F V Q W T

  28. M Case 3: Deleting a degree 2 node Most complicated, but not too bad. Requires 3 steps: 1. Find immediate predecessor 2. Copy predecessor value to node to be deleted 3. Delete predecessor Example: Consider deleting H F X D L P B V Q W T

  29. To be continued . . .

More Related