intiaf41 class 21 - PowerPoint PPT Presentation

1 / 52
About This Presentation
Title:

intiaf41 class 21

Description:

Definitions: BST, add, find.. Basic implementation, code example. ... BST: motivation. Array ds allowed a fast (direct) access. ... – PowerPoint PPT presentation

Number of Views:33
Avg rating:3.0/5.0
Slides: 53
Provided by: jxia7
Category:
Tags: bst | class | intiaf41

less

Transcript and Presenter's Notes

Title: intiaf41 class 21


1
intiaf41 class 21
  • Binary Trees
  • Binary Search Trees (BST)

2
Todays Topics
  • Binary Tree review last class.
  • Binary Tree more examples.
  • Binary Search Tree motivation.
  • Definitions BST, add, find..
  • Basic implementation, code example.
  • More remove, balance, Iterators

3
Review Binary Tree
  • Instead of using linked list, (array) we
    introduce a new data structure, binary tree
  • A binary tree
  • root
  • left subtree
  • right subtree

D
F
B
G
E
C
A
4
Review Root And Subtrees
  • A non-empty tree can be split into root and
    subtrees.

root
right subtree
left subtree
5
Three-node Binary Trees
Tree of size 3
6
Review basic operations
  • Depth of a node
  • number of hops away from the root.
  • Height of a tree
  • max depth of a node (empty tree is -1).
  • Definition of Binary Tree (not a node).
  • Traverse
  • pre, post, in order
  • By level ??

7
Binary Tree Node Definition
  • private class BTN
  • private _data Object
  • private BTN _left, _right
  • public BTN(Object d,
  • BTN l, BTN r)
  • _data d _left l _right r
  • // get set left, right, data.

8
Basic Operations Binary Tree
  • public class BinaryTree
  • protected BTN _root
  • BinaryTree() _root null
  • public void add(Object data)
  • if (_root null)
  • _root new BTN(data, null, null)
  • else this.add(_root, new BTN(data))

9
Binary Tree add (random)
  • private void add(BTN curr, Object data)
  • double select Math.random()
  • if (select lt0.5) // left side
  • if(curr.get_left() null)
    curr.set_left(new BTN(data))
  • else add(curr.get_left() ,data)
  • else // right side!
  • if(curr.get_right() null)
    curr.set_right(new BTN(data))
  • else add(curr.get_right(), data)

10
Binary Tree Inorder
  • public void print_inorder(BTN curr)
  • if(curr ! null)
  • print_inorder(curr.get_left())
  • System.out.print(curr " ")
  • print_inorder(curr.get_right())

11
Binary Tree size
  • public int size() return size(_root)
  • private int size(BTN curr)
  • if(curr null) return 0
  • return 1 size(curr.get_left())
    size(curr.get_right())

