# trees, trees, and more trees

Post on 20-Jan-2016

31 views

Embed Size (px)

DESCRIPTION

Trees, Trees, and More Trees. Trees, Trees, and More Trees. - PowerPoint PPT PresentationTRANSCRIPT

Trees, Trees, and More Trees

Trees, Trees, and More TreesBy looking at forests of terms, awesome animations, and complete examples, we hope to get at the root of trees. Hopefully, each of you leaves with something new and exciting learned about trees. Try not to lose sight of the forest through the trees. Sorry, this was all very bad. I hope it didnt leaf a bad taste in your mouth.

TREESA binary tree is a set of elements that is either empty or contains a single element (the root of the tree) and whose remaining elements are partitioned into two disjoint subsets, each of which itself is a binary tree.

HDAGEPLWRrootRight subtreerooted at PLeft subtree

HDAGEPLWRrootlevel 0level 1level 2level 3Right subtreeLeft subtree

Full Binary Tree of Level n

ABFCEDJIHKLMGNOA is the root of the tree.A is the parent of B and C.A is an ancestor of all nodes.

ABFCEDJIHKLMGNOB and C are siblings.J is a descendent of B.

Height of binary tree :Number of nodes on the longest path from the root to a leaf.

The height of the empty tree is 0.

The height of a single node tree is 1.Note: (not an AP term) the definition of height is not from some cs bible some will define other ways

ABFCEDJIHKLMGNOHeight of binary tree ?

ABFCEDJIHKLMGNOHeight of binary tree ?Height = 4

Full Binary : A recursive definition A binary tree is full if the tree is emptyor if the tree's left and right subtrees have the same height and both are full

Binary Search TreeA binary search tree : every node in the left subtree is less than or equal to its parent node.Every node in the right subtree is greater than its parent nodeWhen the tree is traversed in order, the values of the nodes will be in order.

To insert to a binary search tree:if value less thango leftelsego right

15Insert :15 8 25 6 14 24 20 22 30 13 26

15Insert :14 8 25 6 14 24 20 22 30 13 268

15Insert :14 8 25 6 14 24 20 22 30 13 26825

15Insert :15 8 25 6 14 24 20 22 30 13 268256

15Insert :15 8 25 6 14 24 20 22 30 13 26825614

15Insert :15 8 25 6 14 24 20 22 30 13 2682561424

15Insert :15 8 25 6 14 24 20 22 30 13 268256142420

15Insert :15 8 25 6 14 24 20 22 30 13 26825614242022

15Insert :15 8 25 6 14 24 20 22 30 13 2682561424202230

15Insert :15 8 25 6 14 24 20 22 30 13 268256142420223013

15Insert :15 8 25 6 14 24 20 22 30 13 26825614242022302613

Tree TraversalsSee also: animations on web site

ABFCEDJIHKLMGNOInorder traversal of a binary treeleftrootright

ABFCEDJIHKLMGNOInorder traversal of a binary treeleftrootrightH D I B J E K A L F M C N G O

ABFCEDJIHKLMGNOPreorder traversal of a binary tree:rootleftright

ABFCEDJIHKLMGNOPreorder traversal of a binary tree:rootleftrightA B D H I E J KC F L M G N O

ABFCEDJIHKLMGNOPostorder traversal of a binary tree:leftrightROOT

ABFCEDJIHKLMGNOPostorder traversal of a binary tree:leftrightROOTH I D J K E B L M F N O G C A

LBFWEXAQPGTEInorder traversal ?

LBFWEXAQPGTEInorder traversal :XA B E Q L P F W T G E

LBFWEXAQPGTEPreorder traversal ?

LBFWEXAQPGTEPreorder traversal:L B X A E Q W F PG T E

LBFWEXAQPGTEPostorder traversal ?

LBFWEXAQPGTEPostorder traversal :A X Q E B P F T E G W L

breadth-first-order tree traversalROW (or level) order traversal

ABCDEFGHI

breadth-first-order tree traversalROW (or level) order traversal

