1 / 51

Trees

Trees. Binary Search, AVL, Splay. Introduction. Trees are a hierarchical collection (non-linear) Mirrors real-world structures Books (chapters/paragraphs/sentences/words) Organizational charts Filing Cabinets/folders/documents File systems (drives/folders/files). Terminology.

Télécharger la présentation

Trees

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. Trees Binary Search, AVL, Splay

  2. Introduction • Trees are a hierarchical collection (non-linear) • Mirrors real-world structures • Books (chapters/paragraphs/sentences/words) • Organizational charts • Filing Cabinets/folders/documents • File systems (drives/folders/files)

  3. Terminology • Trees are formed from nodes and edges. Nodes are sometimes called vertices. Edges are sometimes called branches. • Nodes may have a number of properties including value and label. • Edges are used to relate nodes to each other. In a tree, this represents a “parent-child” relationship. • An edge {a,b} between nodes a and b establishes a as the parent of b. Also, b is called a child of a. • Although edges are usually drawn as simple lines, they are really directed from parent to child. In tree drawings, this is top-to-bottom.

  4. Definition • This definition is “recursive” and “constructive”. • A single node is a tree. It is "root." • Suppose N is a node and T1, T2, ..., Tk are trees with roots n1, n2, ...,nk, respectively. We can construct a new tree T by making N the parent of the nodes n1, n2, ..., nk. Then, N is the root of T and T1, T2, ..., Tk are subtrees.

  5. More Terminology • A node is either internal or it is a leaf. • A leaf is a node that has no children • An internal node has children (duh) • Every node in a tree (except root) has exactly one parent. • The degreeof anode is the number of children it has. • The degreeof atree is the maximum degree of all of its nodes.

  6. Definitions (Paths and Properties) • A path is a sequence of nodes n1, n2, ..., nk such that node ni is the parent of node ni+1 for all 1 <= i <= k. • The length of a path is the number of edges on the path (one less than the number of nodes). • The descendents of a node are all the nodes that are on some path from the node to any leaf. • The ancestors of a node are all the nodes that are on the path from the node to the root. • The siblings of a node are all of the nodes having the same parent as that node. • The depth of a node is the length of the path from root to the node. The depth of a node is sometimes called its level. • The heightof anode is the length of the longest path from the node to a leaf. • The heightof atree is the height of its root.

  7. Example • What are the leaf nodes? • Y,Z,U,V,W • What are the internal nodes? • R,S,T,X • What is the degree of the tree? • 3 • What is the height of the tree? • 3 • What are the heights of nodes R, S, and Z? • 3, 2, 0 • What is the depth of node T? • 1 • Is R,S,X a path? • Yes • Is R,X,Y is a path? • No. The sequence does not satisfy the "parenthood" property (R is not the parent of X).

  8. Linked Representation • Use nodes to impose a hierarchical structure • A node stores data • A node keeps a list of its’ children • A node may keep a reference to its’ parent • A node may have a label • A tree maintains only a reference to the rootnode class TreeNode { private List children; private Object data; private TreeNode parent; private String label; }

  9. Binary Tree Definitions • Informal: A binary tree is a tree in which each node has degree of exactly 2 and the children of each node are distinguished as "left" and "right." Some of the children of a node may be empty. • Formal: A binary tree is: • either empty, or • it is a node that has a left and a right subtree, each of which is a binary tree. • A full binary tree (FBT) is a binary tree in which each node has exactly 2 non-empty children or exactly two empty children, and all the leaves are on the same level. • A complete binary tree (CBT) is a FBT except, perhaps, that the deepest level may not be completely filled. If not completely filled, it is filled from left-to-right. • A FBT is a CBT, but not vice-versa.

  10. Complete/Full Binary Tree Example • Is this a full binary tree? • No – not all leaf nodes are at the same level. • No - node G has an empty right and a non-empty left. • Is it a complete binary tree? • Yes • Is this a full binary tree? • No - node I has an empty right and a non-empty left. • Is it a complete binary tree? • No – the final level is not completed in left-to-right fashion

  11. Full Binary Tree Property The number of nodes at level n in a full binary tree is 2n

  12. Full Binary Tree Property A full binary tree of height h has ?? leaf nodes and ?? total nodes

  13. Traversing Binary Trees • It is often necessary to enumerate and/or process each element in a binary tree. There are four common means of traversing (enumerating) the elements of a tree. • InOrder • PostOrder • PreOrder • LevelOrder • In Java terms, we can write an Enumeration class that corresponds to each of these traversals • boolean hasMoreElements() • Object nextElement()

  14. PreOrder Traversal algorithm preOrder(TreeNode t) Input: a tree node (can be considered to be a tree) Output: None. Each node is visited in pre-order fashion Visit node t if t has a left child preOrder(left child of t) if t has a right child preOrder(right child of t)

  15. InOrder Traversal algorithm inOrder(TreeNode t) Input: a tree node (can be considered to be a tree) Output: None. Each node is visited in in-order fashion if t has a left child inOrder(left child of t) Visit node t if t has a right child inOrder(right child of t)

  16. PostOrder Traversal algorithm postOrder(TreeNode t) Input: a tree node (can be considered to be a tree) Output: None. Each node is visited in post-order fashion if t has a left child postOrder(left child of t) if t has a right child postOrder(right child of t) Visit node t

  17. LevelOrder Traversal algorithm levelOrder(TreeNode t) Input: a tree node (can be considered to be a tree) Output: None. Each node is visited in top-to-bottom, left-to-right order Let Q be a Queue Q.enqueue(t) while the Q is not empty tree = Q.dequeue() Visit node tree if tree has a left child Q.enqueue(left child of tree) if T has a right child Q.enqueue(right child of tree)

  18. Euler Tour Traversal(buy 3 get 1 free) algorithm eulerTour(TreeNode t) Input: a tree node (can be considered to be a tree) Output: None. Each node is visited in a “unified” algorithm if(t is leaf node) perform the action for visiting the leaf node else { perform the action for visiting node t from the left if t has a left child eulerTour(left child of t) perform the action for visiting node t from below if t has a right child eulerTour(right child of t) perform the action for visiting node t on the right }

  19. + * - 3 2 + * 2 5 5 8 Binary Tree Example • Expression Tree • A binary tree whose internal nodes store “operators” and whose leaves store “values” • Corresponds to an arithmetic expression

  20. + * - 3 2 + * 2 5 5 8 Expression Trees • Convert the expression tree to a fully parenthesized expression ((3*2)+((2+5)-(5*8))) Use an Euler tour to accomplish the conversion Leaf node action : print the value of the leaf node Left action : print ‘(‘ Below action : print the value of the node Right action : print ‘)’

  21. + * - 3 2 + * 2 5 5 8 Expression Trees • Convert a fully parenthesized expression into an expression tree! ((3*2)+((2+5)-(5*8))) algorithm convertToTree(Expression e) Let S be a stack for every token t in e if t is a ‘(‘ do nothing if t is a ‘)‘ form a new tree where right child is s.pop(); root is s.pop(); left is s.pop(); push the new tree onto S otherwise S.push(new node containing token t) return s.pop()

  22. Evaluate Arithmetic Expressions • Specialization of an postOrder traversal algorithm evalExpr(TreeNode t) Input: a tree node (can be considered to be a tree) Output: the result of the arithmetic expression if (t is leaf node) return t.getData() else { x <- evalExpr(left child of t) y <- evalExpr(right child of t)  <- t.getData() //operator stored at t return x  y }

  23. Binary Tree Implementation • Can be implemented using an array • Imagine that the tree is complete • Imagine that each node is given an integer label (starting with one) in sequential level-order fashion • Then A[i] contains Nodei • More Formal Definition • Let N(t) be the number (or index) of node t • If r is the root node then N(r) = 1 • If u is the left child of v then N(u) = 2*N(v) • If u is the right child of v then N(u) = 2*N(v) + 1

  24. A B C D E F G 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Binary Tree Implementation

  25. Binary Tree ImplementationSequential class BinaryTree { // what variables go here??? BinaryTree() { } public boolean contains() { } // print the items in-order public void print() { } } Complete the code to the left. Note that a generic Tree interface is usually not provided.

  26. Binary Tree Implementation • Can be implemented using linked nodes • A node keeps track of its’ left and right children • A node keeps track of its’ parent • A node stores a data element class BinaryTreeNode { private BinaryTreeNode parent, left, right; private Object data; }

  27. Binary Tree Implementation • root node has null parent • leaf nodes have null left/right children • all nodes contain a data element (not shown)

  28. Binary Tree Algorithms • The BinaryTreeNode is a recursive data structure. Tree algorithms are therefore easy to write in a recursive manner (based on the BinaryTreeNode class…not the BinaryTree class!) • Height • Size • Find

  29. Binary Search Trees • A binary search tree is a binary tree with an additional property For every node N in the tree, the values of all the nodes in the left subtree of N are less-than-or-equal-to the value of N and the values of all the nodes in the right subtree of N are greater-than-or-equal-to the value of N

  30. Operations to Support • Binary Search Trees should support fast • insertion • deletion • searching (duh) • size • isEmpty

  31. BST Auxilliary Methods algorithm TreeNode findMin(TreeNode t) { INPUT: TreeNode t which can’t be null. OUTPUT: the node containing the smallest value in t if t has a left child return findMin(t.left) else return t } algorithm TreeNode findMax(TreeNode t) { INPUT: TreeNode t which can’t be null. OUTPUT: the node containing the smallest value in t if t has a right child return findMax(t.right) else return t }

  32. BST Auxilliary Methods algorithm TreeNode findNext(TreeNode t) { INPUT: TreeNode t OUTPUT: the node occurring after t in an in-order listing of nodes if t is null return null if t has a right child return findMin(right child of t) else while t has a parent and t is the right child of its parent t = parent if t has a parent return t.parent else return null }

  33. BST Insertion algorithm TreeNode insert(Comparable v, TreeNode t) { INPUT: a comparable object v and binary tree node t OUTPUT: the TreeNode that is the root of the tree with v having been inserted if(t == null) return new TreeNode(v) if(v.compareTo(t.value) <= 0) { t.left = insert(v, t.left) } else { t.right = insert(v, t.right) } } Note that BST’s can only store objects that are “comparable”!

  34. BST Search algorithm boolean contains(Comparable v, TreeNode t) { INPUT: a comparable object v and binary tree node t OUTPUT: true if t contains v and false otherwise if(t == null) return false if v.compareTo(t.value) == 0 return true else if v.compareTo(t.value) <= 0 return contains(v, t.left) else return contains(v, t.right) }

  35. BST Removal algorithm TreeNode delete(Comparable v, TreeNode t) { INPUT: a comparable object v and binary tree node t OUTPUT: The root node of the tree with v removed if(t == null) return null if v.compareTo(t.value) < 0 t.left = delete(v, t.left) else if v.compareTo(t.value) > 0 t.right = delete(v, t.right) else if t has 2 children t.data = findMin(t.right).data t.right = delete(t.data, t.right) else if t has a left child t = t.left else t = t.right return t }

  36. Performance Method Worst Case Average void insert(Comparable element) O(N) O(Log N) boolean contains(Comparable element) O(N) O(Log N) void delete(Comparable element) O(N) O(Log N) int size() O(1) O(1) boolean isEmpty() O(1) O(1) Note: The “average” case makes certain assumptions about the way the tree is generated

  37. Self AdjustingBinary Search Trees • Insertions/removals may “deepen” and “unbalance” a binary search tree. • Self-adjusting binary search trees automatically restore balance after each insertion/removal by performing a series of rotations. • Self-adjusting binary search trees insure good worst-case performance.

  38. AVL Trees • Definition: An AVL tree is a binary search tree with a balance condition. For every node of the tree, the height of the left and right sub-trees can differ by at most 1 Note that the height of an empty tree is defined to be -1

  39. 3 3 44 44 0 2 1 2 17 78 17 78 1 0 1 0 0 50 88 32 50 88 0 0 0 0 48 62 41 62 AVL Examples Is this an AVL tree? • No. It is not a binary search tree since node 41 is not greater-than-or-equal-to node 44 but is in the right subtree of node 44. Is this an AVL tree? • No. The difference in the heights of the root node is greater than 1

  40. h = 0 h = 1 h = 2 h = 3 AVL Trees Given an AVL tree of height h, what is the minimal number of nodes that the tree may contain?

  41. AVL Trees • Proposition: The height of an AVL tree containing N elements is O(Log N) • Let n(h) be the minimal number of nodes of an AVL tree of height h • n(0) = 1 • n(1) = 2 • n(2) = 4 • n(h) = 1 + n(h-1) + n(h-2) for values of h > 2 • n(h) is strictly increasing therefore n(h-1) > n(h-2) • Replace n(h-1) with n(h-2) in the above formula • n(h) > 1 + n(h-2) + n(h-2) • n(h) > 1 + 2 * n(h-2) • n(h) > 2*n(h-2) • Note that n(h-2) > 2*n(h-4) so n(h) > 4*n(h-4) in general then • n(h) > 2i*n(h-2i) (The minimal # of nodes for any i such that h-2i>=0) • Choose i = Floor(h/2) to utilize our base-case information • n(h) > 2Floor(h/2)*n(h-2*Floor(h/2)) • n(h) > 2Floor(h/2)*n(0) (since h-2*Floor(h/2) may be 1) • n(h) > 2Floor(h/2) • Taking the Log of both sides results in • Log(n(h)) > h/2 • This says that the height of an AVL tree is O(Log(N)) where N is the size of the AVL tree

  42. Insertion into an AVL Tree algorithm insert(Key K) Insert K into the AVL tree using the straightforward binary search tree algorithm As you insert, push each node visited onto a stack. Don’t need to push Ks’ node onto the stack. Let X,Y,and Z be NULL valued nodes while the stack is not empty X = Y Y = Z Z = s.pop() if Z is not balanced then rotate(X,Y,Z) return algorithm rotate(Nodes X, Y, Z) Let A, B, and C be an IN-ORDER listing of nodes X,Y,Z Let T0, T1, T2 and T3 by an IN-ORDER listing of the subtrees of X,Y and Z (don’t include X,Y or Z) Replace Z with B Make A the left child of B Make T0 the left child of A and T1 the right child of A Make C the right child of B Make T2 the left child of C and T3 the right child of B

  43. C A B B B B A A C C T3 T0 A C T2 T1 T0 T0 T1 T1 T2 T2 T3 T3 T1 T2 T0 T3 C A A C T0 T3 B B T0 T3 T1 T2 T1 T2 Four Possible Rotations(2 cases + symmetry) Single Right Rotation Single Left Rotation Single Rotations Double Rotations Right-Left Rotation : Single Right Rotation, and Single Left Rotation Left-Right Rotation : Single Left Rotation, and Single Right Rotation

  44. AVL Removal • Removals of a node may unbalance the tree. • The only nodes that may become unbalanced are those nodes on the path from root to the removed node • Multiple rotations may be required to re-balance • Let Z be the first unbalanced node on the path from deleted to the root • Let Y be the child of Z not on the “removal path” • Let X be the tallest child of Y • Rotate nodes X, Y, and Z and repeat

  45. 31 83 45 12 95 5 18 39 51 99 14 Removal Example 62 71 Remove 62!

  46. AVL Performance Method Runtime void insert(Comparable element) O(Log N) boolean contains(Comparable element) O(Log N) void delete(Comparable element) O(Log N) int size() O(1) boolean isEmpty() O(1)

  47. Principle of Locality Splay Tree BST node is most efficient to search? A Splay Tree is a BST in which the most recently accessed node is at the root.

  48. Q: How to move node Z to the root? Three Cases A: Use rotations Moving a node to the root Zig-Zig Case Zig-Zag Case Root Case

  49. Use double rotation Zig-zag Case Before After

  50. Rotate twice, but different Zig-Zig Case Before After

More Related