92
SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group Binary Search Tree A running demonstration of binary search data structure and algorithms

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group Binary Search Tree A running demonstration of binary search

Embed Size (px)

Citation preview

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

Binary Search Tree

A running demonstration of binary search data structure and algorithms

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

Insert

First value is the 'Root' of the tree.

87

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

Insert

87

Since 50 is Less than 87, we move to the left sub-tree

**Since no sub-tree, 50 is now added as the left leaf**

50

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

Insert

87

50

Since 27 is Less than 87, we move to the left sub-tree

Since 27 is Less than 50, we move to the left sub-tree

**Since no sub-tree, 27 is now added as the left leaf**

27

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

Insert

87

50

27

Since 111 is Greater than 87, we move to the right sub-tree

**Since no sub-tree, 111 is now added as the right leaf**

111

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

Insert

87

50

27

111

Since 99 is Greater than 87, we move to the right sub-tree

Since 99 is Less than 111, we move to the left sub-tree

**Since no sub-tree, 99 is now added as the left leaf**

99

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

Insert

87

50

27

111

99

Since 42 is Less than 87, we move to the left sub-tree

Since 42 is Less than 50, we move to the left sub-tree

Since 42 is Greater than 27, we move to the right sub-tree

**Since no sub-tree, 42 is now added as the right leaf**

42

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

Insert

87

50

27

111

99

42

Since 90 is Greater than 87, we move to the right sub-tree

Since 90 is Less than 111, we move to the left sub-tree

Since 90 is Less than 99, we move to the left sub-tree

**Since no sub-tree, 90 is now added as the left leaf**

90

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

Insert

87

50

27

111

99

42 90

Since 105 is Greater than 87, we move to the right sub-tree

Since 105 is Less than 111, we move to the left sub-tree

Since 105 is Greater than 99, we move to the right sub-tree

**Since no sub-tree, 105 is now added as the right leaf**

105

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

Insert

87

50

27

111

99

42 90 105

Since 58 is Less than 87, we move to the left sub-tree

Since 58 is Greater than 50, we move to the right sub-tree

**Since no sub-tree, 58 is now added as the right leaf**

58

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

Insert

87

50

27

111

99

42 90 105

58

Since 32 is Less than 87, we move to the left sub-tree

Since 32 is Less than 50, we move to the left sub-tree

Since 32 is Greater than 27, we move to the right sub-tree

Since 32 is Less than 42, we move to the left sub-tree

**Since no sub-tree, 32 is now added as the left leaf**

32

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

Insert

87

50

27

111

99

42 90 105

58

32

Since 68 is Less than 87, we move to the left sub-tree

Since 68 is Greater than 50, we move to the right sub-tree

Since 68 is Greater than 58, we move to the right sub-tree

**Since no sub-tree, 68 is now added as the right leaf**

68

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

Insert

87

50

27

111

99

42 90 105

58

32

68

Since 43 is Less than 87, we move to the left sub-tree

Since 43 is Less than 50, we move to the left sub-tree

Since 43 is Greater than 27, we move to the right sub-tree

Since 43 is Greater than 42, we move to the right sub-tree

**Since no sub-tree, 43 is now added as the right leaf**

43

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

Insert

87

50

27

111

99

42 90 105

58

32

68

43

Since 60 is Less than 87, we move to the left sub-tree

Since 60 is Greater than 50, we move to the right sub-tree

Since 60 is Greater than 58, we move to the right sub-tree

Since 60 is Less than 68, we move to the left sub-tree

**Since no sub-tree, 60 is now added as the left leaf**

60

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

Insert

87

50

27

111

99

42 90 105

58

32

68

43 60

Since 70 is Less than 87, we move to the left sub-tree

Since 70 is Greater than 50, we move to the right sub-tree

Since 70 is Greater than 58, we move to the right sub-tree

Since 70 is Greater than 68, we move to the right sub-tree

**Since no sub-tree, 70 is now added as the right leaf**

70

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

Insert

87

50

27

111

99

42 90 105

58

32

68

43 60 70

Since 51 is Less than 87, we move to the left sub-tree

Since 51 is Greater than 50, we move to the right sub-tree

Since 51 is Less than 58, we move to the left sub-tree

**Since no sub-tree, 51 is now added as the left leaf**

51

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

Insert

87

50

27

111

99

42 90 105

58

32

68

43 60 70

