Download presentation

Presentation is loading. Please wait.

Published byDaniella Hancock Modified over 4 years ago

1
Week 7 - Wednesday

2
What did we talk about last time? Recursive running time Master Theorem Introduction to trees

4
Infix to Postfix Converter

7
Allows us to determine the Big Oh running time of many recursive functions that would otherwise be difficult to determine

8
a is the number of recursive calls made b is how much the quantity of data is divided by each recursive call f(n) is the non-recursive work done at each step

14
A tree is a data structure built out of nodes with children A general tree node can have any non- negative number of children Every child has exactly one parent node There are no loops in a tree A tree expressions a hierarchy or a similar relationship

15
The root is the top of the tree, the node which has no parents A leaf of a tree is a node that has no children An inner node is a node that does have children An edge or a link connects a node to its children The level of a node is the length of the path from the root to the node plus 1 Note that some definitions leave off the plus 1 The height of the tree is the greatest level of any node A subtree is a node in a tree and all of its children

16
1 1 2 2 3 3 4 4 5 5 6 6 7 7 Root Inner Nodes Leaves

17
A binary tree is a tree such that each node has two or fewer children The two children of a node are generally called the left child and the right child, respectively

18
1 1 2 2 3 3 4 4 5 5 6 6

19
Full binary tree: every node other than the leaves has two children Perfect binary tree: a full binary tree where all leaves are at the same depth Complete binary tree: every level, except possibly the last, is completely filled, with all nodes to the left Balanced binary tree: the depths of all the leaves differ by at most 1

20
A binary search tree is binary tree with three properties: 1. The left subtree of the root only contains nodes with keys less than the root’s key 2. The right subtree of the root only contains nodes with keys greater than the root’s key 3. Both the left and the right subtrees are also binary search trees

21
4 4 2 2 5 5 1 1 3 3 6 6

23
public class Tree { private static class Node { public int key; public Node left; public Node right; } private Node root = null; … }

24
Almost all the methods we call on trees will be recursive Each will take a Node reference to the root of the current subtree Because the root is private, assume that every recursive method is called by a public, non-recursive proxy method: public Type doSomething() calls private static Type doSomething( Node root )

25
private static int smallest(Node node) Proxy: public int smallest() { return smallest( root ); } What’s the code? Use recursion!

26
private static int largest(Node node) Proxy: public int largest() { return largest( root ); } What’s the code? Use recursion!

27
private static boolean contains(Node node, int key) Proxy: public boolean contains(int key) { return contains( root, key ); } What’s the code? Use recursion!

28
private static Node add(Node node, int key) Proxy: public void add(int key) { root = add( root, key ); } What’s the code? Use recursion!

31
Implementations of other binary search tree methods Traversals of binary search trees

32
Finish Project 2 Due this Friday CS Club tonight at 6pm in E281 So totally awesome.

Similar presentations

© 2019 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google