cs302 – data structures - autonomous robots lab...traversing 2-3 trees •to traverse a 2-3 tree...

Post on 17-Feb-2020

4 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

CS302 - Data Structuresusing C++

Kostas Alexis

Topic: Balanced Search Trees - Introduction

2-3 Trees

• General Search Tree

• Interior nodes are either 2-nodes or 3-nodes

• 2-node has one data item and two children

• 3-node has two data items and three children• Simple implementations may use 3-nodes for both

2-3 Trees

• General Search Tree

• Interior nodes are either 2-nodes or 3-nodes

• 2-node has one data item and two children

• 3-node has two data items and three children• Simple implementations may use 3-nodes for both

50

leftChild rightChild

data

60|90

leftChild rightChildmiddleChild

small|large data

2-3 Trees

• General Search Tree

• Interior nodes are either 2-nodes or 3-nodes

• 2-node has one data item and two children

• 3-node has two data items and three children• Simple implementations may use 3-nodes for both

50

leftChild rightChild

data

60|90

leftChild rightChildmiddleChild

<50 >50

<60 >90>60,<90

small|large data

2-3 Trees

• General Search Tree

• Interior nodes are either 2-nodes or 3-nodes

• 2-node has one data item and two children

• 3-node has two data items and three children• Simple implementations may use 3-nodes for both

• Are never taller than minimum-height binary tree

• A 2-3 tree with n nodes never has height greater than log2(𝑛 + 1)

50

leftChild rightChild

data

60|90

leftChild rightChild

small|large data

middleChild

<50 >50

<60 >90>60,<90

2-3 Trees

60

30 90

10 50

20 40

80 95

70

A Balanced Binary Search Tree

50

30 70 | 90

40 60 9580

2-3 Tree with the same elements

10 | 20

2-3 Trees

60

30 90

10 50

20 40

80 95

70

A Balanced Binary Search Tree – Height = 4

50

30 70 | 90

40 60 9580

2-3 Tree with the same elements – Height = 3

10 | 20

Traversing 2-3 Trees

• To traverse a 2-3 Tree• Perform the analogue of an in-order traversal

• Leftmost subtree,

• Left value,

• Center subtree,

• Right value,

• Rightmost subtree

• Searching a 2-3 tree is as efficient assearching the shorted binary tree (withthe same values)

• 𝑂(log2𝑛)

50

30 70 | 90

40 60 958010 | 20

Traversing 2-3 Trees

• To traverse a 2-3 Tree• Perform the analogue of an in-order traversal

• Leftmost subtree,

• Left value,

• Center subtree,

• Right value,

• Rightmost subtree

• Searching a 2-3 tree is as efficient assearching the shorted binary tree (withthe same values)

• 𝑂(log2𝑛)

50

30 70 | 90

40 60 958010 | 20

10 20

Traversing 2-3 Trees

• To traverse a 2-3 Tree• Perform the analogue of an in-order traversal

• Leftmost subtree,

• Left value,

• Center subtree,

• Right value,

• Rightmost subtree

• Searching a 2-3 tree is as efficient assearching the shorted binary tree (withthe same values)

• 𝑂(log2𝑛)

50

30 70 | 90

40 60 958010 | 20

10 20 30

Traversing 2-3 Trees

• To traverse a 2-3 Tree• Perform the analogue of an in-order traversal

• Leftmost subtree,

• Left value,

• Center subtree,

• Right value,

• Rightmost subtree

• Searching a 2-3 tree is as efficient assearching the shorted binary tree (withthe same values)

• 𝑂(log2𝑛)

50

30 70 | 90

40 60 958010 | 20

10 20 30 40

Traversing 2-3 Trees

• To traverse a 2-3 Tree• Perform the analogue of an in-order traversal

• Leftmost subtree,

• Left value,

• Center subtree,

• Right value,

• Rightmost subtree

• Searching a 2-3 tree is as efficient assearching the shorted binary tree (withthe same values)

• 𝑂(log2𝑛)

50

30 70 | 90

40 60 958010 | 20

10 20 30 40 50

Traversing 2-3 Trees

• To traverse a 2-3 Tree• Perform the analogue of an in-order traversal

• Leftmost subtree,

• Left value,

• Center subtree,

• Right value,

• Rightmost subtree

• Searching a 2-3 tree is as efficient assearching the shorted binary tree (withthe same values)

• 𝑂(log2𝑛)

50

30 70 | 90

40 60 958010 | 20

10 20 30 40 50 60

Traversing 2-3 Trees

• To traverse a 2-3 Tree• Perform the analogue of an in-order traversal

• Leftmost subtree,

• Left value,

• Center subtree,

• Right value,

• Rightmost subtree

• Searching a 2-3 tree is as efficient assearching the shorted binary tree (withthe same values)

• 𝑂(log2𝑛)

50

30 70 | 90

40 60 958010 | 20

10 20 30 40 50 60 70

Traversing 2-3 Trees

• To traverse a 2-3 Tree• Perform the analogue of an in-order traversal

• Leftmost subtree,

• Left value,

• Center subtree,

• Right value,

• Rightmost subtree

