290 likes | 464 Views
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
E N D
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 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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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.)
M Case 1: Deleting a leaf Easy! Example: Consider deleting L H X D L P B F V Q W T
M Case 1: Deleting a leaf Easy! Example: Consider deleting L H X D P B F V Q W T
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
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
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
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.
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
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