KEMBAR78
Data structures and Algorithm analysis_Lecture4.pptx
Data structures and
Algorithm analysis
Lecturer: Dr. Safa Abd El-aziz Ahmed
Email: friendship_safa2004@yahoo.com
Tel: 01002323306
Motivation
• Accessing elements in a linear linked list can be prohibitive especially for large
amounts of input
• Trees are simple data structures for which the running time of most operations is
O(log N) on average
• For example, if N = 1 million:
• Searching an element in a linear linked list requires at most O(N) comparisons (i.e., 1 million
comparisons)
• Searching an element in a binary search tree (a kind of tree) requires O(log2 𝑁) comparisons
( 20 comparisons)
Motivation
• Trees are one of the most important and extensively used data structures in
computer science
• File systems of several popular operating systems are implemented as trees
• For example
Definitions
Definitions
• A tree T is a set of nodes
• Each non-empty tree has a root node and zero or more sub-trees T1, T2, ……, Tk
• Each subtree is a tree
• The root of a tree is connected to the root of each sub-tree by a directed edge
• Each node n1 connects to sub-tree rooted at n2 then:
• n1 is the parent of n2
• n2 is a child of n1
• Each node in a tree has only one parent
• Except the root node, which has no parent
Definitions
• Nodes with no children are leaves
• Nodes with children are non-leaves
• Nodes with the same parent are siblings
• A path from nodes n1 to nk is a sequence of nodes n1,n2, n3,………,nk such that ni is
the parent of ni+1 for 1 ≤ i < k
• The length of a path is the number of edges on the path (i.e. k – 1)
• Each node has a path of length 0 to itself
• There is exactly one path from the root to each node in the tree
Definitions
• A path from nodes n1 to nk is a sequence of nodes n1,n2, n3,………,nk such
that ni is the parent of ni+1 for 1 ≤ i < k
• Nodes ni, ni+1, ………, nk are descendants of ni and ancestors of nk
• Nodes ni+1, ………, nk are proper descendants of ni
• Nodes ni, ni+1, ………, nk-1 are proper ancestors of nk
Definitions
• B, C, D, E, F, G are siblings
• B, C, H, I, P, Q, K, L, M, N are leaves (or leaf nodes)
• K, L, M are siblings
• The path from A to Q is A—E—J—Q
• A, E, J are proper ancestors of Q
• E, J, Q (as well as I and P) are proper descendants of A
Definitions
• The depth of a node ni is the length of the unique path from the root to ni
• The root node has a depth of 0
• The depth of the tree is the depth of its deepest leaf
• The height of a node ni is the length of the longest path from ni to a leaf
• All leaves have a height of 0
• The height of a tree is the height of its root node
• The height of a tree equals its depth
Definitions
• Height of each node?
• Height of tree?
• Depth of each node?
• Depth of tree?
Binary Tree
• A binary tree is a tree where each node has no more than two children
• If a node is missing one or both children, then that child pointer is NULL
Example: Expression Trees
• Store expressions in a binary tree
• Leaves of trees are operands (e.g. constants, variables)
• Other internal nodes are unary or binary operators
• Used by compilers to parse and evaluate expressions
• Arithmetic, logic, relational, etc.
• E.g. (a + b * c) + ((d * e + f) * g)
Example: Expression Trees
• Evaluate expression
• Recursively evaluate left and right subtrees
• Apply operator at root node to results from subtrees
• Use post-order traversal: left, right, root
• Tree traversals
• Pre-order traversal: root, left, right
• The work at a node is performed before (pre) its children are processed
• In-order traversal: left, root, right
• Post-order traversal: left, right, root
• The work at a node is performed after (post) its children are evaluated
Traversals (Expression Tree to infix, postfix,
prefix)
• Pre-order traversal: Print out the operator first and the recursively print out the left and
right subtrees
• + + a * b c * + * d e f g
• In-order traversal: Recursively producing a parenthesized left expression, then printing out
the operator at the root and finally recursively producing a parenthesized right expression
• (a+ (b*c)) + (((d*e)+f)*g)
• Post-order traversal: Recursively print-out the left subtree, the right subtree and then the
operator
• a b c * + d e * f + g * +
Example: Postfix to Expression Trees
• Constructing an expression tree from postfix notation
• Use a stack of pointers to trees
• Read postfix expression from left to right
• If the symbol is a operand, create a one node tree and push a pointer to it onto a stack
• If operator, then:
• Create BinaryTreeNode with operator as the element
• Pop top two items off the stack
• Insert these items as left and right child of new node
• Push pointer to node on the stack
Example: Postfix to Expression Trees
• E.g., a b + c d e + * *
Example: Postfix to Expression Trees
• E.g., a b + c d e + * *
Example: Infix to Expression Tree
• E.g. (a + b * c) + ((d * e + f) * g)
• Convert infix to postfix first, and
• then apply postfix to expression tree evaluation algorithm
Binary Search Trees
• Application of Binary Tree
• Searching
• Each node in the tree stores an item
• Assume items are integers
• arbitrarily complex items are also possible
• Property that makes a Binary Tree  Binary Search Tree
• For every node X, in the tree, the values of all the items in a left subtree are smaller than item in X
• And values of all the items in its right subtree are larger than or equal to item in X.
• All the elements in the tree are ordered in some consistent manner
Binary Search Trees
• Binary search tree (BST) is a tree where:
• For any node n, items in left subtree of n < item in node n ≤ items in right subtree of n
• Average depth of a BST is O(logN)
• Which of the two trees is a BST?
Operations on a BST
• search(T, x) or contains(T, x): O(?)
• Returns true if x is in BST T and returns false, otherwise
• findMin(T) : O(?)
• Returns the minimum element or smallest element in the left subtree of BST T
• findMax(T) : O(?)
• Returns the maximum element or largest element in the right subtree of BST T
• printTree(T) : O(?)
• Pre-order, in-order, or post-order traversals
Operations on a BST
• insert(T, x): O(?)
• Inserts x into the BST T
• remove(T, x): O(?)
• Removes x from the BST T
• makeEmpty(T): O(?)
• Deletes all the nodes of the BST T
Operations on a BST
• search(T, x) or contains(T, x)
• T is a pointer to the root node of the BST
• x is the element we want to search
T
Operations on a BST
• search(T, x) or contains(T, x)
• Returns a pointer to x in the BST if x is in T and returns NULL, otherwise
T
Operations on a BST
• search(T, x) or contains(T, x)
• Typically, we assume no duplicate elements in the BST
• If duplicates, then store counts in nodes, or each node has a list of objects
Operations on a BST
• search(T, x) or contains(T, x)
• Complexity of searching a BST with N nodes is O(?)
• Complexity of searching a BST of height h is O(h)
• h = f(N)?
Operations on a BST
• findMin(T)
• Returns the minimum element or smallest element in the left subtree of BST T
T
• Start at the root and go left as long as there is a left child
• The stopping point is the smallest element
• Complexity: O(?)
Operations on a BST
• findMax(T)
• Returns the maximum element or largest element in the right subtree of BST T
• Complexity: O(?)
T
Operations on a BST
• preorder(T)
• Prints the elements of BST T in pre-order (root, left, right)
• Complexity: O(?)
Operations on a BST
• inorder(T)
• Prints the elements of BST T in inorder (left, root, right)
• Complexity: O(?)
Operations on a BST
• postorder(T)
• Prints the elements of BST T in post-order (left, right, root)
• Complexity: O(?)
Operations on a BST
• insert(T, x)
• Inserts x into the BST T
• E.g. insert 5
T T
Operations on a BST
• insert(T, x)
• “Search” for x until we reach the end of tree; insert x there
• Complexity: O(?)
Operations on a BST
• remove(T, x)
• Removes x from the BST T
• Case 1: Node to remove has 0 or 1 child
• If a node is a leaf, just delete it
• If the node has one child, the node can be deleted
after its parent adjust a link to bypass the node
• E.g. remove 4
Operations on a BST
• remove(T, x)
• Removes x from the BST T
• Case 2: Node to remove has 2 children
• Replace the data of this node with the smallest data of the
right subtree and recursively delete that node (which is now empty)
• Smallest node in the right subtree cannot have a left child, the
second remove is an easy one
• First Replace node element with successor
• Remove successor (Case 1)
• E.g. remove 2
Operations on a BST
Operations on a BST
• makeEmpty(T)
• Deletes all the nodes in the BST T
• Complexity: O(?)

