1 Theory I Algorithm Design and Analysis (2 - Trees: traversal and analysis of standard search trees) Prof. Th. Ottmann.

Slides:



Advertisements
Similar presentations
Chapter 12 Binary Search Trees
Advertisements

S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Senem Kumova Metin Spring2009 BINARY TREES && TREE TRAVERSALS Chapter 10 in A Book on C.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
Main Index Contents 11 Main Index Contents Tree StructuresTree Structures (3 slides) Tree Structures Tree Node Level and Path Len. Tree Node Level and.
1 Theory I Algorithm Design and Analysis (3 - Balanced trees, AVL trees) Prof. Th. Ottmann.
BST Data Structure A BST node contains: A BST contains
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
Data Structures Using C++ 2E Chapter 11 Binary Trees and B-Trees.
Data Structures Using C++1 Chapter 11 Binary Trees.
Data Structures Using C++1 Chapter 11 Binary Trees.
Version TCSS 342, Winter 2006 Lecture Notes Trees Binary Trees Binary Search Trees.
Chapter 19 - basic definitions - order statistics ( findkth( ) ) - balanced binary search trees - Java implementations Binary Search Trees 1CSCI 3333 Data.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.
Chapter 12. Binary Search Trees. Search Trees Data structures that support many dynamic-set operations. Can be used both as a dictionary and as a priority.
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 ),
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Tree Data Structures.
Chapter 19: Binary Trees Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
Binary SearchTrees [CLRS] – Chap 12. What is a binary tree ? A binary tree is a linked data structure in which each node is an object that contains following.
Binary Trees Definition A binary tree is: (i) empty, or (ii) a node whose left and right children are binary trees typedef struct Node Node; struct Node.
Trees, Binary Trees, and Binary Search Trees COMP171.
1 Trees 4: AVL Trees Section 4.4. Motivation When building a binary search tree, what type of trees would we like? Example: 3, 5, 8, 20, 18, 13, 22 2.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
Data Structures – Week #5 Trees (Ağaçlar). December 4, 2015Borahan Tümer, Ph.D.2 Trees (Ağaçlar) Toros GöknarıAvrupa Göknarı.
IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100I: Data.
Binary Trees In computer science, a binary tree is a tree data structure in which each node has at most two children, which are referred to as the left.
David Stotts Computer Science Department UNC Chapel Hill.
CSS446 Spring 2014 Nan Wang.  to study trees and binary trees  to understand how binary search trees can implement sets  to learn how red-black trees.
AVL Trees An AVL tree is a binary search tree with a balance condition. AVL is named for its inventors: Adel’son-Vel’skii and Landis AVL tree approximates.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
Binary Search Trees (BST)
Lecture 17: Trees and Networks I Discrete Mathematical Structures: Theory and Applications.
Binary Search Trees.  Understand tree terminology  Understand and implement tree traversals  Define the binary search tree property  Implement binary.
1 Joe Meehean. A A B B D D I I C C E E X X A A B B D D I I C C E E X X  Terminology each circle is a node pointers are edges topmost node is the root.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
1 CSE 326: Data Structures Trees Lecture 6: Friday, Jan 17, 2003.
Trees By JJ Shepherd. Introduction Last time we discussed searching and sorting in a more efficient way Divide and Conquer – Binary Search – Merge Sort.
BSTs, AVL Trees and Heaps Ezgi Shenqi Bran. What to know about Trees? Height of a tree Length of the longest path from root to a leaf Height of an empty.
Data Structure and Algorithms
Lecture 15 Nov 3, 2013 Height-balanced BST Recall:
Lec 13 Oct 17, 2011 AVL tree – height-balanced tree Other options:
UNIT III TREES.
Week 6 - Wednesday CS221.
Binary Search Tree (BST)
Tree.
Lecture 22 Binary Search Trees Chapter 10 of textbook
ITEC 2620M Introduction to Data Structures
Binary Trees, Binary Search Trees
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
AVL Trees CENG 213 Data Structures.
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
(2,4) Trees /26/2018 3:48 PM (2,4) Trees (2,4) Trees
(2,4) Trees (2,4) Trees (2,4) Trees.
(2,4) Trees 2/15/2019 (2,4) Trees (2,4) Trees.
Chapter 10 1 – Binary Trees Tree Structures (3 slides)
(2,4) Trees /24/2019 7:30 PM (2,4) Trees (2,4) Trees
Binary Trees, Binary Search Trees
Trees.
(2,4) Trees (2,4) Trees (2,4) Trees.
Binary SearchTrees [CLRS] – Chap 12.
Lecture 10 Oct 1, 2012 Complete BST deletion Height-balanced BST
1 Lecture 13 CS2013.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Presentation transcript:

1 Theory I Algorithm Design and Analysis (2 - Trees: traversal and analysis of standard search trees) Prof. Th. Ottmann

2 Binary trees for storing sets of keys (in the internal nodes of trees), such that the operations find insert delete (remove) are supported. Search tree property: All keys in the left subtree of a node p are smaller than the key of p, and the key of p is smaller than all keys in the right subtree of p. Implementation: Binary Search Trees

3 Standard trees (8) Tree structure depends on the order of insertions into the initially empty tree Height can increase linearly, but it can also be in O(log n), more precisely  log 2 (n+1)  Insert 5

4 Traversal of trees Traversal of the nodes of a tree for output for calculating the sum, average, number of keys... for changing the structure Most important traversal orders: 1.Preorder = NLR (Node-Left-Right) first visit the root, then recursively the left and right subtree (if existent) 2.Postorder = LRN 3.Inorder = LNR 4.The mirror image versions of 1-3