12
Binary Tree element at
  • public Object elementAt(int i)
  • if(ilt0 igt size()) throw new
    RuntimeException(" ERR index out of
    range ")
  • return at(_root,i)
  • // HOW ???? in order

13
Binary Tree elementAt
  • private Object at(BTN curr, int ind)
  • int Lsize size(curr.get_left())
  • if(ind lt Lsize) return at(curr.get_left(),
    ind)
  • if(ind Lsize) return curr.get_data()
  • return at(curr.get_right(), ind - (Lsize 1)

14
Binary Tree num of nodes in a level
  • public int level_size(int level)
  • if(isEmpty() levellt0 level gt height())
    return 0
  • return level_size(_root,level)
  • // two ways

15
Binary Tree num of nodes in a level
  • private int level_size(BTN curr, int level)
  • if(curr null) return 0
  • if(level 0) return 1
  • return level_size(curr.get_left(), level-1)
    level_size(curr.get_right(),level-1)
  • \\ we can also keep the current level and compare!

16
Binary Tree public methods
  • Check your self
  • equals structure, as sets, as in-order
  • Copy constructor
  • Print by level ??? (Queue)

17
Binary Tree print by level
  • private void print_by_level1()
  • BTN n
  • Queue q new CircularQ()
  • q.enqueue(_root)
  • while ( ! q.isEmpty() )
  • n (BTN) q.dequeue()
  • System.out.print(n.get_data() " " )
  • if (n.get_left() ! null)
    q.enqueue(n.get_left())
  • if (n.get_right() ! null)
    q.enqueue(n.get_right())
  • // how would we print each level in a
    new line??

18
Binary Tree print by level
  • private void print_by_level2()
  • BTN n
  • Queue q new CircularQ()
  • q.enqueue(_root)
  • int l01,l10
  • while ( ! q.isEmpty() )
  • n (BTN) q.dequeue()
  • if (l00) l0l1l10 System.out.println()
  • l0l0-1
  • System.out.print(n.get_data() " " )
  • if (n.get_left() ! null)
    q.enqueue(n.get_left()) l1l11
  • if (n.get_right() ! null)
    q.enqueue(n.get_right()) l1l11

19
Transform Linked List Into Binary Tree
  • We can easily transform a linked list into
    different binary trees.
  • We would like to do insert by order

head
A
B
C
D
E
C
A
or
B
D
B
C
A
E
D
E
20
Binary Trees implementations
  • BTN inner class.
  • BTN vers subtree BinaryTree / BinaryTree2
  • Iterators in\post\pro order \ by level
  • Ex6 Map, Filter.

21
BST motivation
  • Array ds allowed a fast (direct) access.
  • Linked List ds allowed a fast rescale property.
  • we would like to have both
  • fast access.
  • scaleable.
  • Introducing Binary Search Tree

22
BT vers BST
  • BST a binary tree, that if traversed inorder
    sorted by order.

5
15
29
29
9
35
18
7
35
18
15
19
5
19
7
9
23
BST definition
  • Binary Search Tree (BST)
  • the key of every node in the left subtree is
    smaller than the key of this node
  • the key of every node in the right subtree is
    larger than the key of this node.
  • empty ?
  • unique ?

15
29
9
7
35
18
5
19
24
Examples Of BST
10
10
10
5
14
5
14
5
14
1
7
1
12
1
12
These are BSTs
This is not a BST
25
BST basic operations.
  • BTS class definition
  • Should we use Binary Tree
  • Order sort by, Comparator??
  • Over Riding
  • add(Object key)
  • Object find(Object key)

15
29
9
7
35
18
5
19
26
Transform Linked List Into Binary Search Tree
  • Suppose the numbers in a linked list are
  • 10, 4, 7, 15, 3, 18, 16

10
10
10
10
4
4
15
4
7
7
(1)
(2)
(3)
(4)
27
Transform Linked List Into Binary Search Tree
  • Suppose the numbers in a linked list are
  • 10, 4, 7, 15, 3, 18, 16

10
10
10
4
15
4
4
15
15
7
3
7
3
7
3
18
18
(5)
(6)
(7)
16
28
BST Definition
  • public class BST extends BinaryTree
  • public BST( ) super()
  • public BST(Object keys)
  • super()
  • for(int i0iltkeys.lengthii1)
  • this.add(keysi)
  • .

29
BST Definition
  • private static int compare(Object o1, Object
    o2)
  • int ans 0
  • double d1 ((Number)o1).doubleValue()
  • double d2 ((Number)o2).doubleValue()
  • if(d1gtd2) ans 1
  • else if(d1ltd2) ans -1
  • return ans

30
BST Definition
  • private static boolean smaller(Object b1, Object
    b2)
  • return (compare(b1, b2) lt 0)
  • private static boolean equals(Object b1, Object
    b2)
  • return (compare(b1, b2) 0)

31
Search In BST
  • It is very easy to locate an element in BST
  • Use Recursive Implementation
  • Suppose we want to search 7

10
5
14
1
7
16
32
Search In BST
  • step 1 current root 10
  • Because 7lt10, search left subtree
  • step 2 current root 5
  • Because 7 gt 5, search right subtree
  • Step 3 current root 7
  • target found

10
5
14
1
7
16
33
BST find
  • // two stage function public ? private
  • public BTN find(Object data)
  • return find(_root, data)

34
BST find
  • private Object find(BTN curr, Object key)
  • if(curr null) return null
  • if(this.equals(curr.get_data(),key))
  • return curr.get_data()
  • if (this.smaller(key, curr.get_data()))
  • return this.find(curr.get_left(), key)
  • else return this.find(curr.get_right(), key)

35
BST Definition add
  • public void add(Object data)
  • if(this.find(data) null) // not exist
  • if(_root null) _root new BTN(data)
  • else this.add(_root, data)

36
BST Definition add
  • private void add(BTN curr, Object data)
  • if (smaller(data, curr.get_data()))
  • if(curr.get_left() null)
  • curr.set_left(new BTN(data))
  • else this.add(curr.get_left(), data)
  • else
  • if(curr.get_right() null)
  • curr.set_right(new BTN(data))
  • else this.add(curr.get_right(), data)

37
Search Complexity In BST
  • In each level, only one comparison
  • Cost height of tree

worst case 3
worst case 6
38
Search Complexity In BST
  • height of a random BST with n nodes??
  • Average case ??
  • See Main2.java
  • size 18136 ? height 35
  • size 181181 ? height 43

39
BST BTN - inner class
  • public class BST
  • private BTN _root
  • public BST( ) _root null
  • private class BTN // inner (private) class.
  • .

40
Remove Method In BST
  • Remove a node from BST
  • Three cases
  • Case 1 node is a leaf
  • Case 2 node has one child
  • Case 3 node has two children

41
Remove Method Case 1
  • The node is a leaf
  • Assume we want to remove node 7
  • just modify node 7s parent child pointer

10
5
14
1
7
16
42
Remove Method Case 2
  • The node has one child
  • Assume we want to remove node 14
  • just lift up node 14s subtree

10
5
14
1
7
16
15
20
43
Remove Method Case 3
  • The node has two children
  • Assume we want to remove node 10

3
1
10
5
14
4
7
16
15
20
44
Remove Method Case 3 (cont.)
  • After removing node 10, there are two subtree for
    node 10
  • We can not simply lift left/right subtree up
  • But we know, all nodes in left subtree is less
    than all nodes in right subtree.

3
1
10
5
14
4
7
16
15
20
45
Remove Method Case 3(cont.)
  • How to merge two split subtree into one tree?
  • Find the maximum node in leftsubtree
  • Joint the right subtree into that node

3
3
3
1
10
1
10
5
1
5
7
4
5
14
14
7
4
4
7
16
14
16
15
20
16
15
20
15
20
46
Summary For Remove Method
prev
  • Step 1
  • find the node which needs to be deleted
  • prev is the parent of node to be deleted

3
node
1
10
5
14
4
7
16
15
20
47
Summary For Remove Method
  • Step 2
  • find the maximum node tmp of left subtree
  • merge the nodes right subtree to tmps right
    child

prev
3
node
prev
1
10
3
node
5
1
10
4
7
5
14
tmp
14
4
7
16
16
tmp
15
20
15
20
48
Summary For Remove Method
  • Step 3
  • lift nodes left child up

prev
prev
3
node
3
node
5
1
1
10
4
7
14
5
16
4
7
14
15
20
16
15
20
49
Advanced BTS issues
  • Balanced BST
  • Why a balanced BST is better?
  • How balanced is a BST?
  • Suggestions for simple BST Balancing

50
Advanced BTS issues
  • Iterators
  • What for?
  • What kind?
  • Suggestions for implementations
  • is Valid? (mod counter).

51
Complete Examples
  • BinaryTree.java
  • BST.java
  • Main1.java binary trees
  • Main2.java BST
  • BinaryTree2.java - binary trees - list style
  • Main3.java

52
Over View
  • Ex6 go for that.
  • Binary Trees check list of methods.
  • BST find, add, balanced
  • Advanced iterators, inner classes
Write a Comment
User Comments (0)
About PowerShow.com