1 / 15

Non-Linear Structures

Non-Linear Structures. Trees. President. VP - Mktg. VP - Oper. VP - Sales. CIO. Sales Mgr. data. data. data. data. data. data. data. data. data. data. data. data. data. data. data. data. 17. 52. 70. 23. 90. 47. 88. 87. 99. 35. 28. 65. 55. 1. 41.

tbourque
Télécharger la présentation

Non-Linear Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Non-Linear Structures Trees

  2. President VP - Mktg VP - Oper VP - Sales CIO Sales Mgr.

  3. data data data data data data data data data data data data data data data data

  4. 17 52 70 23 90 47 88 87 99 35 28 65 55 1 41 Incoming values: 70, 23, 90, 47, 88, 52, 17, 99, 35, 28, 65, 55, 1, 41, 87 Building a Binary Search Tree

  5. BTNode - data : Object - left : BTNode - right : BTNode + createBTNode() + getData() : Object + setData() : void + getLeft() : BTNode + setLeft() : void + getRight(): BTNode + setRight(): void

  6. Generally, the procedure is the same as for adding a node to the tree. • Follow the path until you run into a node with the value you're searching for. • If you reach a null pointer, the value doesn't exist in the tree. Searching a BST

  7. 70 23 90 47 88 52 17 99 35 28 65 55 1 41 87 Searching a BST

  8. After deletion, the tree still must qualify as a BST. • Three different cases: • The node to be removed is a leaf. • The node to be removed has one child. • The node to be removed has two children. Deleting a Node from a BST

  9. 70 23 90 47 88 52 17 99 35 28 65 55 1 41 87 Deleting – Case 1

  10. 70 23 90 47 88 52 17 99 35 28 65 55 41 87 Deleting – Case 2

  11. 70 23 90 47 87 52 17 99 35 28 65 55 41 Deleting – Case 3

  12. 70 23 90 41 87 52 17 99 35 28 65 55 47 Deleting – Case 3 (Step 1)

  13. 70 23 90 41 87 52 17 99 35 28 65 55 Deleting – Case 3 (Step 2)

  14. Find the node with the value to be deleted (n1). • If it's a leaf Delete the node. • If it has one child Assign a reference to n1's child to n1's parent. • If it has 2 children Find the node with the highest value in the left sub-branch (n2). Swap the values of n1 and n2. Call delete recursively to remove the original value. BST Deletion Algorithm

  15. insert – Inserts a node at the proper location. • delete – Deletes a node based on a key. Returns true if the node deleted, and false if the key not found. • find – Finds a node based on a key, and returns the data object found in the node. Returns null if the key is not found. • clear – Empties the tree of all nodes. BST Methods

More Related