9
Trees 1 Trees

Trees

Embed Size (px)

DESCRIPTION

Trees. Some Trees and Graphs. root. root. 6. 6. 5. 5. 8. 8. 3. 3. 7. 7. 9. 9. root. root. root. root. 6. 6. 5. 6. 8. 7. 7. 8. 5. 4. 3. 2. 8. 3. 7. 7. a binary tree. a tree. a tree and a. a Binary search Tree. not a tree. Terminology. parent nodes. - PowerPoint PPT Presentation

Citation preview

Page 1: Trees

Trees 1

Trees

Page 2: Trees

Trees 2

Some Trees and Graphs

6

382

7

6

3

7 8

5

7

4

8

a tree a binarytree a Binary

search Treenot a tree

7

5

6

a treeand a ...

root root root root

9

6

5

3

8

7

root

Terminology

9

6

5

3

8

7

rootparent nodes

children nodes

leaf nodes

left sub treerightsub tree

A Tree - each child nodehas a single parent node,

except for the root which hasno parent

Page 3: Trees

Trees 3

Binary Search Trees(BST)

9

6

5

3

8

7

rootBinary Trees

A tree where each node has at most 2-children

left rightdata

A binary search tree is a binary tree where the elements of the left sub-tree are smaller than the root, and the elements of the right sub-tree are larger than the root.

Standard processes on a BST:

• Traversals x 3• Insert to tree• Remove from tree• Search tree

public interface BinarySearchTree{ public void inOrderTraversal(); public void preOrderTraversal(); public void postOrderTraversal(); public void insert(Comparable item); public boolean search(Comparable item); public void remove(Comparable item);}

Page 4: Trees

Trees 4

BST t = new BST();

t.insert(new Integer(6));

t.insert(new Integer(4));

t.insert(new Integer(3));

t.insert(new Integer(8));

root 6root

6

4 root

6

4

3root

6

4

3

8

t.insert(new Integer(8));

root

6

4

3

8

root

6

4

3

8t.insert(new Integer(5));

5

4

9

6

3

8

7

root

5

boolean b = t.search(new Integer(3));

b = t.search(new Integer(5));

4

9

6

3

8

7

root

5

root

b = t.search(new Integer(2));

4

9

6

3

8

7

root

5

BST Operations

Page 5: Trees

Trees 5

Pre-order Traversal: Root - left sub tree - Right sub tree

6 - 4 - 3 - 5 - 8 - 7 - 9

Post-order Traversal: left sub tree - Right sub tree - Root

3 - 5 - 4 - 7 - 9 - 8 - 6

In-order Traversal: left sub tree - Root - Right sub tree

3 - 4 - 5 - 6 - 7 - 8 - 9

BST Operations - Traversals

4

9

6

3

8

7

root

5

What traversal technique should be employed if you wanted to save the contents of the tree in order to

recreate the same tree at a later time?

Page 6: Trees

Trees 6

BST Operations - Removing a node

6

4

5

9

3

2

remove(2) =>

6

4

59

3

2

6

4

5

9

3

2

6

4

5

9

3

2

6

4

5

9

3

2

6

4

5

9

3

2

remove(3) =>

no children

one child

remove(6)

two children

Replace by theright-most nodein the left sub-tree

Page 7: Trees

Trees 7

left rightdata

Binary Search Treesimport java.util.*;public class BST implements BinarySearchTree{ class TreeNode{ TreeNode left; TreeNode right; Comparable data; TreeNode(Comparable d){ data = d; left = null; right = null;} } TreeNode root;

public BST(){ root = null;}

public void inOrderTraversal(){ inOrderTraversalHelper(root);}

private void inOrderTraversalHelper(TreeNode node){ if (node != null){

inOrderTraversalHelper(node.left); System.out.println(node.data.toString()); inOrderTraversalHelper(node.right); } } //pre and post order traversals very similar to inorder

public boolean search(Comparable item){ return searchHelper(item,root); } private boolean searchHelper(Comparable target, TreeNode node){ if (node == null) return false; if (target.compareTo(node.data) == 0) return true; if (target.compareTo(node.data) > 0) return searchHelper(target,node.right);

return searchHelper(target, node.left); }

Animation of In order Traversal

Page 8: Trees

Trees 8

Binary Search Treespublic void insert(Comparable item){ if (search(item)) return;//prevent duplicates root = insertHelper(item,root);}private TreeNode insertHelper(Comparable item, TreeNode node){ if (node == null) return new TreeNode(item); if (node.data.compareTo(item)>0) node.left = insertHelper(item,node.left); else node.right = insertHelper(item,node.right); return node;}

public void remove(Comparable item) throws NoSuchElementException{ root = removeHelper(item,root);}private TreeNode removeHelper(Comparable item, TreeNode node)

throws NoSuchElementException{ if (node == null) throw new NoSuchElementException(); if (node.data.compareTo(item) == 0){

if (node.left == null) return node.right;if (node.right == null) return node.left;Comparable data = findRightMostDataValue(node.left);node.left = removeHelper(data, node.left);node.data = data;return node;

} if (node.data.compareTo(item) > 0) node.left = removeHelper(item, node.left); if (node.data.compareTo(item) < 0) node.right = removeHelper(item, node.right); return node;}private Comparable findRightMostDataValue(TreeNode node){ if (node.right == null) return node.data; else return findRightMostDataValue(node.right);}

Page 9: Trees

Trees 9

Why Trees?Improve Insert/Search/Delete performance over linear structures.

If the tree is “well balanced”, then expected performance:

Insert/Delete/Search O(log2n)

(Under what circumstance?)

Worst case times for a BST:

Insert/Delete/Search O(n)

(Under what circumstance?)

Other tree structures:

Goal is to grow a well balanced trees that are not too tall

• Trees where nodes have more than 2-children• Trees that detect and modify themselves when they become unbalanced• Trees that are grown from the bottom up - thereby ensuring balanced growth.