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.
Examples
Examples
Examples
Examples
Examples
Examples
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
Tree terminology
Tree terminology
ancestor Internal node
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.
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
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
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
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
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
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
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
depth-first traversal.
depth first traversal.
• Postorder traversal: visit root after visiting the g subtrees
breadth-first traversal.
breadth first traversal.
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
Examples of Binary Trees
Examples of Binary Trees
• Expression Treesp
• internal nodes (nonleaf) : operators • external nodes (leaf): operands
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
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 /
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();
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
Binary search tree examples
Binary search tree examples
• Binary search tree of names
Binary search tree of names
Value < root
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
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
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
Examples of Binary Trees
Examples of Binary Trees
• Examples of HeapsExamples of Heaps
Examples of General Trees
Examples of General Trees
• A parse tree for the algebraic expression
A parse tree for the algebraic expression
Examples of General Trees
Examples of General Trees