binary search trees continued n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Binary Search Trees (Continued) PowerPoint Presentation
Download Presentation
Binary Search Trees (Continued)

Loading in 2 Seconds...

play fullscreen
1 / 13

Binary Search Trees (Continued) - PowerPoint PPT Presentation


  • 181 Views
  • Uploaded on

Binary Search Trees (Continued). Study Project 3 Solution Balanced Binary Search Trees Balancing Operations Reading: L&C 11.1 – 11.4. Study Project 3 Solution. Project 3 was due before class today Discuss solution. Balanced Binary Search Trees.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Binary Search Trees (Continued)' - delu


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
binary search trees continued
Binary Search Trees (Continued)
  • Study Project 3 Solution
  • Balanced Binary Search Trees
  • Balancing Operations
  • Reading: L&C 11.1 – 11.4
study project 3 solution
Study Project 3 Solution
  • Project 3 was due before class today
  • Discuss solution
balanced binary search trees
Balanced Binary Search Trees
  • The balance of a binary search tree is important for obtaining its efficiency
  • If we add 3, 5, 9, 12, 18, and 20 to a binary search tree, we get a degenerate tree
  • This is less efficient than a singly linked list because our code needs to check the null left pointer at each level while traversing it
  • Operations are O(n) instead of O(log n)
  • We want our binary search trees balanced
balanced binary search trees1
Balanced Binary Search Trees
  • Degenerate tree for a binary search tree

3

5

9

12

18

20

balancing operations
Balancing Operations
  • Brute force balancing methods work but are unnecessarily time consuming
  • We could use an in-order traversal of the tree and move everything out to an array
  • Then, we could use a recursive method to insert the middle element of the array as the root and subdivide the array into two halves
  • Eventually, we will rebuild a balanced tree
balancing operations1
Balancing Operations
  • We prefer to use balancing operations after each add or remove element operation
  • Semantics of balancing operations
    • Right rotation
    • Left rotation
    • Rightleft rotation
    • Leftright rotation
balancing operations2
Balancing Operations
  • Semantics of Right Rotation

A. Make the left child of the root the new root

B. Make former root the right child of the new root

C. Make right child of the former left child of the former root the new left child of the former root

13

7

7

7

7

15

5

13

5

13

5

13

5

10

3

15

3

15

3

10

15

10

10

Initial

Tree

Step C

3

Step A

Step B

balancing operations3
Balancing Operations
  • Semantics of Left Rotation

A. Make the right child of the root the new root

B. Make former root the left child of the new root

C. Make left child of the former right child of the former root the new right child of the former root

5

10

10

10

3

10

5

13

5

13

5

13

7

13

3

15

3

15

3

7

15

7

7

Initial

Tree

Step A

Step B

Step C

15

balancing operations4
Balancing Operations
  • Semantics of Rightleft Rotation

A. Right rotation around right child of root

B. Left rotation around root

5

5

10

3

13

3

10

5

13

10

15

7

13

3

7

15

7

15

Initial

Tree

After Right

Rotation

After Left

Rotation

balancing operations5
Balancing Operations
  • Semantics of Leftright Rotation

A. Left rotation around left child of root

B. Right rotation around root

13

13

7

5

15

7

15

5

13

3

7

5

10

3

10

15

10

3

Initial

Tree

After Left

Rotation

After Right

Rotation

introduction to project 4
Introduction to Project 4
  • Study use of these Java Collections APIs
    • HashMap<K,V>
    • Map.Entry<K,V>
    • PriorityQueue<T>
  • Study the concept of Huffman coding based on character frequency
    • A tree with the most frequent characters in leaves closer to the root
    • Following left child adds a 0 to the code
    • Following right child adds a 1 to the code
sample huffman coding tree
Sample Huffman Coding Tree

Symbol/Frequency

E / 0.40

T / 0.35

I / 0.20

Others / 0.05

Binary Code Sequence (Read right to left)

0

10

1

110

11

111

An E which occurs 40% of the time takes only 1 binary digit – Zero

A T which occurs 35% of the time takes only 2 binary digits – One and Zero

An I which occurs 20% of the time takes only 2 binary digits – One and One

All other characters which only occur 5% of the time take 3 or more digits

uml diagram for project 4
UML Diagram for Project 4

HuffmanTree

<<Interface>>

Comparable<HuffmanNode>

- encodeTable : HashMap

- decodeTable : HashMap

- compressionRatio : double

+ main(String [ ] args) : void

+ HuffmanTree (message : String)

+ encode (message : String) : String

+ decode (message : String) : String

+ getCompressionRatio (void) : double

HuffmanNode

- value : String

- frequency : int

- zero : HuffmanNode

- one : HuffmanNode

+ HuffmanNode (message : String)

+ compareTo(that : HuffmanNode) : int

+ toString(void) : String

+ getValue(void) : String

+ getFrequency (void) : int

+ increment (void) : void

+ getZero (void) : HuffmanNode

+ getOne (void) : HuffmanNode

java.util.HashMap

java.util.Set

java.util.Map.Entry

java.util.PriorityQueue