1 / 37

Rooted Tree

Rooted Tree. root. a. internal node (not a leaf). parent. d. b. node (self). c. sibling. e. f. leaf (no children). g. h. child. child. j. i. e, i, k, g, h are leaves. descendent. k. a few terms: parent, child, decendent, ancestor, sibling, subtree, path, degree,.

myd
Télécharger la présentation

Rooted Tree

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. Rooted Tree root a internal node (not a leaf) parent d b node (self) c sibling e f leaf (no children) g h child child j i e, i, k, g, h are leaves descendent k a few terms: parent, child, decendent, ancestor, sibling, subtree, path, degree,

  2. Subtree root A node and all of its descendents. a d b c e f g h i j k

  3. Paths in a Tree There exists a unique path from any node to any of its descendents. From a parent node to its child and other descendents. a d b c Path 1 Path 2 f e h g i j Path 1: { a, b, f, j } Path 2: { d, i }

  4. Depth and Height depth 0 tree height = 4 7 3 10 4 depth 1 node height = 2 8 12 11 2 depth 2 1 depth 3 6 5 Height of a node: the length of a node to its deepest descendent. depth 4 9

  5. Degree The number of children of a node x is called the degreeof x. degree = 3 7 3 10 4 8 12 degree = 1 11 2 degree = 0 1 6 5 9

  6. Binary Trees Each node has at most two children. Left child: the child node on the left. Right child: the child node on the right. A set of nodes Tis abinary treeif a) it is empty, or b) it consists of three disjoint subsets: 1) a root node 2) a left binary subtree 3) a right binary subtree r a e b c f d right subtree left subtree

  7. Representing Rooted Trees: Direct Way Every node has three references • One reference to the object stored at that node • One reference to the node’s parent • one reference to the node’s children

  8. Representing Rooted Trees: Child-sibling representation Every node has three references (direct way) • One reference to the object stored at that node • One reference to the node’s parent • one reference to the node’s left most children • One reference to the node’s sibling

  9. Child-sibling representation

  10. Tree Traversal A traversal is a manner of visiting each node in a tree once. There are several different traversals, each of which orders the nodes differently. A recursive way (preorder traversal) • visit the root of T • for each subtree T’ of the root, recursive traverse T’ The order in which you visit the subtrees in step 2 is arbitrary. The simplest way is to start at the rootʼs firstChild and follow the nextSibling links. In binary trees, the order is always left to right. A typical application: listing file directory

  11. Implementation What is the time complexity?

  12. Tree Traversal Another recursive way (postorder traversal) • for each subtree T’ of the root, recursive traverse T’ • visit the root of T In a postorder traversal, you visit each node’s children (in the left-to-right order) before the node itself A typical application: sum the total disk space used root directory

  13. Postorder tree Traversal

  14. Tree Traversal Another recursive way (inorder traversal for a binary tree) • recursively traverse T’s left subtree • visit the root of T • recursively traverse T’s right subtree Yet another way (level order traversal) visit the root of T visit all depth-1 nodes from left to right visit all depth-2 nodes, and so on how would you implement this?

  15. Implementing Level Traversal Enqueue the root Dequeue a node Visit it Enqueue its children (in order from left to right)

  16. Using Tree Traversal 1 Computing the arity (or degree) of a tree, i.e., the maximum number of children of a node Arity/degree = 6

  17. Computing arity

  18. Using Tree Traversal 2 Computing the height of a tree Using recursive If the root has no children, the height is 0 Otherwise, the height is 1+max(height(leftsubtree), height(rightsubtree)

  19. Computing tree height

  20. Expression Trees • An expression tree is a binary tree that represents an arithmetic expression • leaves are operands • internal nodes are operators prefix infix postfix

  21. From Postfix to Expression Tree The idea is to use a stack to store subtrees for subexpressions.

  22. Evaluating an expression tree

  23. Binary Trees • A few APIs • isLeaf() • setLeft() • setRight() • setData()

  24. Set A set is a collection of elements that contains no duplicates. Standard Operations: containment, union, and set differences

  25. Binary Search Tree A binary search tree is often used to implement a set where the element type if comparable. Property: For any node X, every key (element) in the left subtree of X is less than X’s key, and every key in the right subtree of X is greater than X’s key A same set of elements may have different BSTs, and an inorder traversal of a binary search tree visits the nodes in sorted order.

  26. Building a BST for a set

  27. Contains(c) Checking if a given element is C contained by a set is easier when the set is represented by a BST

  28. Contains(c)

  29. Add(k) The worst-case time needed to search for a key or to add a key to a BST T is O(height(T)).

  30. Building a BST for a set Starting from a root, adding elements in the set one by one The tree built for a set of elements depends on the insertion order. The tree can be in two extreme cases: Well-balanced tree (best case) Very unbalanced tree (worst case)

  31. remove() Case 1: the node to be removed has no children Solution: Just delete the node

  32. remove() Case 2: the node to be removed has only one child Solution: Just replace the deleted node with its child.

  33. remove() Case 3: the node to be removed has two children Solution: replace the node with its successor

  34. remove() Case 3: the node to be removed has two children The successor’s left child cannot exist, why?

  35. successor() Case 1: The node has a right subtree. Go down to leftmost element in right subtree. Case 2: The node does not have a right subtree. If there actually is a successor, then it must be the root r of this subtree. To find r, go up the tree following parent pointers, stopping when the previous node in the path is a left child of the current node.

  36. iterator() The tree iterator follow the nature order, starting at the minimum, or leftmost, element of the tree, proceeding all the way to the maximum. • next() • Go down the tree, following left child pointers until we find a node with no left child. This must be the minimum. • hasNext() • Implement using successor() • remove() What is their time complexity()?

More Related