Binary Search Trees


A Binary Search Tree of States



Download 469,5 Kb.
bet2/2
Sana23.06.2022
Hajmi469,5 Kb.
#696694
1   2
Bog'liq
rfdhdh

A Binary Search Tree of States

  • Arizona
  • Arkansas
  • Colorado
  • The data in the dictionary will be stored in a binary tree, with each node containing an item and a key.
  • Washington
  • Oklahoma
  • Florida
  • Mass.
  • New
  • Hampshire
  • West
  • Virginia

A Binary Search Tree of States

  • Colorado
  • Arizona
  • Arkansas
  • Washington
  • Oklahoma
  • Colorado
  • Florida
  • Mass.
  • New
  • Hampshire
  • West
  • Virginia
  • Storage rules:
  • Every key to the left of a node is alphabetically before the key of the node.

A Binary Search Tree of States

  • Arizona
  • Colorado
  • Arkansas
  • Storage rules:
  • Every key to the left of a node is alphabetically before the key of the node.
  • Washington
  • Oklahoma
  • Florida
  • Mass.
  • New
  • Hampshire
  • West
  • Virginia
  • Example:
  • “Massachusetts” and
  • “ New Hampshire”
  • are alphabetically
  • before “Oklahoma”

A Binary Search Tree of States

  • Arizona
  • Arkansas
  • Storage rules:
  • Every key to the left of a node is alphabetically before the key of the node.
  • Every key to the right of a node is alphabetically after the key of the node.
  • Washington
  • Oklahoma
  • Colorado
  • Florida
  • West
  • Virginia
  • Mass.
  • New
  • Hampshire

A Binary Search Tree of States

  • Arizona
  • Arkansas
  • Storage rules:
  • Every key to the left of a node is alphabetically before the key of the node.
  • Every key to the right of a node is alphabetically after the key of the node.
  • Washington
  • Oklahoma
  • Colorado
  • Florida
  • West
  • Virginia
  • Mass.
  • New
  • Hampshire

Retrieving Data

  • Arizona
  • Arkansas
  • Start at the root.
  • If the current node has the key, then stop and retrieve the data.
  • If the current node's key is too large, move left and repeat 1-3.
  • If the current node's key is too small, move right and repeat 1-3.
  • Washington
  • Oklahoma
  • Colorado
  • Florida
  • West
  • Virginia
  • Mass.
  • New
  • Hampshire

Retrieve " New Hampshire"

  • Arizona
  • Arkansas
  • Washington
  • Oklahoma
  • Colorado
  • Florida
  • West
  • Virginia
  • Mass.
  • New
  • Hampshire
  • Start at the root.
  • If the current node has the key, then stop and retrieve the data.
  • If the current node's key is too large, move left and repeat 1-3.
  • If the current node's key is too small, move right and repeat 1-3.

Retrieve "New Hampshire"

  • Arizona
  • Arkansas
  • Washington
  • Oklahoma
  • Colorado
  • Florida
  • West
  • Virginia
  • Mass.
  • New
  • Hampshire
  • Start at the root.
  • If the current node has the key, then stop and retrieve the data.
  • If the current node's key is too large, move left and repeat 1-3.
  • If the current node's key is too small, move right and repeat 1-3.

Retrieve "New Hampshire"

  • Arizona
  • Arkansas
  • Washington
  • Oklahoma
  • Colorado
  • Florida
  • West
  • Virginia
  • Mass.
  • New
  • Hampshire
  • Start at the root.
  • If the current node has the key, then stop and retrieve the data.
  • If the current node's key is too large, move left and repeat 1-3.
  • If the current node's key is too small, move right and repeat 1-3.

Retrieve "New Hampshire"

  • Arizona
  • Arkansas
  • Washington
  • Oklahoma
  • Colorado
  • Florida
  • West
  • Virginia
  • Mass.
  • New
  • Hampshire
  • Start at the root.
  • If the current node has the key, then stop and retrieve the data.
  • If the current node's key is too large, move left and repeat 1-3.
  • If the current node's key is too small, move right and repeat 1-3.

