1 Introduction to trees Instructor: Dimitrios Kosmopoulos.

Slides:



Advertisements
Similar presentations
Chapter 7. Binary Search Trees
Advertisements

Comp 122, Spring 2004 Binary Search Trees. btrees - 2 Comp 122, Spring 2004 Binary Trees  Recursive definition 1.An empty tree is a binary tree 2.A node.
Topic 23 Red Black Trees "People in every direction No words exchanged No time to exchange And all the little ants are marching Red and black antennas.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
1 Trees most slides taken from Mike Scott, UT Austin.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
Binary Trees A binary tree is made up of a finite set of nodes that is either empty or consists of a node called the root together with two binary trees,
1 Chapter 7 Trees. 2 What is a Tree In computer science, a tree is an abstract model of a hierarchical structure A tree consists of nodes with a parent-child.
Trees COMP53 Oct 31, What is a Tree? A tree is an abstract model of a hierarchical structure A tree consists of nodes with a parent-child relation.
Trees1 Part-C Trees Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery.
Chapter 12 Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define trees as data structures Define the terms.
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Trees.
Version TCSS 342, Winter 2006 Lecture Notes Trees Binary Trees Binary Search Trees.
Advanced Data Structures and Algorithms COSC-600 Lecture presentation-6.
Lecture Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion to process trees.
Searching: Binary Trees and Hash Tables CHAPTER 12 6/4/15 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education,
S EARCHING AND T REES COMP1927 Computing 15s1 Sedgewick Chapters 5, 12.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
CMSC 341 Introduction to Trees. 8/3/2007 UMBC CMSC 341 TreeIntro 2 Tree ADT Tree definition  A tree is a set of nodes which may be empty  If not empty,
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
CSIT 402 Data Structures II
Topic 17 Introduction to Trees
1 Introduction to trees Instructor: Dimitrios Kosmopoulos.
Compiled by: Dr. Mohammad Omar Alhawarat
Starting at Binary Trees
Topic 19 Binary Search Trees "Yes. Shrubberies are my trade. I am a shrubber. My name is 'Roger the Shrubber'. I arrange, design, and sell shrubberies."
Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child.
Red–black trees.  Define the red-black tree properties  Describe and implement rotations  Implement red-black tree insertion  We will skip red-black.
1 Binary Trees (7.3) CSE 2011 Winter November 2015.
Trees1 Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery © 2010 Goodrich, Tamassia.
Trees1 Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery © 2013 Goodrich, Tamassia, Goldwasser.
CH 7. TREES ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA AND MOUNT (WILEY.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
CMSC 341 Introduction to Trees. 2/21/20062 Tree ADT Tree definition –A tree is a set of nodes which may be empty –If not empty, then there is a distinguished.
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
Rooted Tree a b d ef i j g h c k root parent node (self) child descendent leaf (no children) e, i, k, g, h are leaves internal node (not a leaf) sibling.
Tree Data Structures. Heaps for searching Search in a heap? Search in a heap? Would have to look at root Would have to look at root If search item smaller.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
CS 307 Fundamentals of Computer ScienceRed Black Trees 1 Topic 19 Red Black Trees "People in every direction No words exchanged No time to exchange And.
CH 7 : TREE ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA AND MOUNT (WILEY.
CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
1 Binary Search Trees  Average case and worst case Big O for –insertion –deletion –access  Balance is important. Unbalanced trees give worse than log.
© 2004 Goodrich, Tamassia Trees1 Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery.
Topic 18 Binary Trees 樹高千丈﹐落葉巋根 "A tree may grow a thousand feet tall, but its leaves will return to its roots." -Chinese Proverb.
Trees CSIT 402 Data Structures II 1. 2 Why Do We Need Trees? Lists, Stacks, and Queues are linear relationships Information often contains hierarchical.
1 CMSC 341 Introduction to Trees Textbook sections:
Red-Black Trees an alternative to AVL trees. Balanced Binary Search Trees A Binary Search Tree (BST) of N nodes is balanced if height is in O(log N) A.
What is a Tree? Formally, we define a tree T as a set of nodes storing elements such that the nodes have a parent-child relationship, that satisfies the.
Binary Search Trees What is a binary search tree?
Trees Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery
Binary Trees "A tree may grow a thousand feet tall, but its leaves will return to its roots." -Chinese Proverb.
CMSC 341 Introduction to Trees.
Week 11 - Friday CS221.
Binary Search Trees -Monty Python and The Holy Grail
Topic 18 Binary Search Trees
Binary Trees, Binary Search Trees
Binary Tree and General Tree
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
Data Structures and Database Applications Binary Trees in C#
Topic 19 Binary Search Trees
Topic 18 Binary Trees "A tree may grow a thousand feet tall, but its leaves will return to its roots." -Chinese Proverb.
Topic 23 Red Black Trees "People in every direction No words exchanged No time to exchange And all the little ants are marching Red and Black.
Trees CMSC 202, Version 5/02.
CMSC 202 Trees.
Trees.
Data Structures II AP Computer Science
Trees.
NATURE VIEW OF A TREE leaves branches root. NATURE VIEW OF A TREE leaves branches root.
Presentation transcript:

1 Introduction to trees Instructor: Dimitrios Kosmopoulos

Why trees ?  Describe dynamic properties of algorithms  Build and use explicit data structures that are concrete realizations of trees

4 Definitions  A tree is a data structure accessed beginning at a root node –Each node is either a leaf or an internal node –An internal node has 1 or more children, nodes that can be reached directly from that internal node. –The internal node is said to be the parent of its child nodes root node leaf nodes internal nodes

5 Formal Definition of a Tree  A tree is either –empty (no nodes) or –a root connected to 0 or more trees (called sub trees) root sub tree Asub tree B

6 Properties of Trees and Nodes  siblings: two nodes that have the same parent  edge: the link from one node to another  path length: the number of edges that must be traversed to get from one node to another root siblings edge path length from root to this node is 3

7 More Properties of Trees  There is precisely one path connecting any two nodes  If the above condition is not satisfied then we have a graph

8 More Properties of Trees  depth: the path length from the root of the tree to this node  height of a node: The maximum distance (path length) of any leaf from this node –a root has a height of 0 –the height of a tree is the height of the root of that tree  descendants: any nodes that can be reached via 1 or more edges from this node  ancestors: any nodes for which this node is a descendant

9 Tree Visualization DBCFEAGHJIKLMNO

10 Tree example: Arithmetic Expression Tree  Binary tree associated with an arithmetic expression –internal nodes: operators –external nodes: operands  Example: arithmetic expression tree for the expression (2  ( a  1)  (3  b))    2 a1 3b

11 Tree example: Decision Tree  Binary tree associated with a decision process –internal nodes: questions with yes/no answer –external nodes: decisions  Example: dining decision Want a fast meal? How about coffee?On expense account? StarbucksSpike’sAl FornoCafé Paragon Yes No YesNoYesNo

12 Binary Trees  There are many variations on trees  A popular one is the binary trees  binary tree: a tree with at most two children for each node –the possible children are normally referred to as the left and right child parent left child right child

13 Full Binary Tree  full binary tree: a binary tree in which each node has exactly 2 or 0 children

14 Perfect Binary Tree  perfect binary tree: a binary tree with all leaf nodes at the same depth. All internal nodes have exactly two children.  a perfect binary tree has the maximum number of nodes for a given height  a perfect binary tree has 2 (n+1) - 1 nodes where n is the height of a tree –height = 0 -> 1 node –height = 1 -> 3 nodes –height = 2 -> 7 nodes –height = 3 -> 15 nodes

Implementation class Node { Item item; Node l; Node r; Node(Item v, Node l, Node r) { this.item = v; this.l = l; this.r = r; } }

16 A Binary Node class public class BNode {private Object myData; private BNode myLeft; private BNode myRight; public BNode(); public BNode(Object data, Bnode left, BNode right) public Object getData() public BNode getLeft() public BNode getRight() public void setData(Object data) public void setLeft(BNode left) public void setRight(BNode right) }

Mathematical properties - 1  A binary tree with N internal nodes has N+1 external nodes  proof : induction Induction 1.check for N=0 (or N=1), 2.assume that it holds for N=k, 3.prove that it holds for N=k+1 (or N)

Mathematical properties - 2  A binary tree with N internal nodes has 2N links: –N-1 links to internal nodes and –N+1 to external nodes

Mathematical properties - 3  The height of any binary tree with N-1 internal nodes is at least logN and at most N- 1

20 Binary Tree Traversals  Many algorithms require all nodes of a binary tree be visited and the contents of each node processed.  There are 4 traditional types of traversals –preorder traversal: process the root, then process all sub trees (left to right) –in order traversal: process the left sub tree, process the root, process the right sub tree –post order traversal: process the left sub tree, process the right sub tree, then process the root –level order traversal: starting from the root of a tree, process all nodes at the same depth from left to right, then proceed to the nodes at the next depth.

21 Results of Traversals  To determine the results of a traversal on a given tree draw a path around the tree. –start on the left side of the root and trace around the tree. The path should stay close to the tree pre order: process when pass down left side of node in order: process when pass underneath node post order: process when pass up right side of node

Pre-order traversal private void traverseR(Node h) { if (h == null) return; h.item.visit(); traverseR(h.l); traverseR(h.r); } void traverse() { traverseR(root); }

In order, post order private void traverseR(Node h) { if (h == null) return; traverseR(h.l); h.item.visit(); traverseR(h.r); } private void traverseR(Node h) { if (h == null) return; traverseR(h.l); traverseR(h.r); h.item.visit(); }

Examples

Stack equivalent –pre order private void traverseS(Node h) { NodeStack s = new NodeStack(max); s.push(h); while (!s.empty()) { h = s.pop(); h.item.visit(); if (h.r != null) s.push(h.r); if (h.l != null) s.push(h.l); } void traverseS() { traverseS(root); }

Queue instead of stack private void traverseQ(Node h) { NodeQueue q = new NodeQueue(max); q.put(h); while (!q.empty()) { h = q.get(); h.item.visit(); if (h.l != null) q.put(h.l); if (h.r != null) q.put(h.r); } void traverseQ() { traverseQ(root); }

Level order traversal

Tree parameters computation private static int count(Node h) { if (h == null) return 0; return count(h.l) + count(h.r) + 1; } int count() { return count(root); } private static int height(Node h) { if (h == null) return -1; int u = height(h.l), v = height(h.r); if (u > v) return u+1; else return v+1; } int height() { return height(root); }

29 Tree Traversals DCFAGHJKLMNO

Searching in Graphs  Trees are special cases of graphs  In graphs multiple paths between nodes may exist

Depth first  We follow each edge in the graph, with one of two possible outcomes: –if the edge takes us to a node that we have already visited, we ignore it; –if it takes us to a node that we have not yet visited, we follow it there via a recursive call.  The set of all edges that we follow in this way forms a spanning tree for the graph.

Depth first private void dfs(int k) { visit(k); visited[k] = true; for (Node t = adj[k]; t != null; t = t.next) if (!visited[t.v]) dfs(t.v); } A stack can be alternatively used

Depth first 0: 7, 5, 2, 1, 6 7: 1, 2, 0, 4 4: 6, 5, 7 3 3: 5, 4 1: 7,0 2: 7, 0 6: 4, 0 5: 0, 4, 3

Time  Depth first search requires time proportional to V+E, where V is the vertices, E is the nodes, using the adjacency lists representation

Breadth first if we use a queue instead of a stack, then we have breadth-first search, which is analogous to level- order traversal in a tree. void bfs(int k) { intQUEUE q = new intQUEUE(V*V); q.put(k); while (!q.empty()) if (!visited[k = q.get()]) { Node t; visit(k); visited[k] = true; for (t = adj[k]; t != null; t = t.next) if (!visited[t.v]) q.put(t.v); }

Breadth first 0: 7, 5, 2, 1, 6 7: 1, 2, 0, 4 4: 6, 5, 7 3 3: 5, 4

39 Recursive methods for  Preorder preorder(v) {output(v) If(v.left!=null) Preorder(v.left) If(v.right!=null) Preorder(v.right) }  Postorder  Inorder  By-level (breadth-first)

40 Example: Evaluate Arithmetic Expressions  Specialization of a postorder traversal –recursive method returning the value of a subtree –when visiting an internal node, combine the values of the subtrees Algorithm evalExpr(v) if v is leaf return v.getdata () else x  evalExpr(v.left) y  evalExpr(v.right)   operator stored at v return x  y   

41 Implementation 1: Linked Structure for Binary Trees  A node is represented by an object storing –Element –Parent node –Left child node –Right child node  Node objects implement the Position ADT B D A CE   BADCE 

42 Implementation 2: Array-Based Representation of Binary Trees  nodes are stored in an array … let rank(node) be defined as follows: rank(root) = 1 if node is the left child of parent(node), rank(node) = 2*rank(parent(node)) if node is the right child of parent(node), rank(node) = 2*rank(parent(node)) A HG FE D C B J

43 Binary Search Trees

44 Linked List Access  Accessing a item from a linked list takes O(N) time for an arbitrary element  Binary trees can improve upon this and reduce access to O( log N ) time for the average case  Expands on the binary search technique and allows insertions and deletions  Worst case degenerates to O(N) but this can be avoided by using balanced trees (AVL, Red-Black)

45 Binary Search Trees  A binary tree is a tree where each node has at most two children, referred to as the left and right child  A binary search tree is a binary tree where every node's left subtree holds values less than the node's value, and every right subtree holds values greater.  A new node is added as a leaf. parent left child right child root

46 Implementation of Binary Node public class BSTNode {private Comparable myData; private BSTNode myLeft; private BSTNode myRightC; public BinaryNode(Comparable item) {myData = item;} public Object getValue() {return myData;} public BinaryNode getLeft() {return myLeft;} public BinaryNode getRight() {return myRight;} public void setLeft(BSTNode b) {myLeft = b;} // setRight not shown }

47 Performance of Binary Trees  For the three core operations (add, access, remove) a binary search tree (BST) has an average case performance of O(log N)  Even when using the naïve insertion / removal algorithms  no checks to maintain balance  balance achieved based on the randomness of the data inserted

48 Sample Insertion  100, 164, 130, 189, 244, 42, 141, 231, 20, 153 (from HotBits: If you insert 1000 random numbers into a BST using the naïve algorithm what is the expected height of the tree? (Number of links from root to deepest leaf.)

49 Worst Case Performance  In the worst case a BST can degenerate into a singly linked list.  Performance goes to O(N) 

50 Properties of a BST  The minimum value is in the left most node  The maximum value is in the right most node –useful when removing an element from the BST  An inorder traversal of a BST provides the elements of the BST in ascending order

51 More on Implementation  Many ways to implement BSTs  Using nodes is just one and even then many options and choices public class BinarySearchTree {private TreeNode root; private int size; public BinarySearchTree() {root = null; size = 0; }

52 Add an Element, Recursive

53 Add an Element, Iterative

54 Remove an Element  Three cases –node is a leaf, 0 children (easy) –node has 1 child (easy) –node has 2 children (interesting)

55 Red Black Trees

56 Binary Search Trees  Average case and worst case Big O for –insertion –deletion –access  Balance is important. Unbalanced trees give worse than log N times for the basic tree operations  Can balance be guaranteed?

57 Red Black Trees  A BST with more complex algorithms to ensure balance  Each node is labeled as Red or Black.  Path: A unique series of links (edges) traverses from the root to each node. –The number of edges (links) that must be followed is the path length  In Red Black trees paths from the root to elements with 0 or 1 child are of particular interest

58 Paths to Single or Zero Child Nodes  How many?

59 Red Black Tree Rules 1.Every node is colored either Red or black 2.The root is black 3.If a node is red its children must be black. (a.k.a. the red rule) 4.Every path from a node to a null link must contain the same number of black nodes (a.k.a. the path rule)

60 Example of a Red Black Tree  The root of a Red Black tree is black  Every other node in the tree follows these rules: –Rule 3: If a node is Red, all of its children are Black –Rule 4: The number of Black nodes must be the same in all paths from the root node to null nodes

61 Red Black Tree?

62 Red Black Tree? Perfect? Full? Complete?

63 Implications of the Rules  If a Red node has any children, it must have two children and they must be Black. (Why?)  If a Black node has only one child that child must be a Red leaf. (Why?)  Due to the rules there are limits on how unbalanced a Red Black tree may become. –on the previous example may we hang a new node off of the leaf node that contains 0?

64 Properties of Red Black Trees  If a Red Black Tree is complete, with all Black nodes except for Red leaves at the lowest level the height will be minimal, ~log N  To get the max height for N elements there should be as many Red nodes as possible down one path and all other nodes are Black –This means the max height would be < 2 * log N –see example on next slide

65 Max Height Red Black Tree

66 Maintaining the Red Black Properties in a Tree  Insertions  Must maintain rules of Red Black Tree.  New Node always a leaf –can't be black or we will violate rule 4 –therefore the new leaf must be red –If parent is black, done (trivial case) –if parent red, things get interesting because a red leaf with a red parent violates rule 3

67 Insertions with Red Parent - Child Must modify tree when insertion would result in Red Parent - Child pair using color changes and rotations.

68 Case 1  Suppose sibling of parent is Black. –by convention null nodes are black  In the previous tree, true if we are inserting a 3 or an 8. –What about inserting a 99? Same case?  Let X be the new leaf Node, P be its Red Parent, S the Black sibling and G, P's and S's parent and X's grandparent –What color is G?

69 Case 1 - The Picture G P S E D X C A B Relative to G, X could be an inside or outside node. Outside -> left left or right right moves Inside -> left right or right left moves

70 Fixing the Problem G P S E D X C A B If X is an outside node a single rotation between P and G fixes the problem. A rotation is an exchange of roles between a parent and child node. So P becomes G's parent. Also must recolor P and G.

71 Single Rotation P X G S C A B E D Apparent rule violation?

72 Case 2  What if X is an inside node relative to G? –a single rotation will not work  Must perform a double rotation –rotate X and P –rotate X and G G P S E D X A B C

73 After Double Rotation X P G S C A B E D Apparent rule violation?

74 Case 3 Sibling is Red, not Black G P S E D X B C A Any problems?

75 Fixing Tree when S is Red  Must perform single rotation between parent, P and grandparent, G, and then make appropriate color changes P X G E C B A S D

76 More on Insert  Problem: What if on the previous example G's parent had been red?  On the way down the tree, if we see a node X that has 2 Red children, we make X Red and its two children black. –if recolor the root, recolor it to black –the number of black nodes on paths below X remains unchanged –If X's parent was Red then we have introduced 2 consecutive Red nodes.(violation of rule) –to fix, apply rotations to the tree, same as inserting node

77 Example of Inserting Sorted Numbers  Insert 1. A leaf so red. Realize it is root so recolor to black. 1

78 Insert 2 12 make 2 red. Parent is black so done.

79 Insert Insert 3. Parent is red. Parent's sibling is black (null) 3 is outside relative to grandparent. Rotate parent and grandparent 213

80 Insert On way down see 2 with 2 red children. Recolor 2 red and children black. Realize 2 is root so color back to black 2134 When adding 4 parent is black so done.

81 Insert 's parent is red. Parent's sibling is black (null). 5 is outside relative to grandparent (3) so rotate parent and grandparent then recolor

82 Finish insert of

83 Insert On way down see 4 with 2 red children. Make 4 red and children black. 4's parent is black so no problem.

84 Finishing insert of 's parent is black so done.

85 Insert 's parent is red. Parent's sibling is black (null). 7 is outside relative to grandparent (5) so rotate parent and grandparent then recolor

86 Finish insert of

87 Insert On way down see 6 with 2 red children. Make 6 red and children black. This creates a problem because 6's parent, 4, is also red. Must perform rotation.

88 Still Inserting Recolored now need to rotate

89 Finish inserting Recolored now need to rotate

90 Insert On way down see 4 has two red children so recolor 4 red and children black. Realize 4 is the root so recolor black

91 Finish Inserting After rotations and recoloring

92 Insert On way down see 8 has two red children so change 8 to red and children black 10

93 Insert Again a rotation is needed.

94 Finish inserting