C++ Trees presentation

Attribution Non-Commercial (BY-NC)

8 views

13 Tree

C++ Trees presentation

Attribution Non-Commercial (BY-NC)

- Hacking a Google Interview Handout 3
- Data Structure Practical Questions
- Bst
- C Program
- Klein Splay Trees and Link-cut Trees
- CS-102 AVLS
- Tree Plan Guide 176
- Aschheim Resume and FQXI Essay
- DSA Question Bank
- 0907.5433
- Enhancement
- Tree
- All Proc
- Udaya School of Engineeringx
- lec15
- 02. Eksplorasi Divide & Conquer.pdf
- CS301-lec15
- SAP ME How-To-Guide - Nonconformance
- Tutorial Safeti and Safeti Lite
- Two Factorial Definitions

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

- Hacking a Google Interview Handout 3Uploaded bymachinelearner
- Data Structure Practical QuestionsUploaded byJayeeta nayak
- BstUploaded byapi-3830748
- C ProgramUploaded byprabu1928
- Klein Splay Trees and Link-cut TreesUploaded byDanh Nguyen
- CS-102 AVLSUploaded bySai Avinash Duddupudi
- Tree Plan Guide 176Uploaded byStraub1979
- Aschheim Resume and FQXI EssayUploaded byRay Aschheim
- DSA Question BankUploaded byanurekhar
- 0907.5433Uploaded byAncuța Nicu
- EnhancementUploaded byJoy Chakravorty
- TreeUploaded byAnonymous V7P5fNQI
- All ProcUploaded byHilton Oliveira
- Udaya School of EngineeringxUploaded byArul Prabhin
- lec15Uploaded byShengFeng
- 02. Eksplorasi Divide & Conquer.pdfUploaded byLukmanul Hakim Abdul Salam
- CS301-lec15Uploaded byism33
- SAP ME How-To-Guide - NonconformanceUploaded byOlya Solar
- Tutorial Safeti and Safeti LiteUploaded byEfari Bahchevan
- Two Factorial DefinitionsUploaded byCiprian HC
- Binary Trees 1Uploaded bysushma
- Training Manual for the Client-Server Version of ProActiveUploaded byA. El Mahdi
- asm 1Uploaded byLê Văn Phương
- INFO1905 Cheat Sheet FinalUploaded byJeffrey
- CadabraUploaded byAnurag
- 2012 Test Ian AnswersUploaded byJazal Ahamed
- Lecture18(Heaps,Hashing)Uploaded byavinash
- Dsa Mk Lect1Uploaded byAnkit Priyarup
- Size Balanced TreeUploaded byFrankie Liu
- Structured and Semistructured _Database Programming Languages_DBPL-99Uploaded byL'enin Aleph

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

- RATP safety approach for railway signalling systems.pdfUploaded bypoketupi
- Spong-Textbook.pdfUploaded byIvan Avramov
- WarehousingUploaded byMervyn Joe Sebastian
- Www Gamefaqs Com_2 Torchlight IIUploaded byAlexander Abonado
- Final Year Project ProposalUploaded byATUNBI
- YaoUploaded bySarojini Sellamuthu
- Academy Module3 WebUploaded byYoonee Jeong
- How to Add Realistic Post-production Shadows in Photoshop 3D Artist - Animation, Models, Inspiration & Advice 3DArtist MagazineUploaded byAravind
- Automate Now and Move In-Line. X-Line Designed & Built by EnduroShield.Uploaded byPR.com
- Rotel RT-09Uploaded byPirvan Nicoleta
- Ssr With Facts StatcomUploaded byArun Joseph
- Airways Kpis DefinitionUploaded byshadabkhan321
- SCR C106D Equivalent CircuitUploaded byJohn_pet93
- DD-12-033_A Instruction for Smartpack Installation and Configuration (E63451818-ALU)Uploaded byLivington Briones
- 325843797 Digital Crime and TerrorismUploaded bysilvicutza
- Erp History PDFUploaded byTiffany
- crypto_ch02Uploaded byapi-19876649
- Automatic Cab Signaling System for Rail EnginesUploaded bysvijay_790
- Catalogo Square D MCBUploaded byHugo Palencia
- Peace Corps FOIA Sexual Assault OSS Trainng Response LetterUploaded byAccessible Journal Media: Peace Corps Documents
- CUMHROM-TQM-Notesgiven2010(B&W)Uploaded bytubai25
- IGNOU MCS-024 Solved Assigment 2013-14Uploaded byLakshman Swamy
- Automatic License Plate RecognitionUploaded bykavithekiran
- UseCases NotesUploaded byhaaaaamid
- US Army: DoD Contracting GuideUploaded byArmy
- Speakout Vocabulary Extra Pre-Intermediate Unit 11Uploaded byshasha1982
- Data Guard11gUploaded byanand
- SBI_513_11-12-15Uploaded byJosé López
- Virtual Reality and Agumented Reality_2020Uploaded byangelinawatson
- En 13263-1 CommentsUploaded byLefteris Anastasiou

## Much more than documents.

Discover everything Scribd has to offer, including books and audiobooks from major publishers.

Cancel anytime.