Adding a New Item with a Given Key

  • Arizona
  • Arkansas
  • Pretend that you are trying to find the key, but stop when there is no node to move to.
  • Add the new node at the spot where you would have moved to if there had been a node.
  • Washington
  • Oklahoma
  • Colorado
  • Florida
  • West
  • Virginia
  • Mass.
  • New
  • Hampshire

Adding

  • Arizona
  • Arkansas
  • Pretend that you are trying to find the key, but stop when there is no node to move to.
  • Add the new node at the spot where you would have moved to if there had been a node.
  • Washington
  • Oklahoma
  • Colorado
  • Florida
  • West
  • Virginia
  • Mass.
  • New
  • Hampshire
  • Iowa

Adding

  • Arizona
  • Arkansas
  • Pretend that you are trying to find the key, but stop when there is no node to move to.
  • Add the new node at the spot where you would have moved to if there had been a node.
  • Washington
  • Oklahoma
  • Colorado
  • Florida
  • West
  • Virginia
  • Mass.
  • New
  • Hampshire
  • Iowa

Adding

  • Arizona
  • Arkansas
  • Pretend that you are trying to find the key, but stop when there is no node to move to.
  • Add the new node at the spot where you would have moved to if there had been a node.
  • Washington
  • Oklahoma
  • Colorado
  • Florida
  • West
  • Virginia
  • Mass.
  • New
  • Hampshire
  • Iowa

Adding

  • Arizona
  • Arkansas
  • Pretend that you are trying to find the key, but stop when there is no node to move to.
  • Add the new node at the spot where you would have moved to if there had been a node.
  • Washington
  • Oklahoma
  • Colorado
  • Florida
  • West
  • Virginia
  • Mass.
  • New
  • Hampshire
  • Iowa

Adding

  • Arizona
  • Arkansas
  • Pretend that you are trying to find the key, but stop when there is no node to move to.
  • Add the new node at the spot where you would have moved to if there had been a node.
  • Washington
  • Oklahoma
  • Colorado
  • Florida
  • West
  • Virginia
  • Mass.
  • New
  • Hampshire
  • Iowa

Adding

  • Arizona
  • Arkansas
  • Pretend that you are trying to find the key, but stop when there is no node to move to.
  • Add the new node at the spot where you would have moved to if there had been a node.
  • Washington
  • Oklahoma
  • Colorado
  • Florida
  • West
  • Virginia
  • Mass.
  • New
  • Hampshire
  • Iowa

Adding

  • Arizona
  • Arkansas
  • Washington
  • Oklahoma
  • Colorado
  • Florida
  • West
  • Virginia
  • Mass.
  • New
  • Hampshire
  • Iowa
  • Where would you
  • add this state?
  • Kazakhstan

Adding

  • Arizona
  • Arkansas
  • Washington
  • Oklahoma
  • Colorado
  • Florida
  • West
  • Virginia
  • Mass.
  • New
  • Hampshire
  • Iowa
  • Kazakhstan is the
  • new right child
  • of Iowa?
  • Kazakhstan

Removing an Item with a Given Key

  • Arizona
  • Arkansas
  • Find the item.
  • If necessary, swap the item with one that is easier to remove.
  • Remove the item.
  • Washington
  • Oklahoma
  • Colorado
  • Florida
  • West
  • Virginia
  • Mass.
  • New
  • Hampshire
  • Iowa
  • Kazakhstan

Removing "Florida"

  • Arizona
  • Arkansas
  • Find the item.
  • Washington
  • Oklahoma
  • Colorado
  • Florida
  • West
  • Virginia
  • Mass.
  • New
  • Hampshire
  • Iowa
  • Kazakhstan

Removing "Florida"

  • Arizona
  • Arkansas
  • Washington
  • Oklahoma
  • Colorado
  • Florida
  • West
  • Virginia
  • Mass.
  • New
  • Hampshire
  • Iowa
  • Kazakhstan
  • Florida cannot be
  • removed at the
  • moment...

