Upload
pierce-small
View
229
Download
0
Embed Size (px)
DESCRIPTION
Observations (so far data structures) Array – Unordered Add, delete, search – Ordered Linked List – ??
Citation preview
BINARY TREES
10-2
Objectives• Define trees as data structures• Define the terms associated with trees• Discuss tree traversal algorithms• Discuss a binary tree implementation• Examine a binary tree example
Observations (so far data structures)
• Array– Unordered
• Add, delete, search
– Ordered• Linked List
– ??
Why use a Tree?
• Fundamental data storage structures used in programming.
• Combines advantages of an ordered array and a linked list.
• Searching as fast as in ordered array. • Insertion and deletion as fast as in linked list.
10-5
Trees• A tree is a nonlinear data structure used to
represent entities that are in some hierarchical relationship
• Examples in real life: • Family tree• Table of contents of a book• Class inheritance hierarchy in Java• Computer file system (folders and subfolders)• Decision trees• Top-down design
10-6
Example: Computer File System
Root directory of C drive
Documents and Settings Program Files My Music
Desktop Favorites Start Menu Microsoft OfficeAdobe
10-7
Tree Definition• Tree: a set of elements of the same type such
that• It is empty• Or, it has a distinguished element called the
root from which descend zero or more trees (subtrees)
• What kind of definition is this?• What is the base case?• What is the recursive part?
10-8
Tree Definition
Subtrees of the root
Root
10-9
Tree Terminology
Leaf nodes
RootInterior nodes
10-10
Tree Terminology• Nodes: the elements in the tree• Edges: connections between nodes• Root: the distinguished element that is the
origin of the tree• There is only one root node in a tree
• Leaf node: a node without an edge to another node
• Interior node: a node that is not a leaf node• Empty tree has no nodes and no edges
10-11
• Parent or predecessor: the node directly above in the hierarchy• A node can have only one parent
• Child or successor: a node directly below in the hierarchy
• Siblings: nodes that have the same parent• Ancestors of a node: its parent, the parent of its
parent, etc.• Descendants of a node: its children, the children of its
children, etc.
Tree Terminology
10-12
Height of a Tree• A path is a sequence of edges leading from
one node to another• Length of a path: number of edges on the
path• Height of a (non-empty) tree : length of
the longest path from the root to a leaf• What is the height of a tree that has only a
root node?• By convention, the height of an empty tree is
-1
10-13
Level of a Node• Level of a node : number of edges
between root and node• It can be defined recursively:
• Level of root node is 0• Level of a node that is not the root node is
level of its parent + 1• Question: What is the level of a node in
terms of path length?• Question: What is the height of a tree in
terms of levels?
10-14
Level of a Node
Level 0
Level 1
Level 2
Level 3
10-15
Subtrees• Subtree of a node: consists of a child
node and all its descendants• A subtree is itself a tree• A node may have many subtrees
10-16
Subtrees
Subtrees of the root node
10-17
Subtrees
Subtrees of the node labeled E
E
10-18
More Tree Terminology
• Degree or arity of a node: the number of children it has
• Degree or arity of a tree: the maximum of the degrees of the tree’s nodes
10-19
Binary Trees• General tree: a tree each of whose nodes
may have any number of children• n-ary tree: a tree each of whose nodes may
have no more than n children• Binary tree: a tree each of whose nodes
may have no more than 2 children• i.e. a binary tree is a tree with degree (arity) 2• The children (if present) are called the left
child and right child
10-20
• Recursive definition of a binary tree:it is• The empty tree• Or, a tree which has a root whose left and right
subtrees are binary trees
• A binary tree is a positional tree, i.e. it matters whether the subtree is left or right
Binary Trees
10-21
Binary Tree
A
IH
D E
B
F
C
G
Binary Trees• Every node in a binary tree
can have at most two children.
• The two children of each node are called the left child and right child corresponding to their positions.
• A node can have only a left child or only a right child or it can have no children at all.
• Left child is always less that its parent, while right child is greater than its parent in a binary search tree
Representing Tree in Java
• Similar to Linked List with 2 Links– Store the nodes at unrelated locations in memory
and connect them using references in each node that point to its children.
• Can also be represented as an array, with nodes in specific positions stored in corresponding positions in the array.
24
Binary Tree Nodes
Abs tra ct T re e M o de l
A
E F
H
D
CB
G
l e ft A ri gh t
T re e N ode M ode l
l e ft B ri gh t
l e ft E ri gh t
l e ft G ri gh t
l e ft D ri gh t
l e ft C ri gh t
l e ft H ri gh t
l e ft F ri gh t
Array implementation
10-26
Tree Traversals• A traversal of a tree requires that each
node of the tree be visited once• Example: a typical reason to traverse a tree is
to display the data stored at each node of the tree
• Standard traversal orderings:• preorder• inorder• postorder• level-order
10-27
Traversals
A
IH
D E
B
F
C
G
We’ll trace the different traversals using this tree; recursive calls, returns, and “visits” will be numbered in the order they occur
10-28
Preorder Traversal• Start at the root• Visit each node, followed by its children; we will
choose to visit left child before right
• Recursive algorithm for preorder traversal:• If tree is not empty,
• Visit root node of tree• Perform preorder traversal of its left subtree• Perform preorder traversal of its right subtree
• What is the base case?• What is the recursive part?
10-29
Preorder Traversal
A
IH
D E
B
F
C
G
Nodes are visited in the order ABDHECFIG
10-30
Inorder Traversal• Start at the root• Visit the left child of each node, then the node, then
any remaining nodes
• Recursive algorithm for inorder traversal• If tree is not empty,
• Perform inorder traversal of left subtree of root• Visit root node of tree• Perform inorder traversal of its right subtree
10-31
Inorder Traversal
A
IH
D E
B
F
C
G
Inorder: Nodes are visited in the order DHBEAIFCG
10-32
Postorder Traversal• Start at the root• Visit the children of each node, then the node
• Recursive algorithm for postorder traversal• If tree is not empty,
• Perform postorder traversal of left subtree of root• Perform postorder traversal of right subtree of root• Visit root node of tree
10-33
Postorder Traversals
A
IH
D E
B
F
C
G
Postorder: Nodes are visited in the order HDEBIFGCA
10-34
Discussion
• Note that the relative order of the recursive calls in preorder, inorder and postorder traversals is the same
• The only differences stem from where the visiting of the root node of a subtree actually takes place
10-35
Level Order Traversal• Start at the root• Visit the nodes at each level, from left to
right
• Is there a recursive algorithm for a level order traversal?
10-36
Level Order Traversal
A
IH
D E
B
F
C
G
Level Order: Nodes will be visited in the order ABCDEFGHI
Finding a Node• To find a node given its key value, start from the
root. • If the key value is same as the node, then node is
found.• If key is greater than node, search the right subtree,
else search the left subtree.• Continue till the node is found or the entire tree is
traversed.• Time required to find a node depends on how many
levels down it is situated, i.e. O(log N).
Inserting a Node
• To insert a node we must first find the place to insert it.
• Follow the path from the root to the appropriate node, which will be the parent of the new node.
• When this parent is found, the new node is connected as its left or right child, depending on whether the new node’s key is less or greater than that of the parent.
• What is the complexity?
Finding Maximum and Minimum Values
• For the minimum, – go to the left child of the root and keep going to
the left child until you come to a leaf node. This node is the minimum.
• For the maximum, – go to the right child of the root and keep going to
the right child until you come to a leaf node. This node is the maximum.
Deleting a Node
• Start by finding the node you want to delete.• Then there are three cases to consider:
1. The node to be deleted is a leaf2. The node to be deleted has one child3. The node to be deleted has two children
Deletion cases: Leaf Node
• To delete a leaf node, simply change the appropriate child field in the node’s parent to point to null, instead of to the node.
• The node still exists, but is no longer a part of the tree.
• Because of garbage collection feature of most programming languages, the node need not be deleted explicitly.
Deletion: One Child
• The node to be deleted in this case has only two connections: to its parent and to its only child.
• Connect the child of the node to the node’s parent, thus cutting off the connection between the node and its child, and between the node and its parent.
Deletion: Two Children
Deletion: Two Children• To delete a node with two children, replace the node with its
inorder successor.• For each node, the node with the next-highest key (to the
deleted node) in the subtree is called its inorder successor.• To find the successor,
– start with the original (deleted) node’s right child. – Then go to this node’s left child and then to its left child and so on,
following down the path of left children. – The last left child in this path is the successor of the original node.
Find successor
Find successor
Delete a node with subtree (case 1)
Successor is a leaf node
Delete a node with subtree (case 2)
Deletion when the successor is the right child.
Delete a node with subtree (case 3)
Delete a node with subtree (case 1)
• If the right child of the original node has no left child, this right child is itself the successor.
• The successor can be the right child or it can be one of this right child’s descendants.
• If the node to be deleted is the root, set the root to the successor.
• Else the node can be either a right child or a left child. In this case set the appropriate field in its parent to point to the successor.
• After this set the left child of the successor to point to the node’s left child.
• If successor is a left descendent of the right child of the node to be deleted, perform the following steps:
-- Plug the right child of the successor into the left child of the successor’s parent.
-- Plug the right child of the node to be deleted into the right child of the successor.
-- Unplug the node from the right child of its parent and set this field to point to the successor.
-- Unplug the node’s left child and plug it into the left child of the successor.
Efficiency
• Assume number of nodes N and number of levels L.• N = 2L -1• N+1 = 2L • L = log(N+1)• The time needed to carry out the common tree
operations is proportional to the base 2 log of N• O(log N) time is required for these operations.
Unbalanced Trees• Some trees can be unbalanced.• They have most of their nodes on one side of the
root or the other. Individual subtrees may also be unbalanced.
• Trees become unbalanced because of the order in which the data items are inserted.
• If the key values are inserted in ascending or descending order the tree will be unbalanced.
Unbalanced Trees
Items inserted in ascending order
If you insert a series of nodes whose keys are in either ascending or descending order, the result will be something like that below.
Unbalanced Trees• The nodes arrange themselves in a line with no
branches. Because each node is larger than the previously inserted one, every node is a right child, so all the nodes are on one side of the root.
• The tree is maximally unbalanced. • If you inserted items in descending order, every node
would be the left child of its parent, and the tree would be unbalanced on the other side.
Unbalanced Trees• When there are no branches, the tree becomes, in effect, a
linked list. The arrangement of data is one-dimensional instead of two-dimensional.
• Unfortunately, as with a linked list, you must now search through (on the average) half the items to find the one you’re looking for.
• In this situation, the speed of searching is reduced to O(N), instead of O(logN) as it is for a balanced tree.
10-57
Using Binary Trees: Expression Trees
• Programs that manipulate or evaluate arithmetic expressions can use binary trees to hold the expressions
• An expression tree represents an arithmetic expression such as(5 – 3) * 4 + 9 / 2• Root node and interior nodes contain operations• Leaf nodes contain operands
10-58
Example: An Expression Tree
/
-
35
+
(5 – 3) * 4 + 9 / 2
4
*
9 2
10-59
Evaluating Expression Trees
• We can use an expression tree to evaluate an expression• We start the evaluation at the bottom left• What kind of traversal is this?
10-60
Evaluating an Expression Tree
-
57 8/
29
* This tree represents the expression
(9 / 2 + 7) * (8 – 5)
Evaluation is based on postorder traversal:
If root node is a leaf, return the associated value.
Recursively evaluate expression in left subtree.
Recursively evaluate expression in right subtree.
Perform operation in root node on these two values, and return result.
+