This preview shows page 1-2-3-4-5-6-7-51-52-53-54-55-56-57-58-103-104-105-106-107-108-109 out of 109 pages.
1111111Trees222222OverviewTree data structureBinary search treesSupport O(log2N) operationsBalanced treesSTL set and map classesB-trees for accessing secondary storageApplications333Trees3GenericTree:G is parent of N and child of AM is child of F and grandchild of AA is an ancestor of P444DefinitionsA tree T is a set of nodesEach non-empty tree has a root node and zero or more sub-trees T1, …, TkEach sub-tree is a treeThe root of a tree is connected to the root of each subtree by a directed edgeIf node n1connects to sub-tree rooted at n2, thenn1is the parent of n2n2is a child of n1Each node in a tree has only one parentExcept the root, which has no parent4Recursive definitionCan a tree have a cycle? “DAG”555DefinitionsNodes with no children are leavesNodes with the same parent are siblingsA path from nodes n1to nkis a sequence of nodes n1, n2, …, nksuch that niis the parent of ni+1for 1 ≤ i < kThe 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 itselfThere is exactly one path from the root to each node in a treeNodes ni,…,nkare descendants of niand ancestors of nkNodes ni+1,…, nkare proper descendantsNodes ni,…,nk-1are proper ancestors5666Definitions6B,C,H,I,P,Q,K,L,M,N are leavesB,C,D,E,F,G are siblingsK,L,M are siblingsThe path from A to Q is A – E – J – QA,E,J are proper ancestors of Q E,J,Q (and I,P) are proper descendants of A777DefinitionsThe depth of a node niis the length of the unique path from the root to niThe root node has a depth of 0The depth of a tree is the depth of its deepest leafThe height of a node niis the length of the longestpath from nito a leafAll leaves have a height of 0The height of a tree is the height of its root nodeThe height of a tree equals its depth7Can there bemore than one?888Trees8Height of each node?Height of tree?Depth of each node?Depth of tree?999Implementation of TreesSolution 1: Vector of childrenSolution 2: List of children9Struct TreeNode{Object element;vector<TreeNode> children;}Struct TreeNode{Object element;list<TreeNode> children;}101010Implementation of TreesSolution 3: First-child, next-sibling10Struct TreeNode{Object element;TreeNode *firstChild;TreeNode *nextSibling;}111111Binary TreesA 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 NULL11struct BinaryTreeNode{Object element;BinaryTreeNode *leftChild;BinaryTreeNode *rightChild;}121212Example: Expression TreesStore expressions in a binary treeLeaves of tree are operands (e.g., constants, variables)Other internal nodes are unary or binary operatorsUsed by compilers to parse and evaluate expressionsArithmetic, logic, etc.E.g., (a + b * c)+((d * e + f) * g)12131313Example: Expression TreesEvaluate expressionRecursively evaluate left and right subtreesApply operator at root node to results from subtreesPost-order traversal: left, right, rootTraversalsPre-order traversal: root, left, rightIn-order traversal: root, left, right13141414Traversals14Pre-order: node - left - rightPost-order: left - right - nodeIn-order: left - node - right151515Traversals15Pre-order: + + a * b c * + * d e f gPost-order: a b c * + d e * f + g * +In-order: a + b * c + d * e + f * g161616Example: Expression TreesConstructing an expression tree from postfix notationUse a stack of pointers to treesRead postfix expression left to rightIf operand, then push on stackIf operator, then:Create a BinaryTreeNode with operator as the elementPop top two items off stackInsert these items as left and right child of new nodePush pointer to node on the stack16171717E.g., a b + c d e + * *Example: Expression Trees17ab(1)ab(2)+ab(3)+edctoptoptopab(4)+edctop+181818E.g., a b + c d e + * *Example: Expression Trees18ab(5)+edctop+*ab(6)+edctop+**191919Binary Search TreesComplexity of searching for an item in a binary tree containing N nodes is O(log N)Binary search tree (BST)For any node n, items in left subtree of n ≤ item in node n ≤ items in right subtree of n19BST?BST?202020Searching in BSTs20Contains (T, x){if (T == NULL)then return NULLif (T->element == x)then return Tif (x < T->element)then return Contains (T->leftChild, x)else return Contains (T->rightChild, x)}Typically assume no duplicate elements.If duplicates, then store counts in nodes, or each node has a list of objects.212121Searching in BSTsComplexity of searching a BST with N nodes is O(?)Complexity of searching a BST of height h is O(h)h = O(N) 21123468123468222222Searching in BSTsFinding the minimum elementSmallest element in left subtreeComplexity ?22findMin (T){if (T == NULL)then return NULLif (T->leftChild == NULL)then return Telse return findMin (T->leftChild)}232323Searching in BSTsFinding the maximum elementLargest element in right subtreeComplexity ?23findMax (T){if (T == NULL)then return NULLif (T->rightChild == NULL)then return Telse return findMax (T->rightChild)}242424Printing BSTsIn-order traversal ==> sortedComplexity?24PrintTree (T){if (T == NULL)then returnPrintTree (T->leftChild)cout << T->elementPrintTree (T->rightChild)}1 2 3 4 6 8252525Inserting into BSTsE.g., insert 525262626Inserting into BSTs“Search” for element until reach end of tree; insert new element there26Insert (x, T){if (T == NULL)then T = new Node(x)if (x < T->element)then if (T->leftChild == NULL)then T->leftChild = new Node(x)else Insert (x, T->leftChild)else if (T->rightChild == NULL)then (T->rightChild = new Node(x)else Insert (x, T->rightChild)}Complexity?272727Removing from BSTsCase 1: Node to remove has 0 or 1 childJust remove itE.g., remove 427282828Removing from BSTsCase 2: Node to remove has 2 childrenReplace node element with successorRemove successor (case 1)E.g., remove 228292929Removing from BSTs29Remove (x, T){if (T == NULL)then returnif (x == T->element)then if ((T->left == NULL) && (T->right != NULL))then T = T->right // implied deleteelse if ((T->right == NULL) && (T->left != NULL))then T = T->left // implied deleteelse successor = findMin (T->right) // Case 2T->element = successor->elementRemove (T->element, T->right)else if (x < T->element)then Remove (x, T->left)else Remove (x, T->right)}Complexity?303030Implementation of
View Full Document