8 views

Uploaded by frankjamison

C++ Trees presentation

- AC06-AT06
- GATE Questions 16-7-13
- UGC NET Computer Science & Applications Paper 2 Dec. 2012
- D3 Tips and Tricks
- ICS 2105 – DSA
- Binary Trees
- Data Structures and Algorithms
- Buses For Anonymous Message Delivery
- Node Failure Time and Coverage Loss Time Analysis for Maximum Stability vs Minimum Distance Spanning Tree Based Data Gathering in Mobile Sensor Networks
- BinarySearchAVLTree.java
- DS LAB PROGRAMS.docx
- Data Strutures and Algorithm
- Written Assignment1 Sample Solution
- Algorithims
- 4401sm
- dt_internals.pdf
- XML diff survey
- Full Software preparation.pdf
- 04- Adversarial Search AI
- Huff Man

You are on page 1of 59

This lecture prepared by the instructors at the University of Manitoba in Canada and has been modified by Dr. Ahmad Reza Hadaegh

1

Trees

A tree is a linked data structure which reflects some hierarchical structure There are several varieties of trees

Each with its own characteristics v A family of related data structures

Trees

u

A tree has a root, branches, leaves

Root node A C F I Leaf nodes G Branches

B E

D H

J

3

Trees

u

The nodes are organized in a top-down hierarchical manner

A graph consists of vertices (nodes) and edges (branches), but is not necessarily hierarchical in nature v Sometimes tree and graph terminology is interchanged

4

Trees

A B

C

F I G

D H J

10 nodes, 9 branches, 1 root node, and 6 leaf nodes

5

Trees

u

If node A points to node B, then A is the parent of B v Furthermore, B is the child of A Children of the same parent are siblings All nodes beneath a given node (children, children of children, ) are descendants of that node All nodes on the path between the root and a given node are ancestors of that node

6

Trees

A B E F I

u u

C G

D H J

u

u u

The parent of G is D and the parent of E is B The children of A are B, C, and D B, C, and D are sibling The ancestors of J are G, D, and A The descendants of D are G, H, I and J

Trees

u

Insert v Add a new node into a tree Delete v Remove a node from a tree Search v Find a node (if it exists) that contains a given key Traverse v Systematically examine (an operate on) all nodes in a tree

8

Binary Trees

u

Binary trees

A binary tree is a tree in which no node has more than two children

A B D E G C F H

9

An ordered binary tree is a binary tree in which

applied recursively throughout the tree structure

10

u

A B

C

E G F H

11

u

D B

E

C F G H

12

u

D C

F

E G H

13

A

B

u

A B C D E

F

G H

14

Ordered binary trees are often referred to as binary search trees (BST)

They tend to be a co-operative structure on which to work

15

Searching a BST

u

Searching a BST

Algorithm v Start with root node v if search key = node key then stop v else if search key < node key then go left v else go right v Stop if at any time during the search, you fall off the tree, stop

16

Searching a BST

u

D B

E

C F G H

17

Searching a BST

u

D B

E

C F G H

18

Searching a BST

Given an existing BST, searching for a key is simple

Effectively a binary search v Is it as efficient as a binary search? Algorithm is recursive, but can be trivially implemented iteratively Could be implemented as follows:

19

Searching a BST

bool Search(nodeptr curr, char key) {

bool found; nodeptr curr;

curr = root; found = false; while ((curr != NULL) && (! found)) if (curr->data == key) found = true; else if (key < curr->data) curr = curr->left; else curr = curr->right; return found; }

20

Traversing a Tree

u

In a tree traversal, every node is visited once There are three traversal orders of general interest v Pre-order, post-order, and in-order traversals

21

Pre-order Traversal

u

Pre-order traversal

Visit the current node Perform a pre-order traversal of the left sub-tree Perform a pre-order traversal of the right sub-tree A naturally recursive algorithm v Easy to program recursively Not so easy to program iteratively

22

Pre-order Traversal

u

void postOrder (nodeptr sroot) { if (sroot != NULL) { postOrder (sroot->left); postOrder (sroot->right); cout << sroot->data; } }

23

Post-order Traversals

Post-order traversal

Perform a post-order traversal of the left sub-tree Perform a post-order traversal of the right sub-tree Visit the current node

24

In-order Traversals

In-order traversal

Perform a in-order traversal of the left sub-tree Visit the current node Perform a in-order traversal of the right sub-tree

25

Insertions

u

Binary trees are normally created by inserting nodes into an initially empty tree

If tree empty, first insert will create the root node Otherwise, tack new node onto the (unique) appropriate node that has less than 2 children This will require a search routine

26

Insertions

u

27

Insertions

u

root 15

28

Insertions

u

? 15 8 root