ABCDEFGHIA B C D E F G H I

15Inorder Traversal ?825614242022302613

15Inorder Traversal :825614242022302613 6 8 13 14 15 20 22 24 25 26 30

15Preorder Traversal ?825614242022302613

15Preorder Traversal :82561424202230261315 8 6 14 13 25 24 20 22 30 26

15Postorder Traversal ?825614242022302613

15Postorder Traversal :8256142420223026136 13 14 8 22 20 24 26 30 25 15

15Our Tree:825614242022302613 What is the height of the tree?

15Height :825614242022302613Height = 5

Deleting a node from a binary treeLDHCAFJP

To delete a leaf...LDHCAFJP

To delete a leaf...LDHCAFJPSet appropriate parent to NULL

To delete a leaf...LDHCAFP

Deleting a node with one child...LDHCAFJP

Deleting a node with one child...LDHCAFJPMake appropriate left or right reference of parent skip over the deleted node and reference the child of the node we want to delete

Deleting a node with one child...LDHAFJP

Deleting a node with 2 children...LDHCAFJP

Deleting a node with 2 children...LDHCAFJPGo once left and as far right as you can for nodes replacement.

Deleting a node with 2 children...DHCAFJPGo once left and as far right as you can.

Binary Search TreesAP Implementation

public class TreeNode{private Object value;private TreeNode left;private TreeNode right;

public TreeNode(Object initValue){ value = initValue;left = null; right = null; }public TreeNode(Object initValue, TreeNode initLeft, TreeNode initRight){ value = initValue;left =initLeft; right = initRight; }

TreeNode.public Object getValue() { return value; }

public TreeNode getLeft() { return left; }

public TreeNode getRight() { return right; }

TreeNode.public void setValue(Object theNewValue){ value = theNewValue; }

public void setLeft(TreeNode theNewLeft){ left = theNewLeft; }

public void setRight(TreeNode theNewRight){ right = theNewRight; }

}

Implementing Binary Search Tree Classcommon behaviors(insertions, deletions, traversals, iterators)

isEmpty()insertprint (inorder traversal)search

TreeSetThe TreeSet class uses a form of balanced binary trees that guarantees that adding and removing an element takes O(log n) time.

We know that a good hashing function can give us a retrieval with efficiency O(1) but if we also wanted to be able to list our data in order, a hash table would not be the appropriate choice.

The TreeSet class implements the Set interface.

interface java.util.Set

TreeSetA TreeSet requires that its elements be comparable. compareTo is defined for the objects placed in the TreeSet.the elements in a tree are ordered For your own classes, realize the Comparable interface define compareTo or provide a Comparator (Comparator is not in AP Subset) Since a TreeSet implements Set, a TreeSet contains no duplicates.A TreeSet guarantees reasonable search performance (O(log n)) and allows for visiting the elements in order.

Nice practice with iterators.The class TreeSetWithOps has all functionality of TreeSet and also includes the methods setIntersection, setUnion, setDifference, isSubset, and isProperSubset. Implement TreeSetWithOps.

MapsA map is a data type that keeps associations between keys and values. Each key in a map has a unique value. But a value may be associated with more than one key. A mapping of your college friends to the university that they attend. ("Owen" maps to "Duke University", "Fran" maps to "Drew University").A mapping of login ids to passwords.Each association has a key mapped to a value. Associations can be put in, changed, and located in a map.

TreeMap

MapThe Map interface requires that the keySet method be implemented. The keySet method produces a Set of keys. We can visit all of the elements of a TreeMap by iterating through the keys in the set that the keyset method produces. The Map method get will return the value associated with a map key. A TreeMap keeps the elements in an order (according to the key) from smallest to largest.

Multiple Choice Sample Question:AP Course DescriptionThe following integers are inserted into an empty binary search tree in the following order.26 20 37 31 22 18 25 29 19Which traversal of the tree would produce the following output?26 20 37 18 22 31 19 25 29(A) Preorder(B) Inorder(C) Postorder(D) Reverse postorder(E) Level-by-level