Upgrade to Pro — share decks privately, control downloads, hide ads and more …

lecture21.pdf

Avatar for William Albritton William Albritton
September 14, 2014
150

 lecture21.pdf

Avatar for William Albritton

William Albritton

September 14, 2014
Tweet

Transcript

  1. Memory Upload • Parts of a tree • Types of

    trees • Tree traversals • Add and remove operations
  2. What is a Tree? • Here is a tree of

    Strings M G P Q N H C O F A K D
  3. Tree Nodes • A node in a tree is similar

    to a node in a linked list • A linked list node has a reference to zero or one other link nodes • A tree node has a reference to zero or more other tree nodes
  4. What is a Tree? • A tree is set of

    nodes with the following restrictions: 1. Each node has one parent (except for the root node) 2. Each node has zero or more child nodes
  5. Edges • A hierarchal relationship exists between the nodes •

    In other words, the node on top is a parent node to the node directly below, which is called the child node • An edge, which represents this hierarchal relationship, connects the nodes
  6. Types of Nodes • Tree nodes can be divided into

    several different types based on their position in the tree 1.Root node • The node at the “top” of the tree • The only node without a parent node 2.Interior node • A node with a parent and at least one child node
  7. Example Tree 1. Root node: • 50 2. Interior nodes:

    • 24, 77, 11, 63, 16, 71, 20 • Tree of Integers 50 24 77 89 63 32 11 71 58 16 5 73 20 14 18
  8. Types of Nodes 3. Leaf node • Node with no

    children 4.Parent node • The node one level above the current node that is connected to the current node by an edge 5.Child node • The node one level below the current node that is connected to the current node by an edge
  9. Example Tree • Leaf nodes:  32, 89, 5, 58,

    14, 73, 18 • Node 11’s parent:  24 • Node 11’s children: 5, 16 • Tree of Integers 50 24 77 89 63 32 11 71 58 16 5 73 20 14 18
  10. Types of Nodes  Ancestor nodes • The current node’s

    parent, parent’s parent, extending to the root node • All nodes on the path from current node to root node  Descendant nodes • The current node’s children, children’s children, extending to all of the leaf nodes • All nodes on all the paths from current node to the leaf nodes
  11. Example Tree • Node 20’s ancestors:  16, 11, 24,

    50 • Node 11’s descendants:  5, 16, 14, 20, 18 • Tree of Integers 50 24 77 89 63 32 11 71 58 16 5 73 20 14 18
  12. Terminology • The depth is the number of edges from

    root node to current node • For example, the root is at depth 0, its children at depth 1, their children are at depth 2, etc. • The height is the number of edges in the path from the root to a tree’s furthest (deepest) leaf • So the height is the maximum depth
  13. Example Tree • Node 50’s depth: • 0 • Node

    11’s depth: • 2 • Node 18’s depth: • 5 • Height of tree: • 5 • Tree of Integers 50 24 77 89 63 32 11 71 58 16 5 73 20 14 18
  14. Terminology • A binary tree is a tree in which

    each node has at most 2 children • The subtree is a tree that consists of a node plus all of its descendants
  15. Example Tree • Is this a binary tree? • Yes,

    because each node has either 0, 1 or 2 children • Tree of Integers 50 24 77 89 63 11 71 58 16 5 73 20 14 18
  16. Example Tree • Is this a binary tree?  No,

    because node 50 has four child nodes, and node 16 has three child nodes • Tree of Integers 50 89 63 32 11 71 58 16 5 73 20 14 18
  17. Example Tree • Node 11’s left subtree:  5 •

    Node 11’s right subtree:  16, 14, 20, 18 • Tree of Integers 50 24 77 89 63 32 11 71 58 16 5 73 20 14 18
  18. Balanced Binary Trees • A balanced binary tree is a

    tree in which the height of left and right subtrees of any node is the same or differs by one (1)
  19. Example Tree • Is this a balanced binary tree? •

    No, because node 24’s left subtree’s height is 3 (three), and node 24’s right subtree’s height is 0 (zero) • Tree of Integers 50 24 77 89 63 32 11 71 58 16 5 73 20 14 18
  20. Example Tree • Is this a balanced binary tree? •

    Yes, because all pairs of left and right subtrees have the same height • Tree of Integers 50 24 77 89 63 32 11 71 58 16 5 73 20 14 18
  21. Traversal Algorithms • A traversal algorithm is a way to

    visit each node of a binary tree • Here are the 3 most common traversal algorithms (all recursive) 1. Preorder traversal 2. Inorder traversal 3. Postorder traversal
  22. Preorder Traversal • Algorithm • Display the root • Process

    the left subtree in Preorder • Process the right subtree in Preorder • Output: 50, 24, 11, 5, 16, 14, 20, 18, 32, 77, 63, 58, 71, 73, 89 • Tree of Integers 50 24 77 89 63 32 11 71 58 16 5 73 20 14 18
  23. Inorder Traversal • Algorithm 1. Process the left subtree in

    Inorder 2. Display the root 3. Process the right subtree in Inorder • Output: 5, 11, 14, 16, 18, 20, 24, 32, 50, 58, 63, 71, 73, 77, 89 • Tree of Integers 50 24 77 89 63 32 11 71 58 16 5 73 20 14 18
  24. Postorder Traversal • Algorithm 1. Process the left subtree in

    Postorder 2. Process the right subtree in Postorder 3. Display the root • Output: 5, 14, 18, 20, 16, 11, 32, 24, 58, 73, 71, 63, 89, 77, 50 • Tree of Integers 50 24 77 63 32 11 71 58 16 5 73 20 14 18 89
  25. Expression Trees • An expression tree is a tree where:

    • Every node with children is an operator • Every leaf node is an operand • Each operator operates on the values of its children
  26. Expression Tree Example • The value of this expression tree

    is 23, because: • ((6*3)+7)- ((9%5)/2) • =(18+7)-(4/2) • =25-2 • =23 • Expression tree - + / 2 % 7 * 5 9 3 6
  27. Expression Tree Example • Preorder traversal • -+*637/%952 • Inorder

    traversal • 6*3+7-9%5/2 • Postorder traversal • 63*7+95%2/- • Expression tree - + / 2 % 7 * 5 9 3 6
  28. Binary Search Trees • A binary search tree is a

    binary tree in which: 1. The value of each node in the left subtree is less than the value of the root node 2. The value of each node in the right subtree is greater than the value of the root node 3. Contains no nodes of equal value
  29. Example Tree • Is this a binary search tree? •

    Yes, because each left child is less than its parent, each right child is greater than its parent, and no values are repeated • Tree of Integers 50 24 77 89 63 32 11 71 58 16 5 73 20 14 18
  30. Example Tree • Is this a binary search tree? •

    No, because node 60, which is the left child of node 5, has a greater value than its parent • Node 60 should be the right child of node 58 • Tree of Integers 50 24 77 89 63 32 11 71 58 16 5 73 20 14 18 60
  31. Binary Search Trees • What is important about binary search

    trees? 1.Quick to add, get, or remove a node • All these operations are O(log2 n) 2.Efficient to sort and search for data • That is why called a binary “search” tree
  32. Binary Search Trees 3. Important to keep the tree balanced

    • For example, a linked list is the most extremely unbalanced binary tree • As a tree becomes more and more unbalanced, the Big-O for add, get, and remove operations will change from a fast O(log2 n) to a slow O(n)
  33. Example Tree • Is this a binary search tree? 

    Yes, but it is an extremely unbalanced binary search tree  Another name for this is an ordered linked list • Tree of Integers 24 32 11 16 5 20 14 18
  34. Add Algorithm 1. Base case 1: if current node does

    not exist a. Draw the new node here as a root or a child node 2. Base case 2: if current node and new node have same value a. Do not add new node, because no duplicates are allowed
  35. Add Algorithm 3. Recursive case 1: if new node’s value

    is less than current node’s value a. Add new node to left subtree 4. Recursive case 2: if new node’s value is greater than current node’s value a. Add new node to the right subtree
  36. Example Add • Create a binary search tree of the

    following letters • M, P, N, O, P, Q, G, C, F, C, H, A, D, G, K • Add M • No nodes in tree, so M becomes the root node • Tree of Strings M
  37. Example Add • Add P • P is greater than

    M, so we add P as the right child node of M • Tree of Strings P M
  38. Example Add • Add N • N is greater than

    M, so move to the right child node • N is less than P, so N becomes P’s left child node • Tree of Strings N P M
  39. Example Add • Add O • O is greater than

    M, so move to right child node • O is less than P, so move to left child node • O is greater than N, so O is N’s right child node • Tree of Strings O N P M
  40. Example Add • Add P • P is greater than

    M, so move to right child node • P is the same value as node P, so we do not add another P to the tree • Tree of Strings M P N O M
  41. Example Add • Add Q • Q is greater than

    M, so move to right child node • Q is greater than P, so Q is P’s right child node • Tree of Strings Q O N P M
  42. Example Add • Add G • G is less than

    M, so G becomes M’s left child node • Tree of Strings G Q O N P M
  43. Example Add • Add C • C is less than

    M, so move to left child node • C is less than G, so C becomes G’s left child node • Tree of Strings C G Q O N P M
  44. Example Add • Add F • F is less than

    M, so move to left child node • F is less than G, so move to left child node • F is greater than C, so F becomes C’s right child node • Tree of Strings F C G Q O N P M
  45. Example Add • Add H • H is less than

    M, so move to left node • H is greater than G, so H becomes G’s right node • Tree of Strings H F C G Q O N P M
  46. Example Add • Add A • A is less than

    M, so move to left node • A is less than G, so move to left node • A is less than C, so A becomes C’s left node • Tree of Strings A H F C G Q O N P M
  47. Example Add • Add D • D is less than

    M, so move to left • D is less than G, so move to left • D is greater than C, so move to right node • D is less than F, so D becomes F’s left node • Tree of Strings D A H F C G Q O N P M
  48. Example Add • Add K • K is less than

    M, so move to left node • K is greater than G, so move to right node • K is greater than H, so K becomes H’s right node • Tree of Strings K D A H F C G Q O N P M
  49. Remove Algorithm 1. Cross out the node to be deleted

    (if such a node exists) 2. If deleted node is a leaf, you are finished 3. If deleted node has a single child node, replace the deleted node with the child node
  50. Remove Algorithm 4. If deleted node has two child nodes

    a. Replace the deleted node with the rightmost node in the left subtree b. If the rightmost node in the left subtree has a left child, replace the rightmost node in the left subtree with its left child
  51. Remove Rational • Why do we want to replace the

    deleted node with the rightmost node in the left subtree? • Have to preserve the order of binary search tree • All nodes on left subtree should be less than root and all nodes on right subtree should be greater than root
  52. Remove Rational • The rightmost node in the left subtree

    is the inorder predecessor (next smallest node) of the root node • So all other nodes in left subtree should be less than this node and all other nodes in right subtree should be greater than this node
  53. Remove M • Remove M (a node with two child

    nodes) • Cross out node M, which is the root node of the tree • Tree of Strings M G P Q N H C O F A K D
  54. K Remove M • Replace value M with value K

    • Move to the left subtree of node M (root node) • Move as far as you can to the rightmost node, which is K • Tree of Strings M G P Q N H C O F A D K
  55. Remove M • Delete node K • Since we replaced

    the root with the value of the rightmost node in the left subtree, which is K, we need to delete node K • Tree of Strings K G P Q N H C O F A D
  56. Remove F • Remove F (a node with one child

    node) • Put an “X” over node F • Tree of Strings K G P Q N H C O F A D
  57. Remove F • Replace node F with node D •

    Since node F only has one child, we replace node F with child node D • Tree of Strings K G P Q N H C O D A F D
  58. Remove A • Remove A (a node with no child

    nodes) • Put an “X” over node A • Tree of Strings K G P Q N H C O D A
  59. Remove A • Delete node A • Since node A

    has no child nodes, we simply delete it from the tree without rearranging the other nodes in the tree • Tree of Strings G P Q N H C O D A K
  60. Memory Defragmenter • Parts of a tree • Types of

    trees • Preorder, inorder, and postorder tree traversals • Add to and remove from a binary search tree
  61. Task Manager • Before the next class, you need to:

    1.Do the assignment corresponding to this lecture 2.Email me any questions you may have about the material 3.Turn in the assignment before the next lecture 4.Get outside and go for a stroll