• Searching a 2-3 tree is as efficient assearching the shorted binary tree (withthe same values)

• 𝑂(log2𝑛)

50

30 70 | 90

40 60 958010 | 20

10 20 30 40 50 60 70

Traversing 2-3 Trees

• To traverse a 2-3 Tree• Perform the analogue of an in-order traversal

• Leftmost subtree,

• Left value,

• Center subtree,

• Right value,

• Rightmost subtree

• Searching a 2-3 tree is as efficient assearching the shorted binary tree (withthe same values)

• 𝑂(log2𝑛)

50

30 70 | 90

40 60 958010 | 20

10 20 30 40 50 60 70 80

Traversing 2-3 Trees

• To traverse a 2-3 Tree• Perform the analogue of an in-order traversal

• Leftmost subtree,

• Left value,

• Center subtree,

• Right value,

• Rightmost subtree

• Searching a 2-3 tree is as efficient assearching the shorted binary tree (withthe same values)

• 𝑂(log2𝑛)

50

30 70 | 90

40 60 958010 | 20

10 20 30 40 50 60 70 80

Traversing 2-3 Trees

• To traverse a 2-3 Tree• Perform the analogue of an in-order traversal

• Leftmost subtree,

• Left value,

• Center subtree,

• Right value,

• Rightmost subtree

• Searching a 2-3 tree is as efficient assearching the shorted binary tree (withthe same values)

• 𝑂(log2𝑛)

50

30 70 | 90

40 60 958010 | 20

10 20 30 40 50 60 70 80 90

Traversing 2-3 Trees

• To traverse a 2-3 Tree• Perform the analogue of an in-order traversal

• Leftmost subtree,

• Left value,

• Center subtree,

• Right value,

• Rightmost subtree

• Searching a 2-3 tree is as efficient assearching the shorted binary tree (withthe same values)

• 𝑂(log2𝑛)

50

30 70 | 90

40 60 958010 | 20

10 20 30 40 50 60 70 80 90 95

Adding to 2-3 Trees

• Inserting values into a 2-3 Tree• Always insert values into an EXISTING leaf

Adding to 2-3 Trees

• Inserting values into a 2-3 Tree• Always insert values into an EXISTING leaf

• Only exception – root (first value)• Create a 2-node and insert value

Adding to 2-3 Trees

• Inserting values into a 2-3 Tree• Always insert values into an EXISTING leaf

• Only exception – root (first value)• Create a 2-node and insert value

• Inserting a value into a 2-node turns it into a 3-node

Adding to 2-3 Trees

• Inserting values into a 2-3 Tree• Always insert values into an EXISTING leaf

• Only exception – root (first value)• Create a 2-node and insert value

• Inserting a value into a 2-node turns it into a 3-node

• Inserting a value into a 3-node causes it todivide

• Result is a subtree of 2-nodes

Adding to 2-3 Trees

• Inserting values into a 2-3 Tree• Always insert values into an EXISTING leaf

• Only exception – root (first value)• Create a 2-node and insert value

• Inserting a value into a 2-node turns it into a 3-node

• Inserting a value into a 3-node causes it todivide

• Result is a subtree of 2-nodes

50 70 90

50

Adding to 2-3 Trees

• Inserting values into a 2-3 Tree• Always insert values into an EXISTING leaf

• Only exception – root (first value)• Create a 2-node and insert value

• Inserting a value into a 2-node turns it into a 3-node

• Inserting a value into a 3-node causes it todivide

• Result is a subtree of 2-nodes

50 70 90

50 | 70

Adding to 2-3 Trees

• Inserting values into a 2-3 Tree• Always insert values into an EXISTING leaf

• Only exception – root (first value)• Create a 2-node and insert value

• Inserting a value into a 2-node turns it into a 3-node

• Inserting a value into a 3-node causes it todivide

• Result is a subtree of 2-nodes

50 70 90

50 | 70 | 90

Adding to 2-3 Trees

• Inserting values into a 2-3 Tree• Always insert values into an EXISTING leaf

• Only exception – root (first value)• Create a 2-node and insert value

• Inserting a value into a 2-node turns it into a 3-node

• Inserting a value into a 3-node causes it todivide

• Result is a subtree of 2-nodes

50 70 90

70

50 90

Adding to 2-3 Trees

• Inserting values into a 2-3 Tree• Always insert values into an EXISTING leaf

• Only exception – root (first value)• Create a 2-node and insert value

• Inserting a value into a 2-node turns it into a 3-node

• Inserting a value into a 3-node causes it todivide

• Result is a subtree of 2-nodes

50 70 90

70

50 90

• To insert an item with key k into a 2-3 tree• Locate the leaf at which the search for k would terminate• Insert the new item k into the leaf

• If the leaf now contains only two items – done

• If the leaf now contains three items, split the leaf into two nodes and move the

middle value into parent

Adding to 2-3 Trees

• When an internal node contains three items• Split the node into two nodes

• Accommodate the node’s children

• When the root contains three items• Split the root into two nodes

• Create a new root node

• Tree grows in height

Adding to 2-3 Trees

• When an internal node contains three items• Split the node into two nodes