51

Since 1 is Less than 87, we move to the left sub-tree

Since 1 is Less than 50, we move to the left sub-tree

Since 1 is Less than 27, we move to the left sub-tree

**Since no sub-tree, 1 is now added as the left leaf**

1

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

Insert

87

50

27

111

99

42 90 105

58

32

68

43 60 70

511

Since 11 is Less than 87, we move to the left sub-tree

Since 11 is Less than 50, we move to the left sub-tree

Since 11 is Less than 27, we move to the left sub-tree

Since 11 is Greater than 1, we move to the right sub-tree

**Since no sub-tree, 11 is now added as the right leaf**

11

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

Predecessor

The node to delete is 50

Predecessor is found by finding the right most node of the 50 nodes left sub-tree

87

50

27

111

99

42 90 105

58

32

68

43 60 70

511

11

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

Sucsessor

Successor is found by finding the left most node of the 50 nodes right sub-tree

87

50

27

111

99

42 90 105

58

32

68

43 60 70

511

11

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

How to delete 50?

There is a left child so we find predecessor

To learn more about finding predecessor insert <Fpred>starting value</Fpred> to data file

87

50

27

111

99

42 90 105

58

32

68

43 60 70

511

11

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

Delete

There is a left child so we find predecessor

This is how the new tree would appear

87

43

27

1

11

42

32

58

51 68

60 70

111

99

90 105

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

How to delete 105?

This is a Leaf case

We just remove this node from the tree

87

43

27

1

11

42

32

58

51 68

60 70

111

99

90 105

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

Delete

This is a Leaf case

This is how the new tree would appear

87

43

27

1

11

42

32

58

51 68

60 70

111

99

90

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

How to delete 87?

There is a left child so we find predecessor

To learn more about finding predicessor insert <Fpred>starting value</Fpred> to data file

87

43

27

1

11

42

32

58

51 68

60 70

111

99

90

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

Delete

There is a left child so we find predecessor

This is how the new tree would appear

70

43

27

1

11

42

32

58

51 68

60

111

99

90

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

How to delete 111?

There is only one child, so replace the node with its only child

70

43

27

1

11

42

32

58

51 68

60

111

99

90

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

After 111 is replaced by its only child

There is only one child, so replace the node with its only child

70

43

27

1

11

42

32

58

51 68

60

99

90

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

PreOrder Traversal

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

70

43

27

1

11

42

32

58

51 68

60

111

99

90

PreOrder Traversal

1.Visit the root

2.Visit the left sub-tree 3.Visit the right sub-tree

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

70

43

27

1

11

42

32

58

51 68

60

111

99

90

PreOrder Traversal

1.Visit the root

2.Visit the left sub-tree 3.Visit the right sub-tree

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

70

43

27

1

11

42

32

58

51 68

60

111

99

90

PreOrder Traversal

1.Visit the root

2.Visit the left sub-tree 3.Visit the right sub-tree

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

70

43

27

1

11

42

32

58

51 68

60

111

99

90

PreOrder Traversal

1.Visit the root

2.Visit the left sub-tree 3.Visit the right sub-tree

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

70

43

27

1

11

42

32

58

51 68

60

111

99

90

PreOrder Traversal

1.Visit the root

2.Visit the left sub-tree 3.Visit the right sub-tree

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

70

43

27

1

11

42

32

58

51 68

60

111

99

90

PreOrder Traversal

1.Visit the root

2.Visit the left sub-tree 3.Visit the right sub-tree

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

70

43

27

1

11

42

32

58

51 68

60

111

99

90

PreOrder Traversal

1.Visit the root

2.Visit the left sub-tree 3.Visit the right sub-tree

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

70

43

27

1

11

42

32

58

51 68

60

111

99

90

PreOrder Traversal

1.Visit the root

2.Visit the left sub-tree 3.Visit the right sub-tree

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

70

43

27

1

11

42

32

58

51 68

60

111

99

90

PreOrder Traversal

1.Visit the root

2.Visit the left sub-tree 3.Visit the right sub-tree

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

70

43

27

1

11

42

32

58

51 68

60

111

99

90

PreOrder Traversal

1.Visit the root

2.Visit the left sub-tree 3.Visit the right sub-tree

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

70

43

27

1

11

42

32

58

51 68

60

111

99

90

PreOrder Traversal

1.Visit the root