5 Preorder Preorder traversal is recursively defined as follows: Traversal of all nodes of a binary tree with root p in preorder: Visit p, traverse the left subtree of p in preorder, traverse the right subtree of p in preorder

6 Preorder implementation // Preorder Node-Left-Right void preOrder (){ preOrder(root); System.out.println (); } void preOrder(SearchNode n){ if (n == null) return; System.out.print (n.content+" "); preOrder(n.left); preOrder(n.right); } // Postorder Left-Right-Node void postOrder(){ postOrder(root); System.out.println (); } //...

7 Inorder The traversal order is: first the left subtree, then the root, then the right subtree: // Inorder Left-Node-Right void inOrder(){ inOrder(root); System.out.println (); } void inOrder(SearchNode n){ if (n == null) return; inOrder(n.left); System.out.print (n.content+" "); inOrder(n.right); }

8 Example Preorder: 17, 11, 7, 14, 12, 22 Postorder: 7, 12, 14, 11, 22, 17 Inorder: 7, 11, 12, 14, 17,

9 Non-recursive variants with threaded trees Recursion can be avoided if instead of null -pointers so-called thread pointers to the successors or predecessors are used Root

10 Example for Search, Insertion, Deletion

11 Sorting with standard search trees Idea: Create a search tree for the input sequence and output the keys by an inorder traversal. Remark: Depending on the input sequence, the search tree may degenerate. Complexity: Depends on internal path length Worst case: Sorted input:   (n 2 ) steps. Best case: We get a complete search tree of minimal height of about log n. Then n insertions and outputs are possible in time O(n log n). Average case: ?

12 Analysis of search trees Two possible approaches to determine the internal path length: 1. Random tree analysis, i.e. average over all possible permutations of keys to be inserted (into the initially empty tree). 2. Shape analysis, i.e. average over all structurally different trees with n keys. Difference of the expected values for the internal path: 1.  n log 2 n – 0.846·n + O(log n) 2.  n·  n + O(n)

13 Reason for the difference  Random tree analysis counts more balanced trees more often ,2,13,1,2 1,3,23,2,1 2,1,3 und 2,3,1

14 Internal path length Internal path length I: measure for judging the quality of a search tree t. Recursive definition: 1. If t is empty, then I(t) = For a tree t with left subtree t l and right subtree t r : I(t) := I(t l ) + I(t r )+ # nodes in t. Apparently:   p tI p internal node in t   p depth 1)( )(

15 Average search path length For a tree t the average search path length is defined by: D(t) = I(t)/n, n = # internal nodes in t Question: What is the size of D(t) in the best worst average case for a tree t with n internal nodes?

16 Internal path: best case We obtain a complete binary tree

17 Internal path: worst case

18 Random trees Without loss of generality, let {1,…,n} be the keys to be inserted. Let s 1,…, s n be a random permutation of these keys. Hence, the probability that s 1 has the value k, P(s 1 =k) = 1/n. If k is the first key, k will be stored in the root. Then the left subtree contains k-1 elements (the keys 1, …, k-1) and the right subtree contains n-k elements (the keys k+1, …,n).

19 Expected internal path length EI(n) : Expectation for the internal path length of a randomly generated binary search tree with n nodes Apparently we have: Behauptung: EI(n) 1.386n log 2 n n + O(logn).         n k n k n k knEIk n n nkn k n n 11 1 ))()1(( 1 ))()1(( 1 )( 1)1( 0)0(

20 Proof (1) and hence From the last two equations it follows that     n k kEI n nn 0 )(* 1 2 )1()1(        )(*2* )(*2)1()1(*)1( n k n k k n n k nn n ).( )1( 12)()2()1()1( )(*212)(*)1()1( nEI n n n n n nn nn n n nn nn n        

21 Proof (2) By induction over n it is possible to show that for all n ≥ 1: is the n-th harmonic number, which can be estimated as follows: where the so-called Euler constant. n H n  ) 1 ( 2 1 ln 2 nn nH n   nHnnEI n 3)1(2)( 

22 Proof (3) Thus, and hence, ) 1 (21ln2*)23( 2)( n nnnnnEI ... ln2 )23(log ln2 )23(log* ln2 )23(log* 2... ln2 )23( 2 )(     n n n n n n e n n n e n n n n nEI    

23 Observations Search, insertion and deletion of a key in a randomly generated binary search tree with n keys can be done, on average, in O(log 2 n) steps. In the worsten case, the complexity can be Ω(n). One can show that the average distance of a node from the root in a randomly generated tree is only about 40% above the optimal value. However, by the restriction to the symmetrical successor, the behaviour becomes worse. If n 2 update operations are carried out in a randomly generated search tree with n keys, the expected average search path is only Θ(  n).

24 Typical binary tree for a random sequence of keys

25 Resulting binary tree after n 2 updates

26 Structural analysis of binary trees Question: What is the average search path length of a binary tree with N internal nodes if the average is made over all structurally different binary trees with N internal nodes? Answer: Let I N = total internal path length of all structurally different binary trees with N internal nodes B N = number of all structurally different trees with N internal nodes Then I N /B N =

27 Number of structurally different binary trees

28 Total internal path length of all trees with N nodes For each tree t with left subtree t l and right subtree t r :

29 Summary The average search path length in a tree with N internal nodes (averaged over all structurally different trees with N internal nodes) is: 1/N · I N /B N