Data structures and Algorithm analysis_Lecture4.pptx

  • 1.
    Data structures and Algorithmanalysis Lecturer: Dr. Safa Abd El-aziz Ahmed Email: friendship_safa2004@yahoo.com Tel: 01002323306
  • 2.
    Motivation • Accessing elementsin a linear linked list can be prohibitive especially for large amounts of input • Trees are simple data structures for which the running time of most operations is O(log N) on average • For example, if N = 1 million: • Searching an element in a linear linked list requires at most O(N) comparisons (i.e., 1 million comparisons) • Searching an element in a binary search tree (a kind of tree) requires O(log2 𝑁) comparisons ( 20 comparisons)
  • 3.
    Motivation • Trees areone of the most important and extensively used data structures in computer science • File systems of several popular operating systems are implemented as trees • For example
  • 4.
  • 5.
    Definitions • A treeT is a set of nodes • Each non-empty tree has a root node and zero or more sub-trees T1, T2, ……, Tk • Each subtree is a tree • The root of a tree is connected to the root of each sub-tree by a directed edge • Each node n1 connects to sub-tree rooted at n2 then: • n1 is the parent of n2 • n2 is a child of n1 • Each node in a tree has only one parent • Except the root node, which has no parent
  • 6.
    Definitions • Nodes withno children are leaves • Nodes with children are non-leaves • Nodes with the same parent are siblings • A path from nodes n1 to nk is a sequence of nodes n1,n2, n3,………,nk such that ni is the parent of ni+1 for 1 ≤ i < k • The length of a path is the number of edges on the path (i.e. k – 1) • Each node has a path of length 0 to itself • There is exactly one path from the root to each node in the tree
  • 7.
    Definitions • A pathfrom nodes n1 to nk is a sequence of nodes n1,n2, n3,………,nk such that ni is the parent of ni+1 for 1 ≤ i < k • Nodes ni, ni+1, ………, nk are descendants of ni and ancestors of nk • Nodes ni+1, ………, nk are proper descendants of ni • Nodes ni, ni+1, ………, nk-1 are proper ancestors of nk
  • 8.
    Definitions • B, C,D, E, F, G are siblings • B, C, H, I, P, Q, K, L, M, N are leaves (or leaf nodes) • K, L, M are siblings • The path from A to Q is A—E—J—Q • A, E, J are proper ancestors of Q • E, J, Q (as well as I and P) are proper descendants of A
  • 9.
    Definitions • The depthof a node ni is the length of the unique path from the root to ni • The root node has a depth of 0 • The depth of the tree is the depth of its deepest leaf • The height of a node ni is the length of the longest path from ni to a leaf • All leaves have a height of 0 • The height of a tree is the height of its root node • The height of a tree equals its depth
  • 10.
    Definitions • Height ofeach node? • Height of tree? • Depth of each node? • Depth of tree?
  • 11.
    Binary Tree • Abinary tree is a tree where each node has no more than two children • If a node is missing one or both children, then that child pointer is NULL
  • 12.
    Example: Expression Trees •Store expressions in a binary tree • Leaves of trees are operands (e.g. constants, variables) • Other internal nodes are unary or binary operators • Used by compilers to parse and evaluate expressions • Arithmetic, logic, relational, etc. • E.g. (a + b * c) + ((d * e + f) * g)
  • 13.
    Example: Expression Trees •Evaluate expression • Recursively evaluate left and right subtrees • Apply operator at root node to results from subtrees • Use post-order traversal: left, right, root • Tree traversals • Pre-order traversal: root, left, right • The work at a node is performed before (pre) its children are processed • In-order traversal: left, root, right • Post-order traversal: left, right, root • The work at a node is performed after (post) its children are evaluated
  • 14.
    Traversals (Expression Treeto infix, postfix, prefix) • Pre-order traversal: Print out the operator first and the recursively print out the left and right subtrees • + + a * b c * + * d e f g • In-order traversal: Recursively producing a parenthesized left expression, then printing out the operator at the root and finally recursively producing a parenthesized right expression • (a+ (b*c)) + (((d*e)+f)*g) • Post-order traversal: Recursively print-out the left subtree, the right subtree and then the operator • a b c * + d e * f + g * +
  • 15.
    Example: Postfix toExpression Trees • Constructing an expression tree from postfix notation • Use a stack of pointers to trees • Read postfix expression from left to right • If the symbol is a operand, create a one node tree and push a pointer to it onto a stack • If operator, then: • Create BinaryTreeNode with operator as the element • Pop top two items off the stack • Insert these items as left and right child of new node • Push pointer to node on the stack
  • 16.
    Example: Postfix toExpression Trees • E.g., a b + c d e + * *
  • 17.
    Example: Postfix toExpression Trees • E.g., a b + c d e + * *
  • 18.
    Example: Infix toExpression Tree • E.g. (a + b * c) + ((d * e + f) * g) • Convert infix to postfix first, and • then apply postfix to expression tree evaluation algorithm
  • 19.
    Binary Search Trees •Application of Binary Tree • Searching • Each node in the tree stores an item • Assume items are integers • arbitrarily complex items are also possible • Property that makes a Binary Tree  Binary Search Tree • For every node X, in the tree, the values of all the items in a left subtree are smaller than item in X • And values of all the items in its right subtree are larger than or equal to item in X. • All the elements in the tree are ordered in some consistent manner
  • 20.
    Binary Search Trees •Binary search tree (BST) is a tree where: • For any node n, items in left subtree of n < item in node n ≤ items in right subtree of n • Average depth of a BST is O(logN) • Which of the two trees is a BST?
  • 21.
    Operations on aBST • search(T, x) or contains(T, x): O(?) • Returns true if x is in BST T and returns false, otherwise • findMin(T) : O(?) • Returns the minimum element or smallest element in the left subtree of BST T • findMax(T) : O(?) • Returns the maximum element or largest element in the right subtree of BST T • printTree(T) : O(?) • Pre-order, in-order, or post-order traversals
  • 22.
    Operations on aBST • insert(T, x): O(?) • Inserts x into the BST T • remove(T, x): O(?) • Removes x from the BST T • makeEmpty(T): O(?) • Deletes all the nodes of the BST T
  • 23.
    Operations on aBST • search(T, x) or contains(T, x) • T is a pointer to the root node of the BST • x is the element we want to search T
  • 24.
    Operations on aBST • search(T, x) or contains(T, x) • Returns a pointer to x in the BST if x is in T and returns NULL, otherwise T
  • 25.
    Operations on aBST • search(T, x) or contains(T, x) • Typically, we assume no duplicate elements in the BST • If duplicates, then store counts in nodes, or each node has a list of objects
  • 26.
    Operations on aBST • search(T, x) or contains(T, x) • Complexity of searching a BST with N nodes is O(?) • Complexity of searching a BST of height h is O(h) • h = f(N)?
  • 27.
    Operations on aBST • findMin(T) • Returns the minimum element or smallest element in the left subtree of BST T T • Start at the root and go left as long as there is a left child • The stopping point is the smallest element • Complexity: O(?)
  • 28.
    Operations on aBST • findMax(T) • Returns the maximum element or largest element in the right subtree of BST T • Complexity: O(?) T
  • 29.
    Operations on aBST • preorder(T) • Prints the elements of BST T in pre-order (root, left, right) • Complexity: O(?)
  • 30.
    Operations on aBST • inorder(T) • Prints the elements of BST T in inorder (left, root, right) • Complexity: O(?)
  • 31.
    Operations on aBST • postorder(T) • Prints the elements of BST T in post-order (left, right, root) • Complexity: O(?)
  • 32.
    Operations on aBST • insert(T, x) • Inserts x into the BST T • E.g. insert 5 T T
  • 33.
    Operations on aBST • insert(T, x) • “Search” for x until we reach the end of tree; insert x there • Complexity: O(?)
  • 34.
    Operations on aBST • remove(T, x) • Removes x from the BST T • Case 1: Node to remove has 0 or 1 child • If a node is a leaf, just delete it • If the node has one child, the node can be deleted after its parent adjust a link to bypass the node • E.g. remove 4
  • 35.
    Operations on aBST • remove(T, x) • Removes x from the BST T • Case 2: Node to remove has 2 children • Replace the data of this node with the smallest data of the right subtree and recursively delete that node (which is now empty) • Smallest node in the right subtree cannot have a left child, the second remove is an easy one • First Replace node element with successor • Remove successor (Case 1) • E.g. remove 2
  • 36.
  • 37.
    Operations on aBST • makeEmpty(T) • Deletes all the nodes in the BST T • Complexity: O(?)