2.Visit the left sub-tree 3.Visit the right sub-tree

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

70

43

27

1

11

42

32

58

51 68

60

111

99

90

PreOrder Traversal

1.Visit the root

2.Visit the left sub-tree 3.Visit the right sub-tree

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

70

43

27

1

11

42

32

58

51 68

60

111

99

90

PreOrder Traversal

1.Visit the root

2.Visit the left sub-tree 3.Visit the right sub-tree

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

70

43

27

1

11

42

32

58

51 68

60

111

99

90

PreOrder Traversal

1.Visit the root

2.Visit the left sub-tree 3.Visit the right sub-tree

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

InOrder Traversal

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

70

43

27

1

11

42

32

58

51 68

60

111

99

90

InOrder Traversal

1.Visit the left sub-tree

2.Visit the root 3.Visit the right sub-tree

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

70

43

27

1

11

42

32

58

51 68

60

111

99

90

InOrder Traversal

1.Visit the left sub-tree

2.Visit the root 3.Visit the right sub-tree

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

70

43

27

1

11

42

32

58

51 68

60

111

99

90

InOrder Traversal

1.Visit the left sub-tree

2.Visit the root 3.Visit the right sub-tree

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

70

43

27

1

11

42

32

58

51 68

60

111

99

90

InOrder Traversal

1.Visit the left sub-tree

2.Visit the root 3.Visit the right sub-tree

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

70

43

27

1

11

42

32

58

51 68

60

111

99

90

InOrder Traversal

1.Visit the left sub-tree

2.Visit the root 3.Visit the right sub-tree

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

70

43

27

1

11

42

32

58

51 68

60

111

99

90

InOrder Traversal

1.Visit the left sub-tree

2.Visit the root 3.Visit the right sub-tree

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

70

43

27

1

11

42

32

58

51 68

60

111

99

90

InOrder Traversal

1.Visit the left sub-tree

2.Visit the root 3.Visit the right sub-tree

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

70

43

27

1

11

42

32

58

51 68

60

111

99

90

InOrder Traversal

1.Visit the left sub-tree

2.Visit the root 3.Visit the right sub-tree

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

70

43

27

1

11

42

32

58

51 68

60

111

99

90

InOrder Traversal

1.Visit the left sub-tree

2.Visit the root 3.Visit the right sub-tree

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

70

43

27

1

11

42

32

58

51 68

60

111

99

90

InOrder Traversal

1.Visit the left sub-tree

2.Visit the root 3.Visit the right sub-tree

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

70

43

27

1

11

42

32

58

51 68

60

111

99

90

InOrder Traversal

1.Visit the left sub-tree

2.Visit the root 3.Visit the right sub-tree

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

70

43

27

1

11

42

32

58

51 68

60

111

99

90

InOrder Traversal

1.Visit the left sub-tree

2.Visit the root 3.Visit the right sub-tree

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

70

43

27

1

11

42

32

58

51 68

60

111

99

90

InOrder Traversal

1.Visit the left sub-tree

2.Visit the root 3.Visit the right sub-tree

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

70

43

27

1

11

42

32

58

51 68

60

111

99

90

InOrder Traversal

1.Visit the left sub-tree

2.Visit the root 3.Visit the right sub-tree

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

PostOrder Traversal

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

70

43

27

1

11

42

32

58

51 68

60

111

99

90

PostOrder Traversal

1.Visit the left sub-tree

2.Visit the right sub-tree 3.Visit the root

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

70

43

27

1

11

42

32

58

51 68

60

111

99

90

PostOrder Traversal

1.Visit the left sub-tree

2.Visit the right sub-tree 3.Visit the root

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

70

43

27

1

11

42

32

58

51 68

60

111

99

90

PostOrder Traversal

1.Visit the left sub-tree

2.Visit the right sub-tree 3.Visit the root

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

70

43

27

1

11

42

32

58

51 68

60

111

99

90

PostOrder Traversal

1.Visit the left sub-tree

2.Visit the right sub-tree 3.Visit the root

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

70

43

27

1

11

42

32

58

51 68

60

111

99

90

PostOrder Traversal

1.Visit the left sub-tree

2.Visit the right sub-tree 3.Visit the root

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

70

43

27

1

11

42

32

58

51 68

60

111

99

90

PostOrder Traversal

1.Visit the left sub-tree

2.Visit the right sub-tree 3.Visit the root

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