void r_insert (nodeptr& if (root == null) root = newnode; else if (newnode->data r_insert (root->le else r_insert (root->rig }

29

Insertions

u

15 8 12

30

Insertions

u

15 8 12

19

31

Insertions

u

15 8 5 12

19

32

Insertions

u

15

8

5 12

19

23

33

Insertions

// This procedure implements a recursive insert. // Probably better to do this iteratively. Why? void r_insert (nodeptr& root, nodeptr newnode) { if (root == null) root = newnode; else if (newnode->data < root->data) r_insert (root->left, newnode); else r_insert (root->right, newnode); }

34

Deletions

This is an optional topic. I strongly recommend you to learn it but it will not be in the exam.

35

Deletions

u

Why?

When deleting from a BST, the algorithm must often reorganize tree while maintaining the inherent ordering

Three cases to consider Deleting a leaf node Easy -- detach leaf node from tree by setting its parents pointer to nil

36

Deletions

u

Delete 12

15

8

5 12

19

23

37

Deletions

u

Delete 12

15

8

5 12

19

23

38

Deletions

u

Done!

15

8

5

19

23

39

Deletions

Deleting a node with a single child Easy -- replace the node to be deleted with its child

40

Deletions

u

Delete 19

15

8

5 12

19

23

41

Deletions

u

Delete 19

15

8

5 12

19

23

42

Deletions

u

Done!

15

8

5 12

23

43

Deletions

Deleting

a node with two children A little more difficult Both subtrees of the deleted node must be linked back into the tree while retaining order

44

Deletions

u

Delete 21

12 8 21 10 17

23

14

19

22

26

45

Deletions

u

Delete 21

12 8 21 10 17 These nodes must be properly linked back into the tree

23

14

19

22

26

46

Deletions

u

Replace the deleted node with one of its children, say the left child (i.e. node 17) v The remaining right child (i.e. node 23) must now be linked back into the tree Place it on the right of the largest node of the new left subtree

47

Deletions

u

Delete 21

12 8 7 10 17 21 23

Replace the deleted node with one of its children, say the left child (i.e. node 17)

14

19

22

26

48

Deletions

u

Delete 21

12 8 7 10 17 23

14

19

22

26

49

Deletions

u

Delete 21

12 8 7 10 17 23 Place it (node 23) on the right of the largest nose of the new left subtree

14

19

22

26

50

Deletions

u

Done!

12

8

7

17

10

14

19

23

22

23

51

Deletions

Another way

Locate the in-order successor of the node to be deleted (the node that contains the next highest value) v To locate the in-order successor, go right, then go left as far as possible Replace the contents of the node to be deleted with the contents of its in-order successor Delete the successor node v This algorithm is possible since the in-order successor node will never have more than one child

52

Deletions

u

Delete 21

12 8 21 10 17

23

14

19

22

26

53

Deletions

u

Delete 21

12 8 21 10 17 Replace the contents of the node to be deleted with the contents of its in-order successor

23

14

19

22

26

54

Deletions

u

Delete 21

12 8 22 10 17

23

14

19

21

26

55

Deletions

u

Delete 21

12 8 22 10 17 Delete the successor node

23

14

19

21

26

56

Deletions

u

Delete 21

12 8 22 10 17 Delete the successor node

23

14

19

21

26

57

Deletions

u

Done!

12 8 7 10 17 22 23

14

19

26

58

Deletions

59

- AC06-AT06Uploaded byNavaneeth Nair
- GATE Questions 16-7-13Uploaded byshinde_jayesh2005
- UGC NET Computer Science & Applications Paper 2 Dec. 2012Uploaded byNibedita Khandual
- D3 Tips and TricksUploaded byEric Kartman Lee
- ICS 2105 – DSAUploaded bySteve Ouma
- Binary TreesUploaded byGobara Dhan
- Data Structures and AlgorithmsUploaded bySean Bugeja
- Buses For Anonymous Message DeliveryUploaded byGriffin Boyce
- Node Failure Time and Coverage Loss Time Analysis for Maximum Stability vs Minimum Distance Spanning Tree Based Data Gathering in Mobile Sensor NetworksUploaded byAIRCC - IJCNC
- BinarySearchAVLTree.javaUploaded byAnimesh Shaw
- DS LAB PROGRAMS.docxUploaded byShobha Kumar
- Data Strutures and AlgorithmUploaded byGiovanni de los Santos
- Written Assignment1 Sample SolutionUploaded byjackaccyou
- AlgorithimsUploaded byPradeep Soni
- 4401smUploaded byRaktim
- dt_internals.pdfUploaded byscribd7942
- XML diff surveyUploaded bymicrodan8710
- Full Software preparation.pdfUploaded byJeremy Dunn
- 04- Adversarial Search AIUploaded byLamiaaAbdRabou
- Huff ManUploaded byijnm5uhb
- ps3solUploaded byJayesh Gorasia
- High Order TreeUploaded bysathishvishva
- Data Structures AptitudeUploaded byChillax Vicki
- 2_3 Heap sort.pptUploaded bybara_sachin
- IOPC14.pdfUploaded byPuneet Jindal
- Mining Concise And Lossless Location-Aware News Feed Representation by Using Efficiency AlgorithmUploaded byIRJET Journal
- Cf CollectionsUploaded bySworna Vidhya
- A Survey on Relation ExtractionUploaded byOctav Porumb
- A9R5DF8Uploaded byrenzo_ortiz
- BTree MotivationUploaded byTippu Sultan

- 05c-HeapTree (Additional Note)Uploaded byfrankjamison
- 11 Graphs MSTUploaded byfrankjamison
- 06 TreeInGeneral(Additional)Uploaded byfrankjamison
- 08a-DivideAndConquerUploaded byfrankjamison
- 05b-HeapSortingUploaded byfrankjamison
- 04b MoreOnStack Queue(AdditionalNote)Uploaded byfrankjamison
- 07c BackTrackingUploaded byfrankjamison
- 07d-TopologicalSortUploaded byfrankjamison
- 07a-DepthAndBreathSearchUploaded byfrankjamison
- 05a-HeapTreeUploaded byfrankjamison
- 07b-DepthAndBreathSearch(AdditionalNotes)Uploaded byfrankjamison
- 09-BalancingATreeUploaded byfrankjamison
- 10-AVLTreeUploaded byfrankjamison
- 07e-HamiltoniannCycleUploaded byfrankjamison
- 12 Graphs ShortestPathUploaded byfrankjamison
- Lay Linalg5!01!05Uploaded byfrankjamison
- Lay Linalg5!01!02Uploaded byfrankjamison
- Lay Linalg5!01!04Uploaded byfrankjamison
- 02a-RunningTimeUploaded byfrankjamison
- CSC331 Week 1 Topic AUploaded byfrankjamison
- Lay Linalg5!02!01Uploaded byfrankjamison
- 04a-StackAndQueueUploaded byfrankjamison
- 02b RunningTime (Optional)Uploaded byfrankjamison
- Lay Linalg5!01!03Uploaded byfrankjamison
- Lay Linalg5!02!03Uploaded byfrankjamison
- CSC331 Week 2 Topic DUploaded byfrankjamison
- 01 IntroductionUploaded byfrankjamison
- CSC331 Week 1 Topic BUploaded byfrankjamison
- Lay Linalg5!01!07Uploaded byfrankjamison
- CSC331 Week 1 Topic CUploaded byfrankjamison

- FactoryTalk View Site EditionUploaded byMichael Akpan
- PowerOn_fusion.pdfUploaded byJagan Vanama
- How Cisco Uses Splunk on Cisco UCS for IT OperationsUploaded byCisco IT
- A Swarm Inspired Multipath Data Transmission With Congestion Control In Manets Using Probabilistic ApproachUploaded byJohn Berg
- Oracle EBS R12 Upgrade Case StudyUploaded byannurbala
- Library Management System projectUploaded byrachna_raghuwanshi
- Texas Instruction SetUploaded bySREEJOTHI
- Sap Function BapiUploaded byrjsoni30in
- Interest Rate Modelling Piterbarg Table of ContentsUploaded byKranthi Kiran
- leap and exoskeleton file revision.docxUploaded byshadrack
- Magistere Lamia BENMIHOUBUploaded byouael
- LD ManualUploaded bysyeddanishrasool
- Rave Dev Guide 5Uploaded bysandrolp_15
- Chapter 7001Uploaded byjangra014
- Transaction Codes for MRP (PP-MRP) - Material Requirements Planning (PP-MRP) - SAP LibraryUploaded byPavilion
- Task Manager Has Been Disabled.pdfUploaded byboydiego
- SQL operators OracleUploaded byB3554
- Bandwidth CalculatorUploaded bySenan Alkaaby
- Coupling and Cohesion StudentUploaded byShweta Yadav
- PowerEdge R820.pdfUploaded byAhmed Abdelfattah
- cactiUploaded bysigoisette
- Clab SolutionUploaded bypramod_076
- GP1-Unity3DUploaded byCarlos Bott
- Auto RunUploaded bySang Pencari Ilmu
- MS Excel_ Nested if Functions (WS)Uploaded byLance Lindsey
- Lesson01_ IntroductionToMicrosoftAccessUploaded bythe_worm_2515
- SwitchingUploaded byaswin
- Introduction to JavaScriptUploaded byCraig
- Audit of Accounts ReceivableUploaded byKairos Le
- CSPMasterUploaded byRobert Kacz