TREE BST HEAP GRAPH

50
TORU-LOTA TREE,BST,HEAP,GRAPH

Transcript of TREE BST HEAP GRAPH

Page 1: TREE BST HEAP GRAPH

TORU-LOTA TREE,BST,HEAP,GRAPH

Page 2: TREE BST HEAP GRAPH

TREE

Page 3: TREE BST HEAP GRAPH

TREE IN COMPUTER SCINCE

Root

Node

Leave

Branch

Page 4: TREE BST HEAP GRAPH

Root: node without parent (A)Siblings: nodes share the same parentInternal node: node with at least one child.External node (leaf ): node without childrenAncestors of a node: parent, grandparent, grand-grandparent, etc.Descendant of a node: child, grandchild, grand-grandchild, etc.Depth of a node: number of ancestorsHeight of a tree: maximum depth of any nodeDegree of a node: the number of its childrenDegree of a tree: the maximum number of its node.

Page 5: TREE BST HEAP GRAPH

Tree Operation

Page 6: TREE BST HEAP GRAPH

BINARY SEARCH TREE

Page 7: TREE BST HEAP GRAPH

Short form of BST is Binary Search TREE

A binary search tree is a binary tree where each node has a Comparable key the key in any node is larger than the keys in all nodes in that node's left sub tree and smaller than the keys in all nodes in that node's right sub tree.

8

6 28

1 5 3525

A COMPLETE BINARY TREE

What is a binary search tree?

Page 8: TREE BST HEAP GRAPH

INSERT

Insert 11

1.Start at root.

6

2 10

0 5 19

4

START

Page 9: TREE BST HEAP GRAPH

INSERT

Insert 11

1.Start at root.2.Move to 10 on right, because 11>6

6

2 10

0 5 19

4

Page 10: TREE BST HEAP GRAPH

INSERT

Insert 11

1.Start at root.2.Move to 10 on right, because 11>63.Move to 19 on right, because 11>10.

6

2 10

0 5 19

4

Page 11: TREE BST HEAP GRAPH

INSERT

Insert 11

1.Start at root.2.Move to 10 on right, because 11>63.Move to 19 on right, because 11>10.4.Insert 11 to the left of 19, because 11<19 and right of 19 is NULL.

6

2 10

0 5 19

4

Page 12: TREE BST HEAP GRAPH

INSERT

Insert 11

1.Start at root.2.Move to 10 on right, because 11>63.Move to 19 on right, because 11>10.4.Insert 11 to the left of 19, because 11<19 and right of 19 is NULL.

6

2 10

0 5 19

4 11

Page 13: TREE BST HEAP GRAPH

INSERT

Insert 11

1.Start at root.2.Move to 10 on right, because 11>63.Move to 19 on right, because 11>10.4.Insert 11 to the left of 19, because 11<19 and right of 19 is NULL.

6

2 10

0 5 19

4 11

11 INSERTED IN THE TREE

Page 14: TREE BST HEAP GRAPH

DELETE

Delete 5

1.Start at root and search 5.

6

2 10

0 5 19

4 11

start

Page 15: TREE BST HEAP GRAPH

DELETE

Delete 5

1.Start at root and search 5.2.Move to 2 on left, because 5<6.

6

2 10

0 5 19

4 11

Page 16: TREE BST HEAP GRAPH

DELETE

Delete 5

1.Start at root and search 5.2.Move to 2 on left, because 5<6.3.Move to 5 on right.

6

2 10

0 5 19

4 11

Page 17: TREE BST HEAP GRAPH

DELETE

Delete 5

1.Start at root and search 5.2.Move to 2 on left, because 5<6.3.Move to 5 on right.4.Delete 5 from the right of 2.

6

2 10

0 19

4 11

Page 18: TREE BST HEAP GRAPH

DELETE

Delete 5

1.Start at root and search 5.2.Move to 2 on left, because 5<6.3.Move to 5 on right.4.Delete 5 from the right of 2.5.Now take 4 and set at the right of 2.

6

2 10

0 4 19

11

Page 19: TREE BST HEAP GRAPH

DELETE

Delete 5

1.Start at root and search 5.2.Move to 2 on left, because 5<6.3.Move to 5 on right.4.Delete 5 from the right of 2.5.Now take 4 and set at the right of 2.

6

2 10

0 4 19

11

5 DELETED FROM THE TREE

Page 20: TREE BST HEAP GRAPH

TRAVERSINGPreorder:

1.Visit the Root2.Traverse the Left subtree

3.Traverse The Right subtree

7>1>0>3>2>5>4>6>9>8>10

In order:1.Traverse the Left subtree.

2.Visit the Root.3.Traverse the Right subtree.

Post Order :1.Traverse the Left subtree

2. Traverse the Right subtree.3.Visit the Root.

7

1 9

0 38 10

52

64

7,1,0,3,2,5,4,6,9,8,10

Page 21: TREE BST HEAP GRAPH

HeapsA heap is a certain kind of complete binary tree.

Each node in a heapcontains a key thatcan be compared toother nodes' keys.

19

4222127

23

45

35

Page 22: TREE BST HEAP GRAPH

Types of Heaps:• There are two types of heaps

1. Max Heap2. Min Heap

Page 23: TREE BST HEAP GRAPH

23

Max Heap

The “MAX heap property"

requires that eachnode's key is >= thekeys of its children