Removing "Florida"

  • Arizona
  • Arkansas
  • Washington
  • Oklahoma
  • Colorado
  • West
  • Virginia
  • Mass.
  • New
  • Hampshire
  • Iowa
  • Kazakhstan
  • ... because removing
  • Florida would
  • break the tree into
  • two pieces.

Removing "Florida"

  • Arizona
  • Arkansas
  • Washington
  • Oklahoma
  • Colorado
  • Florida
  • West
  • Virginia
  • Mass.
  • New
  • Hampshire
  • Iowa
  • Kazakhstan
  • The problem of
  • breaking the tree
  • happens because
  • Florida has 2 children.
  • If necessary, do some rearranging.

Removing "Florida"

  • Arizona
  • Arkansas
  • If necessary, do some rearranging.
  • Washington
  • Oklahoma
  • Colorado
  • Florida
  • West
  • Virginia
  • Mass.
  • New
  • Hampshire
  • Iowa
  • Kazakhstan
  • For the rearranging,
  • take the smallest item
  • in the right subtree...

Removing "Florida"

  • Arizona
  • Arkansas
  • Washington
  • Oklahoma
  • Colorado
  • West
  • Virginia
  • Mass.
  • New
  • Hampshire
  • Iowa
  • Kazakhstan
  • Iowa
  • ...copy that smallest
  • item onto the item
  • that we're removing...
  • If necessary, do some rearranging.

Removing "Florida"

  • Arizona
  • Arkansas
  • Washington
  • Oklahoma
  • Colorado
  • West
  • Virginia
  • Mass.
  • New
  • Hampshire
  • Iowa
  • Kazakhstan
  • ... and then remove
  • the extra copy of the
  • item we copied...
  • If necessary, do some rearranging.

Removing "Florida"

  • Arizona
  • Arkansas
  • Washington
  • Oklahoma
  • Colorado
  • West
  • Virginia
  • Mass.
  • New
  • Hampshire
  • Iowa
  • Kazakhstan
  • ... and reconnect
  • the tree
  • If necessary, do some rearranging.

Removing "Florida"

  • Arizona
  • Arkansas
  • Washington
  • Oklahoma
  • Colorado
  • Florida
  • West
  • Virginia
  • Mass.
  • New
  • Hampshire
  • Kazakhstan
  • Why did I choose
  • the smallest item
  • in the right subtree?

Removing "Florida"

  • Arizona
  • Arkansas
  • Washington
  • Oklahoma
  • Colorado
  • West
  • Virginia
  • Mass.
  • New
  • Hampshire
  • Iowa
  • Kazakhstan
  • Because every key
  • must be smaller than
  • the keys in its
  • right subtree

Removing an Item with a Given Key

  • Find the item.
  • If the item has a right child, rearrange the tree:
    • Find smallest item in the right subtree
    • Copy that smallest item onto the one that you want to remove
    • Remove the extra copy of the smallest item (making sure that you keep the tree connected)
  • else just remove the item.

Summary

  • Binary search trees are a good implementation of data types such as sets, bags, and dictionaries.
  • Searching for an item is generally quick since you move from the root to the item, without looking at many other items.
  • Adding and deleting items is also quick.
  • But as you'll see later, it is possible for the quickness to fail in some cases -- can you see why?

THE END

  • Presentation copyright 2010 Addison Wesley Longman,
  • For use with Data Structures and Other Objects Using C++
  • by Michael Main and Walter Savitch.
  • Some artwork in the presentation is used with permission from Presentation Task Force
  • (copyright New Vision Technologies Inc) and Corel Gallery Clipart Catalog (copyright
  • Corel Corporation, 3G Graphics Inc, Archive Arts, Cartesia Software, Image Club
  • Graphics Inc, One Mile Up Inc, TechPool Studios, Totem Graphics Inc).
  • Students and instructors who use Data Structures and Other Objects Using C++ are welcome
  • to use this presentation however they see fit, so long as this copyright notice remains
  • intact.

Download 469,5 Kb.

Do'stlaringiz bilan baham:
1   2




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish