1 / 37

Trees

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 ), UNIX directory trees, sorting trees etc We will study extensively two useful kind of trees: Binary Search Trees and Heaps.

Download Presentation

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 • Atree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems • Game trees (i.e., chess ), UNIX directory trees, sorting trees etc • We will study extensively two useful kind of trees: Binary Search Trees and Heaps

  2. Trees: Definitions • Trees have nodes. They also have edges that connect the nodes. Between two nodes there is alwaysonly one path. Tree nodes Tree edges

  3. Trees: More Definitions • Trees are rooted. Once the root is defined (by the user) all nodes have a specific level. • Trees have internal nodes and leaves. Every node (except the root) has a parent and it also has zero or more children. root level 0 level 1 internal nodes level 2 parent and child level 3 leaves

  4. Binary Trees • A binary tree is one that each node has at most 2 children

  5. 14 10 16 8 12 15 18 9 7 11 13 Binary Trees: Definitions • Nodes in trees can contain keys (letters, numbers, etc) • Complete binary tree: the one where leaves are only in last two bottom levels with bottom one placed as far left as possible

  6. Binary Trees: Array Representation • Complete Binary Trees can be represented in memory with the use of an array A so that all nodes can be accessed in O(1) time: • Label nodes sequentially top-to-bottom and left-to-right • Left child of A[i] is at position A[2i] • Right child of A[i] is at position A[2i + 1] • Parent of A[i] is at A[i/2]

  7. 14 10 16 8 12 15 18 9 7 11 13 Binary Trees: Array Representation 1 2 3 4 5 6 7 9 10 8 11 1 2 3 4 5 6 7 8 9 10 11 14 10 16 8 12 15 18 7 9 11 13 Array A:

  8. Binary Trees: Pointer Representation • To freely move up and down the tree we need pointers to parent (NIL for root) and pointers to children (NIL for leaves) typedef tree_node { int key; struct tree_node *parent; struct tree_node *left, *right; }

  9. Tree Traversal: InOrder • InOrder is easily described recursively: • Visit left subtree (if there is one) In Order • Visit root • Visit right subtree (if there is one) In Order

  10. Tree Traversal: PreOrder • Another common traversal is PreOrder. • It goes as deep as possible (visiting as it goes) then left to right. • More precisely (recursively): • Visit root • Visit left subtree in PreOrder • Visit right subtree in PreOrder

  11. Tree Traversal: Non-recursive PreOrder can also be implemented with a stack, without recursion: Stack S push root onto S repeat until S is empty v = pop S if v is not NULL visit v push v’s right child onto S push v’s left child onto S

  12. Tree Traversal: PostOrder • PostOrder traversal also goes as deep as possible, but only visits • internal nodes during backtracking. • More precisely (recursive): • Visit left subtree in PostOrder • Visit right subtree in PostOrder • Visit root

  13. Tree Traversal: LevelOrder • LevelOrder visits nodes level by level from root node: • Can be implemented easily with a queue (how??) • We will learn this is called Breadth First Search in the data • structure called graphs later in 242

  14. Binary Search Trees • A Binary Search Tree (BST) is a binary tree with the following properties: • The key of a node is always greater than the keys of the nodes in its left subtree • The key of a node is always smaller than the keys of the nodes in its right subtree

  15. 14 10 16 8 11 15 18 14 10 16 8 11 15 Binary Search Trees: Examples root root C A D root

  16. Binary Search Trees • BST is a tree with the following BST property: key.left < key.parent < key.right NOTE! When nodes of a BST are traversed by Inorder traversal the keys appear in sorted order: inorder(root) { inorder(root.left) print(root.key) inorder(root.right) }

  17. 14 10 16 8 11 15 18 Binary Search Trees: Inorder Example: Inorder visits and prints: print(8) print(10) print(11) print(14) print(15) print(16) print(18)

  18. P L R Searching for a key in a BST Picture BST’s Parent and Left/Right subtrees as follows: Problem: how do you search BST for node with key x ?

  19. search(root, x) compare x to key of root: - if x = key return - if x < key => search in L - if x > key => search in R search L or R in the exact same way Example: 14 10 16 8 11 15 Searching for a key in a BST x=8 is ??? X=17 is ???

  20. Searching for a key in a BST searchBST(root, key) /* found init to false */ if root=nil return if root.key=key then found=true return root else if key < root.key then searchBST(root.L, key) else searchBST(root.R, key) How can you rewrite searchBST non-recursively?

  21. How to insert a new key? The same procedure used for search also applies: Determine the location by searching. Search will fail. Insert new key where the search failed. Example: Inserting a new key in a BST 10 8 Insert 4? 4 3 9 2 5

  22. C C A Building a BST Build a BST from a sequence of nodes read one a time Example: Inserting C A B L M (in this order!) 1) Insert C 2) Insert A

  23. B B B L M C C C L A A A Building a BST 3) Insert B 5) Insert M 4) Insert L

  24. B L A B M C C A L M Building a BST Is there a unique BST for letters A B C L M ? NO!Different input sequences result in different trees Inserting: A B C L M Inserting: C A B L M

  25. Given a BST can you output its keys in sorted order? Visit keys with Inorder: - visit left - print root - visit right How can you find the minimum? How can you find the maximum? B L M C A Sorting with a BST Example: Inorder visit prints: A B C L M

  26. Deleting from a BST To delete node with key x first you need to search for it. Once found, apply one of the following three cases CASE A: x is a leaf p p q q x r r delete x BST property maintained

  27. r r x q q delete x L L Deleting from a BST cont. Case B: x is interior with only one subtree BST property maintained

  28. r delete x r s W Z q t BST property maintained Deleting from a BST cont. Case C: x is interior with two subtrees r x delete x q r W Z t s

  29. W Deleting from a BST cont. Case C cont: … or you can also do it like this • q < x < r • Q is smaller than the smaller element in Z • R is larger than the largest element in W r q t r Z Can you see other possible ways ? s

  30. Complexity of Searching with BST • What is the complexity of searchBST ? • It depends on: • the key x • The other data • The shape of the tree (full, arbitrary) Complexity Analysis: We are interested in best case, worst case and average case

  31. Complexity of Searching with BST level 0 level 1 level 2 level 3 (1+3=4) height (or depth) of tree = 1 + maximum_level

  32. Complexity of Searching with BST If all nodes in the tree exist then it is called a full BST If all levels are full except for the last level then it is called minimum-level BST

  33. Complexity of Searching with BST h Theorem: A full BST of height h has 2 - 1 nodes Proof: Use induction Inductive Basis: A tree of height 1 has one node (root) Inductive Hypothesis: Assume that a tree of height h has 2 - 1 nodes h

  34. L R h+1 h Complexity of Searching with BST Inductive step: Connect two trees of height h to make one of height h+1. You need to add one more node for the new root root By inductive hypothesis the new number of nodes is (2 - 1) + (2 -1) + 1 = 2 - 1 ……proved! h h h+1

  35. Complexity of Searching with BST Theorem:For a minimum level tree of height h: 2 - 1 < # of nodes < 2 - 1 h - 1 h Corollary: The tree with the smallest height with n # of nodes it has a height of h = 1 + log n n WHY? 2

  36. Complexity of Searching with BST Therefore, for a full BST with N nodes the following holds for searchBST: best time analysis ………… O(1) worst time analysis ………… O(log N) average case analysis ………… ??? We need to find what the average is!!

  37. Complexity of Searching with BST • To define what average means you need to: • Find out all possibilities and … • Determine the probability of each possibility … • that is, you need to find the expected value: Possible values for the number of steps j are 1, 2, …, h as we assume that key search value is equally to occur

More Related