comp 245 data structures

33
Comp 245 Data Structures Trees

Upload: hugh

Post on 12-Jan-2016

37 views

Category:

Documents


0 download

DESCRIPTION

Comp 245 Data Structures. Trees. 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. Terminology Using a Binary Tree. Root Child - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Comp 245 Data Structures

Comp 245Data Structures

Trees

Page 2: Comp 245 Data Structures

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.

Page 3: Comp 245 Data Structures

Terminology Using a Binary Tree

Root

Child

Parent

Leaf

Height

Level

Page 4: Comp 245 Data Structures

Full, Complete, Balanced

A FULL Tree A COMPLETE Tree A BALANCED Tree

Page 5: Comp 245 Data Structures

Traversing a Tree

PRE – order

(v)isit – (l)eft – (r)ight VLR

POST – order

(l)eft – (r)ight – (v)isit LRV

IN – order

(l)eft – (v)isit – (r)ight LVR

Page 6: Comp 245 Data Structures

A PRE-order Traversal(VLR)

124853697

This traversal is considered “top-down”

Page 7: Comp 245 Data Structures

A POST-order Traversal(LRV)

845296731

This traversal is considered “bottom-up”

Page 8: Comp 245 Data Structures

An IN-order Traversal(LVR)

This traversal is considered “left to right”

842516937

Page 9: Comp 245 Data Structures

Traversal Practice

Page 10: Comp 245 Data Structures

Traversal Practice

Pre124578369

Post478529631

In427581396

Page 11: Comp 245 Data Structures

Sample Code of a Traversal

void Tree::InOrder(TreePtr P){

if (P != NULL){

InOrder(P->Left);Process(P);InOrder(P->Right);

}}

Page 12: Comp 245 Data Structures

Implementing a Binary TreeLinked

struct Node;

typedef SomeDataType TreeType;

typedef Node* TreePtr;

struct Node{

TreeType info;TreePtr Left, Right;

};

Page 13: Comp 245 Data Structures

Defining a Binary TreeLinked

class Tree{ public:

Tree();~Tree();bool Empty();bool Insert(TreeType);bool Delete(TreeType);void Traverse();

private:void InOrder(TreePtr);TreePtr Root;

};

Page 14: Comp 245 Data Structures

Binary Search TreesBST

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.

Page 15: Comp 245 Data Structures

Example of BST Efficiency

How many accesses to find R?

How many accesses to find R?

Page 16: Comp 245 Data Structures

BST Efficiency

Assuming a tree is balanced, it’s efficiency is approximately log2N where N is the number of elements in the tree.

Example:There are 1000 elements in a BST, it’s efficiency therefore

is approximately log21000 = 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!

Page 17: Comp 245 Data Structures

BST Operation - Insertion

The Insert function can be highly efficient.

The new value is always inserted as a leaf node!

Page 18: Comp 245 Data Structures

BST Operation – InsertionPractice: Build a BST

Build a BST from these values:

LARRYFREDJOESTEVENANCYBILLCAROLTERRY

Page 19: Comp 245 Data Structures

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.

Page 20: Comp 245 Data Structures

Deleting a Node from a BST

There are three cases to account for:LeafOne ChildTwo Child

The algorithm requires a Search to find the node to delete, determining the specific case, and then executing the deletion.

Page 21: Comp 245 Data Structures

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.

Page 22: Comp 245 Data Structures

Delete Leaf Code

void Bst::DeleteLeaf (TreePtr P, TreePtr Parent){

//check for rootif (Parent == NULL)

Root = NULL;else

if (Parent->Left == P)Parent->Left = NULL;

elseParent->Right = NULL;

delete P;}

Page 23: Comp 245 Data Structures

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.

Page 24: Comp 245 Data Structures

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

}

Page 25: Comp 245 Data Structures

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.

Page 26: Comp 245 Data Structures

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.

Page 27: Comp 245 Data Structures

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

}

Page 28: Comp 245 Data Structures

Traversal UsagePreorder

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.

MikeDonHarryGregTimPaulWayne

Page 29: Comp 245 Data Structures

Traversal UsageInorder

The inorder traversal can be used to obtain a sorted

list from a BST.

DonGregHarryMikePaulTimWayne

Page 30: Comp 245 Data Structures

Traversal UsagePostorder

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 DeletionGregHarryDonPaulWayneTimMike

Page 31: Comp 245 Data Structures

Binary Tree ImplementationArray 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.

Page 32: Comp 245 Data Structures

Binary Tree ImplementationArray 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.

Page 33: Comp 245 Data Structures

N-Ary TreesFirst Child-Sibling Implementation