Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 CSE 326: Data Structures Trees Lecture 6: Friday, Jan 17, 2003.

Similar presentations


Presentation on theme: "1 CSE 326: Data Structures Trees Lecture 6: Friday, Jan 17, 2003."— Presentation transcript:

1 1 CSE 326: Data Structures Trees Lecture 6: Friday, Jan 17, 2003

2 2 Trees Material: Weiss Chapter 4 N-ary trees Binary Search Trees AVL Trees Splay Trees

3 3 Tree Jargon Nodes: A, B, …, F Root node: A Leaf nodes: B, E, F, D Edges: (A,B), (A,C), …, (C, F) Path: sequence of nodes connected by edges. Path examples: (B), (A,B), (A,C), (A,C,E), (A,C,F), (C), etc A B CD E F Questions. A tree has N nodes. How many edges does it have ? How many paths ?

4 4 Tree Jargon Length of a path = number of edges Depth of a node x = length of path from root to x Height of node x = length of longest path from x to a leaf Depth and height of tree = height of root The label of a node: A, B, C, … A B CD E F depth=0, height = 2 depth = 2, height=0

5 5 Definition and Tree Trivia Graph-theoretic definition of a Tree: A tree is a graph for which there exists a node, called root, such that: -- for any node x, there exists exactly one path from the root to x Recursive Definition of a Tree: A tree is either: a. empty, or b. it has a node called the root, followed by zero or more trees called subtrees

6 6 Implementation of Trees Obvious Pointer-Based Implementation: Node with value and pointers to children –Problem? A B CD E F

7 7 1 st Child/Next Sibling Representation Each node has 2 pointers: one to its first child and one to next sibling A B CD E F A B CD E F

8 8 Nested List Representation Each node has a pointer to a list containing its children A B CD E F A B CD E F

9 9 Example Arithmetic Expression: A + (B * (C / D) ) Tree for the above expression: Application: Arithmetic Expression Trees + A * B / C D Used in most compilers No parenthesis need – use tree structure Can speed up calculations e.g. replace / node with C/D if C and D are known Calculate by traversing tree (how?)

10 10 Traversing Trees Preorder: Root, then Children –+ A * B / C D Postorder: Children, then Root –A B C D / * + Inorder: Left child, Root, Right child –A + B * C / D + A * B / C D

11 11 void print_preorder ( TreeNode T) { if ( T == NULL ) return; print_element(T.Element()); print_preorder(T.FirstChild()); print_preorder(T.NextSibling()); } void print_preorder ( TreeNode T) { if ( T == NULL ) return; print_element(T.Element()); print_preorder(T.FirstChild()); print_preorder(T.NextSibling()); } Example Code for Recursive Preorder What is the running time for a tree with N nodes?

12 12 Binary Trees Properties –max # of leaves = 2 depth(tree) –max # of nodes = 2 depth(tree)+1 – 1 We care a lot about the depth: –max depth = n-1 –min depth = log(n) (why ?) –average depth for n nodes = (over all possible binary trees) Representation: A B DE C F HG JI TreeNode:Element LeftRight

13 13 Binary Trees Notice: we distinguish between left child and right child A BC F HG A BC F HG 

14 14 Binary Search Tree 4 121062 115 8 14 13 79 Search tree property –all keys in left subtree smaller than root’s key –all keys in right subtree larger than root’s key –result: easy to find any given key inserts/deletes by changing links

15 15 Searching in a Binary Search Tree Boolean find(int x, TreeNode T) { if ( T == NULL ) return false; if (x == T.Element) return true; if (x < T.Element) return find(x, T.Left); return find(x, T.Right); } Boolean find(int x, TreeNode T) { if ( T == NULL ) return false; if (x == T.Element) return true; if (x < T.Element) return find(x, T.Left); return find(x, T.Right); } What is the running time ? 20 92 15 5 10 30 7 17

16 16 Insert a Key TreeNode insert(int x, TreeNode T) { if ( T == NULL ) return new TreeNode(x,null,null); if (x == T.Element) return T; if (x < T.Element) T.Left = insert(x, T.Left); else T.Right = insert(x, T.Right); return T; } TreeNode insert(int x, TreeNode T) { if ( T == NULL ) return new TreeNode(x,null,null); if (x == T.Element) return T; if (x < T.Element) T.Left = insert(x, T.Left); else T.Right = insert(x, T.Right); return T; } What is the running time ? 20 92 15 5 10 30 7 17 3

