• No results found

lecture-9-1-ADT-Tree-2011-1.pdf

N/A
N/A
Protected

Academic year: 2020

Share "lecture-9-1-ADT-Tree-2011-1.pdf"

Copied!
32
0
0

Loading.... (view fulltext now)

Full text

(1)

Tree ADT

Tree ADT

• Data structures (List, Stack, Queue) are

Data structures (List, Stack, Queue) are

linear data organization, where the data

are placed in a linear order.

p

• Trees as data structures are organized tat

into hierarchical order.

(2)

Examples

Examples

(3)

Examples

Examples

(4)

Examples

Examples

(5)

Tree terminology

Tree terminology

A tree is A set of nodes connected by edgesA tree is A set of nodes connected by edges

The edges indicate relationships among nodes

Nodes arranged in levels

Indicate the nodes' hierarchyIndicate the nodes hierarchy

(6)

Tree terminology

Tree terminology

ancestor Internal node

(7)

Tree terminology

• Root: Node without parent is the root node

• Children: Nodes at a given level are children of nodes of previous level

• External Node (Leaf): Node without children( )

• Internal node (None leaf): node with at least one child

• Parent: Node with children is the parent node of those children

• Ancestors of a node: parent, grandparent, grand-grandparent, etc.

(8)

Tree terminology

Tree terminology

• Path: we can reach any node by following a Path y y g that begins at the root and goes from Node to

Node along the connections between them • Length of a path: The number of edges thatLength of a path: The number of edges that

compose it

• Subtree of a node: A tree rooted at a child of that node

A bt f t i bt f t

• A subtree of a tree is a subtree of root

• Height of a tree: the number of levels in the tree: • Height= 1+ height of the longest subtree Height 1 height of the longest subtree

• The height of Empty tree is 0

(9)

Binary tree

Binary tree

• A binary tree is either empty or has the

A binary tree is either empty or has the

following form Where Tleft and Tright are

binary trees

binary trees

(10)
(11)

Binary tree

Binary tree

• Full Tree (Proper): every none leaf has exactly ( p ) y y two children.

• Complete Tree (Improper): full to its next-to-last level but last contains as many as possible and filled in from left to right

filled in, from left to right.

• The height of a binary tree with n nodes that is • The height of a binary tree with n nodes that is

(12)

Binary tree

Binary tree

• The number of nodes in a full binary tree as aThe number of nodes in a full binary tree as a function of the tree's height, where the root

(13)
(14)

Traversals of a Tree

Visiting a node:- Processing the data within a g g node

This is the action performed on each node

d i t l f t

during traversal of a tree

A traversal can pass through a node without visiting it at that moment

visiting it at that moment

For a binary tree

Visit the rootVisit the root

Visit all nodes in the root's left subtree

(15)

depth-first traversal

depth first traversal

Preorder traversal: visit root before the subtrees

Visit the root of a binary tree. (Do some action)Traverse the left subtree in preorder manner

(16)

depth-first traversal

depth first traversal

Inorder traversal: visit root between visiting the g subtrees

Traverse the left subtree in inorder manner Visit root

Visit root

(17)

depth-first traversal.

depth first traversal.

Postorder traversal: visit root after visiting the g subtrees

(18)

breadth-first traversal.

breadth first traversal.

(19)

Traversals of a General Tree

A general tree has traversals that are in

A general tree has traversals that are in

Level order Preorder

Preorder

Postorder

I

d

t

l

t

ll d fi

d f

(20)
(21)

Examples of Binary Trees

Examples of Binary Trees

Expression Treesp

• internal nodes (nonleaf) : operators • external nodes (leaf): operands

(22)

Algorithm for evaluating an expression tree in postorder traversal

posto de t a e sa

Algorithm evaluate(expressionTree) if (expressionTree is empty)

return 0 else

else

