Download presentation

Presentation is loading. Please wait.

Published byMelany MacPherson Modified over 2 years ago

1
Comp 245 Data Structures Trees

2
Introduction to the Tree ADT A tree is a non-linear structure. A treenode can point to 0 to N other nodes. There is one access point to the tree; it is called the root. A tree is recursive in nature.

3
Terminology Using a Binary Tree Root Child Parent Leaf Height Level

4
Full, Complete, Balanced A FULL TreeA COMPLETE TreeA BALANCED Tree

5
Traversing a Tree PRE – order (v)isit – (l)eft – (r)ightVLR POST – order (l)eft – (r)ight – (v)isitLRV IN – order (l)eft – (v)isit – (r)ightLVR

6
A PRE-order Traversal (VLR) 124853697124853697 This traversal is considered “top-down”

7
A POST-order Traversal (LRV) 845296731845296731 This traversal is considered “bottom-up”

8
An IN-order Traversal (LVR) This traversal is considered “left to right” 842516937842516937

9
Traversal Practice

10
Pre 1 2 4 5 7 8 3 6 9 Post 4 7 8 5 2 9 6 3 1 In 4 2 7 5 8 1 3 9 6

11
Sample Code of a Traversal void Tree::InOrder(TreePtr P) { if (P != NULL) { InOrder(P->Left); Process(P); InOrder(P->Right); }

12
Implementing a Binary Tree Linked struct Node; typedef SomeDataType TreeType; typedef Node* TreePtr; struct Node { TreeType info; TreePtr Left, Right; };

13
Defining a Binary Tree Linked class Tree { public: Tree(); ~Tree(); bool Empty(); bool Insert(TreeType); bool Delete(TreeType); void Traverse(); private: void InOrder(TreePtr); TreePtr Root; };

14
Binary Search Trees BST A special type of tree that is very useful! Main characteristic: Given any node P, the left child is lesser than or equal to P; the right child is greater than P. The efficiency of a BST ranges from logarithmic time to linear time.

15
Example of BST Efficiency How many accesses to find R?

16
BST Efficiency Assuming a tree is balanced, it’s efficiency is approximately log 2 N where N is the number of elements in the tree. Example: There are 1000 elements in a BST, it’s efficiency therefore is approximately log 2 1000 = 9.9 or 10. This means that it will take in the absolute worst case, 10 accesses to find a value in the tree. If you contrast this to an ordered list, it will take 1000 accesses in the worst case and 500 in the average case to find an element!! If a tree is not balanced, it’s efficiency will degenerate!

17
BST Operation - Insertion The Insert function can be highly efficient. The new value is always inserted as a leaf node!

18
BST Operation – Insertion Practice: Build a BST Build a BST from these values: LARRY FRED JOE STEVE NANCY BILL CAROL TERRY

19
Inserting a Node into a BST Create a node (Test for success) Store data, set right and left pointers null (it will be a leaf) Search tree for insertion point, keep track of node which will become the parent. Attach this node to parent. Return success or failure of operation.

20
Deleting a Node from a BST There are three cases to account for: Leaf One Child Two Child The algorithm requires a Search to find the node to delete, determining the specific case, and then executing the deletion.

21
Leaf Case How do you know the node is a leaf? This routine will require 1) a pointer to the node to be deleted and 2) a pointer to the parent.

22
Delete Leaf Code void Bst::DeleteLeaf (TreePtr P, TreePtr Parent) { //check for root if (Parent == NULL) Root = NULL; else if (Parent->Left == P) Parent->Left = NULL; else Parent->Right = NULL; delete P; }

23
One Child Case How do you know the node has one child? This routine will require 1) a pointer to the node to be deleted and 2) a pointer to the parent.

24
Delete One Child Code void Bst::DeleteOneChild (TreePtr P, TreePtr Parent) { 1) save pointer to subtree, must be re-attached 2) check for root case 3) re-attach subtree to parent 4) delete P }

25
Two Child Case How do you know the node has two children? This routine will require only a pointer to the node to be deleted.

26
Finding the Closest Predecessor From the two child node to be deleted, take one step left and go as far right as possible. This node is the closest predecessor. Place this value in the node to be deleted. The closest predecessor will be deleted by calling DeleteLeaf or DeleteOneChild.

27
Delete Two Children Case void Bst::DeleteTwoChild (TreePtr P) { 1) Find closest predecessor (cp), keep track of parent to cp!! 2) Copy cp->info to P->info 3) Call DeleteLeaf or DeleteOneChild for cp }

28
Traversal Usage Preorder The preorder traversal can be used to effectively save a tree to file that can be reconstructed identically. This type of traversal can be used to copy a tree also. Mike Don Harry Greg Tim Paul Wayne

29
Traversal Usage Inorder The inorder traversal can be used to obtain a sorted list from a BST. Don Greg Harry Mike Paul Tim Wayne

30
Traversal Usage Postorder The postorder traversal can be used to delete a tree. A tree needs to be deleted from the bottom up because every node at the point of deletion is a leaf. Order of Deletion Greg Harry Don Paul Wayne Tim Mike

31
Binary Tree Implementation Array Based – method 1 The first method will store information in the tree traveling down levels going left to right. Given this storage technique, a node stored at slot I in the array will have it’s left child at 2I + 1, and the right child will be at 2I + 2. A parent can be found at (I – 1)/2.

32
Binary Tree Implementation Array Based – method 2 The second method will have an array of structs. Each struct will contain the information and left and right pointer fields. The pointer fields will simply be index values within the array. Each new value is added at the end of the array as a leaf and the pointer to it’s parent adjusted.

33
N-Ary Trees First Child-Sibling Implementation

Similar presentations

OK

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

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

© 2018 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Earthquakes for kids ppt on batteries Ppt on sudoku game project Ppt on recycling of waste plastics Ppt on simple carburetors Download ppt on wastewater management Ppt on cd/dvd rental Ppt on grease lubrication fittings Ppt on nitrogen cycle and nitrogen fixation Ppt on bmc remedy software Ppt on chapter 3 atoms and molecules video