Page 24: TREE BST HEAP GRAPH

24

Min Heap

The “Min heap property"

requires that eachnode's key is <= thekeys of its children

Page 25: TREE BST HEAP GRAPH

Adding a Node to a Heap

Put the new node in the next available spot.

Push the new node upward, swapping with its parent until the new node reaches an acceptable location.

19

4222127

23

45

35

42

Page 26: TREE BST HEAP GRAPH

Adding a Node to a Heap

Put the new node in the next available spot.

Push the new node upward, swapping with its parent until the new node reaches an acceptable location.

19

4222142

23

45

35

27

Page 27: TREE BST HEAP GRAPH

Adding a Node to a Heap

Put the new node in the next available spot.

Push the new node upward, swapping with its parent until the new node reaches an acceptable location.

19

4222135

23

45

42

27

Page 28: TREE BST HEAP GRAPH

Adding a Node to a Heap

The parent has a key that is >= new node, or

The node reaches the root.

The process of pushing the new node upward is called reheapification upward. 19

4222135

23

45

42

27

Page 29: TREE BST HEAP GRAPH

Removing the Top of a Heap

Move the last node onto the root.

19

4222135

23

45

42

27

Page 30: TREE BST HEAP GRAPH

Removing the Top of a Heap

Move the last node onto the root.

19

4222135

23

27

42

Page 31: TREE BST HEAP GRAPH

Removing the Top of a Heap

Move the last node onto the root.

Push the out-of-place node downward, swapping with its larger child until the new node reaches an acceptable location.

19

4222135

23

27

42

Page 32: TREE BST HEAP GRAPH

Removing the Top of a Heap

Move the last node onto the root.

Push the out-of-place node downward, swapping with its larger child until the new node reaches an acceptable location.

19

4222135

23

42

27

Page 33: TREE BST HEAP GRAPH

Removing the Top of a Heap

Move the last node onto the root.

Push the out-of-place node downward, swapping with its larger child until the new node reaches an acceptable location.

19

4222127

23

42

35

Page 34: TREE BST HEAP GRAPH

Removing the Top of a Heap

The children all have keys <= the out-of-place node, or

The node reaches the leaf.

The process of pushing the new node downward is called reheapification downward.

19

4222127

23

42

35

Page 35: TREE BST HEAP GRAPH

Implementing a Heap

We will store the data from the nodes in a partially-filled array.

An array of data

2127

23

42

35

Page 36: TREE BST HEAP GRAPH

Implementing a Heap

• Data from the root goes in the first location of the array.

An array of data

2127

23

42

35

42

Page 37: TREE BST HEAP GRAPH

Implementing a Heap

• Data from the next row goes in the next two array locations.

An array of data

2127

23

42

35

42 35 23

Page 38: TREE BST HEAP GRAPH

Implementing a Heap

• Data from the next row goes in the next two array locations.

An array of data

2127

23

42

35

42 35 23 27 21

Page 39: TREE BST HEAP GRAPH

Implementing a Heap

• Data from the next row goes in the next two array locations.

An array of data

2127

23

42

35

42 35 23 27 21

We don't care what's inthis part of the array.

Page 40: TREE BST HEAP GRAPH

GraphA graph that consists of a set of nodes (vertices) and a set of edges that relate the nodes to each other.

Page 41: TREE BST HEAP GRAPH

Degree: The number of edge of a node is called degree of that node.

Connected Graph: A graph said to be connected if and only if there is a simple path between two nodes.

Page 42: TREE BST HEAP GRAPH

Complete Graph: A graph is said to be complete if every node is adjacent to every other node.

Tree Graph: A connected graph without any cycle is called a tree graph or free tree or only tree.

Page 43: TREE BST HEAP GRAPH

Cycle: Cycle is a closed simple path with length three or more.

Here,ACDA is a cycle in this Graph.

Labeled Graph: A graph is said to be labeled if its edegs are assigned data.

Page 44: TREE BST HEAP GRAPH

Weighted Graph: A graph is said to be weighted if each edge is assigned a non-negative value called the weight of length.

Page 45: TREE BST HEAP GRAPH

GRAPHMULTIPLE EDGE / DISTINCT EDGE: If two edge connected the same end points.

LOOP : An edge is called a loop if it has same identical end points.

Page 46: TREE BST HEAP GRAPH

DIRECTED GRAPG

DIRECTED GRAPH : In a graph the vertices that are connected together , where all the edges are directed from one two another vertices .From this graph v1 is initial points and v2 is terminal points.v1 is a predecessor of v2 and v2 is successer of v1 .

DEGREE : Directed graph has two types degree .• Indegree• Outdegree

Page 47: TREE BST HEAP GRAPH

DIRECTED GRAPH

SOURCE : A node is called as a source if it has a positive out degree and 0 indegree .

SINK : A node is called as a sink if it has a positive indegree and 0 out degree .

Page 48: TREE BST HEAP GRAPH

ADJACENT NODE : Two nodes are adjacent if they connected via an edge .

ADJANCENT MATRIX : Connectivity between two nodes can be tested quickly.

ADJACENT

Page 49: TREE BST HEAP GRAPH

ADJACENTADJACENT LIST : Adjacent node can be represented also adjacent list.

• 0 -> 1• 1 -> 2 , 3• 2 -> • 3 -> 0 , 2

Page 50: TREE BST HEAP GRAPH

THANK YOU