17 Delete a Key 20 92 15 5 10 30 7 17 How do you delete: 17 ? 9 ? 20 ?????

18 18 FindMin TreeNode min(Node T) { if (T.Left == NULL) return T; else return min(T.Left); } TreeNode min(Node T) { if (T.Left == NULL) return T; else return min(T.Left); } 2092 155 10 307 17 How many children can the min of a node have?

19 19 Successor Find the next larger node in this node’s subtree. –When it exists, it is the next largest node in entire tree 2092 155 10 307 17 How many children can the successor of a node have? TreeNode succ(TreeNode T) { if (T.right == NULL) return NULL; else return min(T.right); } TreeNode succ(TreeNode T) { if (T.right == NULL) return NULL; else return min(T.right); }

20 20 Deletion - Leaf Case 2092 155 10 307 17 Delete(17)

21 21 Deletion - One Child Case 2092 155 10 307 Delete(15)

22 22 Deletion - Two Children Case 3092 205 10 7 Delete(5) replace node with value guaranteed to be between the left and right subtrees: the successor

23 23 Deletion - Two Children Case 3092 205 10 7 Delete(5) always easy to delete the successor – always has either 0 or 1 children!

24 24 Deletion - Two Child Case 3092 207 10 7 Delete(5) Finally copy data value from deleted successor into original node What is the cost of a delete operation ? Can we use the predecessor instead of successor ?

25 25 Cost of the Operations find, insert, delete : Need to compute height(T) For a tree T with n nodes: –height(T)  n –height(T)  log(n)(why ?) time = O(height(T))

26 26 Height of the Binary Search Tree Height depends critically on the order in which we insert the data: –E.g. 1,2,3,4,5,6,7 or 7,6,5,4,3,2,1, or 4,2,6,1,3,5,7 2 1 3 4 5 6 7 6 7 5 4 3 2 1 4 26 1357 Which insertion order corresponds to what tree ? Which tree do we prefer and why ?

27 27 The Average Depth of a BST Insert the elements 1 <2 <... < n in some order, starting with the empty tree For each permutation,  : –T  = the BST after inserting  (1),  (2),...,  (n) The Average Depth: Let’s compute it !

28 28 The Average Depth of a BST H(n) seems hard, let’s compute something else instead The internal path length of a tree T is: depth(T) = sum of all depths of all nodes in T Clearly depth(T)/n  height (T) (why ?) The average internal path length is:

29 29 The Average Depth of a BST Compute D(n) now:

30 30 The Average Depth of a BST Compute D(n) now: n D(n)= 2  i=1,n-1 D(i) + n(n – 1) (n-1) D(n-1)= 2  i=1,n-2 D(i) + (n – 1)(n – 2) n D(n) – (n – 1) D(n-1) = 2D(n-1) +2(n – 1) n D(n) = (n+1)D(n-1) + 2(n – 1) D(n)/n+1= D(n-1)/n + 2(n-1)/n(n+1) < D(n-1)/n + 2/n D(n)/n+1< 2( 1/n + 1/(n-1) +... + 1/3 + 1/2 + 1)  2log(n) D(n)=  (n log n) H(n)=  (log n)

31 31 The Average Depth of a BST What have we achieved ? The average depth of a BST is: H(n) =  (log n)

32 32

33 33 Random Input vs. Random Trees Inputs 1,2,3 3,2,1 1,3,2 3,1,2 2,1,3 2,3,1 Trees For three items, the shallowest tree is twice as likely as any other – effect grows as n increases. For n=4, probability of getting a shallow tree > 50%

34 34 Average cost The average, amortized cost of n insert/find operations is O(log(n)) But the average, amortized cost of n insert/find/delete operations can be as bad as sqrt(n) –Deletions make life harder (recall stretchy arrays) –Read the book for details Need guaranteed cost O(log n) – next time


Download ppt "1 CSE 326: Data Structures Trees Lecture 6: Friday, Jan 17, 2003."

Similar presentations


Ads by Google