{ firstOperand = evaluate(left subtree of expressionTree)

secondOperand = evaluate(right subtree of expressionTree) operator = the root of expressionTree

return the result of the operation operator and its operands return the result of the operation operator and its operands

(23)

Examples of Binary Trees

Examples of Binary Trees

Decision Trees: A decision tree can be the basis of an

expert system Helps users solve problems, make decisions

i t l d ti ith /

(24)

A possible Java interface for a binary decision tree.

t ee

public interface DecisionTreeInterface extends BinaryTreeInterface

{ /** Task: Gets the data in the current node.

* @return the data object in the current node */

public Object getCurrentData(); public Object getCurrentData();

/** Task: Determines whether current node contains an answer. * @return true if the current node is a leaf */

public boolean isAnswer();

/** Task: Moves the current node to the left (right) child of the / Task: Moves the current node to the left (right) child of the current node. */

public void advanceToNo();

public void advanceToYes();

/** Task: Sets the current node to the root of the tree.*/ / Task: Sets the current node to the root of the tree. /

public void reset();

(25)

Examples of Binary Trees

Examples of Binary Trees

Binary Search Trees

Binary Search Trees

• A search tree organizes its data so that a

search is more efficient

search is more efficient

• Binary search tree

– Nodes containNodes contain ComparableComparable objectsobjects

– A node's data is greater than the data in the node's left subtree

(26)

Binary search tree examples

Binary search tree examples

• Binary search tree of names

Binary search tree of names

Value < root

(27)

Binary search tree examples

Binary search tree examples

• If you have the following sequence of integersIf you have the following sequence of integers and wanted to insert them in a Binary search tree. The sequence read from left to write.

• 8,7,8,12,2,5,14,6,11,13,17

8 7

Value < root 8

8

Value >= root 7

Value < root

(28)

An algorithm for searching a binary search tree

Algorithm bstSearch(binarySearchTree, desiredObject)

// Searches a binary search tree for a given object.

// Returns true if the object is found.

if (binarySearchTree is empty) if (binarySearchTree is empty)

return false

else if (desiredObject .equals( object in the root of binarySearchTree)) return true

else if (desiredObject .compareTo( object in the root of binarySearchTree)<0) e se (des edObject co pa e o( object t e oot o b a ySea c ee) 0)

return bstSearch(left subtree of binarySearchTree, desiredObject) else

(29)

Examples of Binary Trees

Examples of Binary Trees

Heapsp

• A complete binary tree

– Nodes contain Comparable objects

Each node contains no smaller (or no larger) than – Each node contains no smaller (or no larger) than

objects in its descendants

Maxheap

– Object in a node is ≥ its descendant objects

Minheap

– Object in a node isObject in a node is ≤≤ descendant objectsdescendant objects

(30)

Examples of Binary Trees

Examples of Binary Trees

• Examples of HeapsExamples of Heaps

(31)

Examples of General Trees

Examples of General Trees

• A parse tree for the algebraic expression

A parse tree for the algebraic expression

(32)

Examples of General Trees

Examples of General Trees

References

Related documents

If the input is greater than the root, it becomes the right child of the root (or, recursively, it goes into the right subtree.).. Example 1: Build a BST from the

AVL condition: For every node, the height of its left subtree and right subtree differ by at most 1... How Long Does

 Revenues from product sales. Upfront payments are received from licensee pharmaceutical companies when the agreement is signed, while milestone payments are generated in

To append data, a client first determines the next available position in the shared log – using a sequencer node as an optimization for avoiding contention with other appending

Issue Contractor A Certificate of Substantial Completion, Issue Contractor A Certificate of Substantial Completion,. Final Punch List &amp; Set Final Completion Date Final Punch

As proved in Lemma 1, the right subtree of a tree node T in the Kd-tree can be eliminated when no matching filter or marker is found in T : In contrast, the left subtree cannot

Keys in left subtree v.left are smaller than v.key Keys in right subtree v.right are greater than