• Accommodate the node’s children

• When the root contains three items• Split the root into two nodes

• Create a new root node

• Tree grows in height

Adding to 2-3 Trees

• When an internal node contains three items• Split the node into two nodes

• Accommodate the node’s children

• When the root contains three items• Split the root into two nodes

• Create a new root node

• Tree grows in height

20

50

30

80

25

60

50

Order of Values Given

20 90 30 80 25 60 50

Adding to 2-3 Trees

• When an internal node contains three items• Split the node into two nodes

• Accommodate the node’s children

• When the root contains three items• Split the root into two nodes

• Create a new root node

• Tree grows in height

20

50

30

80

25

60

50

Order of Values Given

20 90 30 80 25 60 50

20

Adding to 2-3 Trees

• When an internal node contains three items• Split the node into two nodes

• Accommodate the node’s children

• When the root contains three items• Split the root into two nodes

• Create a new root node

• Tree grows in height

20

50

30

80

25

60

50

Order of Values Given

20 90 30 80 25 60 50

20 | 90

Adding to 2-3 Trees

• When an internal node contains three items• Split the node into two nodes

• Accommodate the node’s children

• When the root contains three items• Split the root into two nodes

• Create a new root node

• Tree grows in height

20

50

30

80

25

60

50

Order of Values Given

20 90 30 80 25 60 50

30

20 90

Adding to 2-3 Trees

• When an internal node contains three items• Split the node into two nodes

• Accommodate the node’s children

• When the root contains three items• Split the root into two nodes

• Create a new root node

• Tree grows in height

20

50

30

80

25

60

50

Order of Values Given

20 90 30 80 25 60 50

30 | 80

20 | 25 50 | 60 90

Removing From a 2-3 Tree

• Removing values from 2-3 trees• Always remove from a leaf

• Values (and children) will be redistributed

• Nodes can be merged

• Only root node is deleted• And only if it is empty (contains no values)

Removing From a 2-3 Tree

• Removing values from 2-3 trees• Always remove from a leaf

• Values (and children) will be redistributed

• Nodes can be merged

• Only root node is deleted• And only if it is empty (contains no values)

50

30 70 | 90

10 | 20 40 60 80 95

Removing From a 2-3 Tree

• Removing values from 2-3 trees• Always remove from a leaf

• Values (and children) will be redistributed

• Nodes can be merged

• Only root node is deleted• And only if it is empty (contains no values)

50

30 70 | 90

10 | 20 40 60 80 95

Removing From a 2-3 Tree

• Removing values from 2-3 trees• Always remove from a leaf

• Values (and children) will be redistributed

• Nodes can be merged

• Only root node is deleted• And only if it is empty (contains no values)

50

30 70 | 90

10 40 60 80 95

Removing From a 2-3 Tree

• Removing values from 2-3 trees• Always remove from a leaf

• Values (and children) will be redistributed

• Nodes can be merged

• Only root node is deleted• And only if it is empty (contains no values)

50

30 70 | 90

10 40 60 80 95

Removing From a 2-3 Tree

• Removing values from 2-3 trees• Always remove from a leaf

• Values (and children) will be redistributed

• Nodes can be merged

• Only root node is deleted• And only if it is empty (contains no values)

50

30 70 | 90

10 40 80 95

Removing From a 2-3 Tree

• Removing values from 2-3 trees• Always remove from a leaf

• Values (and children) will be redistributed

• Nodes can be merged

• Only root node is deleted• And only if it is empty (contains no values)

50

30 90

10 40 70 | 80 95

Removing From a 2-3 Tree

• Removing values from 2-3 trees• Always remove from a leaf

• Values (and children) will be redistributed

• Nodes can be merged

• Only root node is deleted• And only if it is empty (contains no values)

50

30 90

10 40 70 | 80 95

Removing From a 2-3 Tree

• Removing values from 2-3 trees• Always remove from a leaf

• Values (and children) will be redistributed

• Nodes can be merged

• Only root node is deleted• And only if it is empty (contains no values)

50

40 90

10 30 70 | 80 95

Removing From a 2-3 Tree

• Removing values from 2-3 trees• Always remove from a leaf

• Values (and children) will be redistributed

• Nodes can be merged

• Only root node is deleted• And only if it is empty (contains no values)

50

90

10 | 40 70 | 80 95

Removing From a 2-3 Tree

• Removing values from 2-3 trees• Always remove from a leaf

• Values (and children) will be redistributed

• Nodes can be merged

• Only root node is deleted• And only if it is empty (contains no values)

50 90

10 | 40 70 | 80 95

Removing From a 2-3 Tree

• Removing values from 2-3 trees• Always remove from a leaf

• Values (and children) will be redistributed

• Nodes can be merged

• Only root node is deleted• And only if it is empty (contains no values)

50 | 90

10 | 40 70 | 80 95

Removing From a 2-3 Tree

• Removing values from 2-3 trees• Always remove from a leaf

• Values (and children) will be redistributed

• Nodes can be merged

• Only root node is deleted• And only if it is empty (contains no values)

50 | 90

10 | 40 70 | 80 95

Thank you

top related