70

43

27

1

11

42

32

58

51 68

60

111

99

90

PostOrder Traversal

1.Visit the left sub-tree

2.Visit the right sub-tree 3.Visit the root

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

70

43

27

1

11

42

32

58

51 68

60

111

99

90

PostOrder Traversal

1.Visit the left sub-tree

2.Visit the right sub-tree 3.Visit the root

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

70

43

27

1

11

42

32

58

51 68

60

111

99

90

PostOrder Traversal

1.Visit the left sub-tree

2.Visit the right sub-tree 3.Visit the root

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

70

43

27

1

11

42

32

58

51 68

60

111

99

90

PostOrder Traversal

1.Visit the left sub-tree

2.Visit the right sub-tree 3.Visit the root

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

70

43

27

1

11

42

32

58

51 68

60

111

99

90

PostOrder Traversal

1.Visit the left sub-tree

2.Visit the right sub-tree 3.Visit the root

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

70

43

27

1

11

42

32

58

51 68

60

111

99

90

PostOrder Traversal

1.Visit the left sub-tree

2.Visit the right sub-tree 3.Visit the root

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

70

43

27

1

11

42

32

58

51 68

60

111

99

90

PostOrder Traversal

1.Visit the left sub-tree

2.Visit the right sub-tree 3.Visit the root

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

70

43

27

1

11

42

32

58

51 68

60

111

99

90

PostOrder Traversal

1.Visit the left sub-tree

2.Visit the right sub-tree 3.Visit the root

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

Breadth First Traversal

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

70

43

27

1

11

42

32

58

51 68

60

111

99

90

Breadth First Traversal

1.Visit the root

2.Visit both Children 3.Visit Their Children

Visit

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

70

43

27

1

11

42

32

58

51 68

60

111

99

90

Breadth First Traversal

1.Visit the root

2.Visit both Children 3.Visit Their Children

Visit

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

70

43

27

1

11

42

32

58

51 68

60

111

99

90

Breadth First Traversal

1.Visit the root

2.Visit both Children 3.Visit Their Children

Visit

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

70

43

27

1

11

42

32

58

51 68

60

111

99

90

Breadth First Traversal

1.Visit the root

2.Visit both Children 3.Visit Their Children

Visit

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

70

43

27

1

11

42

32

58

51 68

60

111

99

90

Breadth First Traversal

1.Visit the root

2.Visit both Children 3.Visit Their Children

Visit

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

70

43

27

1

11

42

32

58

51 68

60

111

99

90

Breadth First Traversal

1.Visit the root

2.Visit both Children 3.Visit Their Children

Visit

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

70

43

27

1

11

42

32

58

51 68

60

111

99

90

Breadth First Traversal

1.Visit the root

2.Visit both Children 3.Visit Their Children

Visit

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

70

43

27

1

11

42

32

58

51 68

60

111

99

90

Breadth First Traversal

1.Visit the root

2.Visit both Children 3.Visit Their Children

Visit

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

70

43

27

1

11

42

32

58

51 68

60

111

99

90

Breadth First Traversal

1.Visit the root

2.Visit both Children 3.Visit Their Children

Visit

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

70

43

27

1

11

42

32

58

51 68

60

111

99

90

Breadth First Traversal

1.Visit the root

2.Visit both Children 3.Visit Their Children

Visit

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

70

43

27

1

11

42

32

58

51 68

60

111

99

90

Breadth First Traversal

1.Visit the root

2.Visit both Children 3.Visit Their Children

Visit

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

70

43

27

1

11

42

32

58

51 68

60

111

99

90

Breadth First Traversal

1.Visit the root

2.Visit both Children 3.Visit Their Children

Visit

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

70

43

27

1

11

42

32

58

51 68

60

111

99

90

Breadth First Traversal

1.Visit the root

2.Visit both Children 3.Visit Their Children

Visit

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

70

43

27

1

11

42

32

58

51 68

60

111

99

90

Breadth First Traversal

1.Visit the root

2.Visit both Children 3.Visit Their Children

Visit

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

Size

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

Size

Size is the amount of nodes in a tree

1

2

3

4

5

6

7

8

9 10

11

12

13

14

Size = 14

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

Height

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group

Height

Hight is the number of Levels between

the root and lowest leaf

70

43

27

1

11

42

32

58

51 68

60

111

99

90

Height = 5