# 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 ),

## Presentation on theme: "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 ),"— Presentation transcript:

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 ), 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 always only 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. level 0 level 1 level 2 level 3 root internal nodes leaves parent and child

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

5 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 14 10 15128 16 7 9 1113 18

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 Binary Trees: Array Representation 14 10 15128 16 7 9 1113 18 1 23 456 7 8 910 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 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 Tree Traversal: PreOrder

11 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 Tree Traversal: Non-recursive

12 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 Tree Traversal: PostOrder

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 Binary Search Trees: Examples C AD 14 10 1511818 16 14 10 15118 16 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 Binary Search Trees: Inorder 14 10 1511818 16 print(8) print(10) print(11) print(14) print(15) print(16) print(18) Inorder visits and prints: Example:

18 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 ? L P R

19 Searching for a key in a BST search(root, x) compare x to key of root: - if x = key return - if x search in L - if x > key => search in R search L or R in the exact same way Example: 14 10 15118 16 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 Inserting a new key in a BST 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: 10 8 5 3 4 2 9 Insert 4?

22 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 C 2) Insert A C A

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

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

25 Sorting with a BST 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? C A B L M Example: A B C L M Inorder visit prints:

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 x delete xBST property maintained q r q r p p

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

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

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

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 height (or depth) of tree = 1 + maximum_level (1+3=4)

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 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 h

34 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 LR h h+1 By inductive hypothesis the new number of nodes is (2 - 1) + (2 -1) + 1 = 2 - 1 ……proved! hhh+1

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

36 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!! Complexity of Searching with BST

37 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: Complexity of Searching with BST Possible values for the number of steps j are 1, 2, …, h as we assume that key search value is equally to occur

Download ppt "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 ),"

Similar presentations