Upload
marl-mwegi
View
3
Download
0
Embed Size (px)
DESCRIPTION
Nice
Citation preview
INTRODUCTION TO TREESTree Properties and Definitions In computer science, a tree is an abstract model of a hierarchical structure Each node in a tree has exactly one parent except for the root node, which has no parent A generalization of the parent-child relationship is the ancestor-descendent relationship A tree consists of
o a finite set of elements, called nodes, and o a finite set of directed lines, called branches, that connect the nodes.
Trees Composed of nodes and edges Node
o Any distinguishable object Edge
o An ordered pair <u,v> of nodeso Illustrated by an arrow with tail at u and head at vo u = tail of <u,v>o v = head of <u,v>
Root o A distinguished node, depicted at the top of the tree
Applications:o Organization chartso File systemso Programming environments
Recursive Definition of a Tree1) A single node, with no edges, is a tree. The root of the tree is its unique node.2) Let T1, …, Tk (k >=1) be trees with no nodes in common, and let r1, …,rk be the roots of those trees,
respectively. Let r be a new node. Then there is a tree T consisting of the nodes and edges of T1, …, Tk , the new node r and the edges <r,r1>,…,<r,rk>. The root of T is r and T1, …,Tk are called the subtrees of T.
r is called the parent of r1, …, rk
r1, …, rk are the children of r r1, …, rk are the siblings of one another Node v is a descendant of u if
o u = v oro v is a descendant of a child of uo A path exists from u to v.
Node Degree Degree of a node
o The number of branches associated with a node. Indegree
The branch is directed toward the node. Outdegree
The branch is directed away from the node.o Degree = indegree + outdegree
A path from u to v You can get from u to v by following a sequences of edges down the tree, starting with an edge whose
tail is u, ending with an edge whose head is v, and with the head of each edge but the last being the tail of the next.
1
Every node is a descendant of itself. If v is a descendant of u, then u is an ancestor of v. Proper descendants:
o The descendants of a node, other than the node itself. Proper ancestors
o The ancestors of a node, other than the node itself.
Tree Terminology Root:
o Node without parent (A) Internal node:
o node with at least one child (A, B, C, F)o A node that is not a root or a leaf.
Leaf (aka External node): o Node without children (E, I, J, K, G, H, D)
Ancestors of a node: o Parent, grandparent, great-grandparent, etc.
Depth of a node: o Number of ancestorso Length of the path from the root of the tree to the node
Levelo The level of a node is its distance from the root.
Height (Depth)o The height of a tree is the level of the leaf in the longest path from the root plus 1.o maximum depth of any node o Length of the longest path from that node to a leaf
Descendant of a node: o Child, grandchild, great-grandchild, etc.
Subtree: o Tree consisting of a node and its descendants
Num_nodes = Num_edges + 1o Every tree has exactly one more node than edgeo Every edge, except the root, is the head of exactly one of the edges
Siblingo nodes with same parent
A
B DC
G HE F
I J K
2
Example:
Exercise: Trees Answer the following questions about the tree shown above:
o What is the size of the tree (number of nodes)?o Classify each node of the tree as a root, leaf, or internal nodeo List the ancestors of nodes B, F, G, and A. Which are the parents?o List the descendents of nodes B, F, G, and A. Which are the children?o List the depths of nodes B, F, G, and A.o What is the height of the tree?o Draw the subtrees that are rooted at node F and at node K.
Special kinds of trees1) Ordered v. unordered2) Binary tree3) Empty v non-empty4) Full 5) Perfect 6) Complete
1) Ordered trees Have a linear order on the children of each node That is, can clearly identify a 1st, 2nd, …, kth child An unordered tree doesn’t have this property
2) Binary tree An ordered tree with at most two children for each node If node has two child, the 1st is called the left child, the 2nd is called the right child If only one child, it is either the right child or the left child
3) Empty binary tree
3
Convenient to define an empty binary tree, written L, for use in this definition:o A binary tree is either L or is a node with left and right subtrees, each of which is a binary tree.
4) Full binary tree Has no nodes with only one child
o Each node either a leaf or it has 2 children # leaves = #non-leaves + 1
5) Perfect binary tree A full binary tree in which all leaves have the same depth Perfect binary tree of height h has:
o 2h+1 -1 nodeso 2h leaveso 2h -1 non-leaves
Interesting because it is “fully packed”, the shallowest tree that can contain that many nodes. Often, we’ll be searching from the root. Such a shallow tree gives us minimal number of accesses to nodes in the tree.
6) Complete binary tree Perfect binary tree requires that number of nodes be one less than a power of 2. Complete binary tree is a close approximation, with similar good properties for processing of data
stored in such a tree. Complete binary tree of height h:
o Perfect binary tree of height h-1o Add nodes from left at bottom level
More formally, o A complete binary tree of height 0 is any tree consisting of a single node.o A complete binary tree of height 1 is a tree of height 1 with either two children or a left child
only.o For height h >= 2, a complete binary tree of height h is a root with two subtrees satisfying one
of these two conditions:o Either the left subtree is perfect of height h-1 and the right is complete of height h-1 ORo The left is complete of height h-1 and the right is perfect of height h-2.
4
Forest A finite set of trees In the case of ordered trees, the trees in the forest must have a distinguishable order as well.Tree Operations Parent(v)
o Return the parent of node v, or L if v is the root Children(v)
o Return the set of children of node v (the empty set, if v is a leaf). FirstChild(v)
o Return the first child of node v, or L if v is a leaf. RightSibling(v)
o Return the right sibling of v, or L if v is the root or the rightmost child of its parent. LeftSibling(v)
o Return the left sibling of v, or L if v is the root or the leftmost child of its parent LeftChild(v)
o Return the left child of node v;o Return L if v has no left or right child.
RightChild(v)o Return the right child of node v; o return L if v has no right child
isLeaf(v)o Return true if node v is a leaf, false if v has a child
Depth(v)o Return the depth of node v in the tree
Height(v)o Return the height of node v in the tree.
Binary Trees A binary tree is a tree with the following properties:
o Each internal node has two childreno The children of a node are an ordered pair
We call the children of an internal node left child and right child Alternative recursive definition: a binary tree is either
o a tree consisting of a single node, oro a tree whose root has an ordered pair of children, each of which is a binary tree
Each node contains:o A value (some sort of data item)o A reference or pointer to a left child (may be null), ando A reference or pointer to a right child (may be null
Applications:o arithmetic expressionso decision processeso searching
Balanceo A binary tree is balanced if every level above the lowest is “full” (contains 2n nodes)o In most applications, a reasonably balanced binary tree is desirable
Sorted binary treeso A binary tree is sorted if every node in the tree is larger than (or equal to) its left descendants,
and smaller than (or equal to) its right descendantso Equal nodes can go either on the left or the right (but it has to be consistent)
5
Arithmetic Expression Tree Binary tree associated with an arithmetic expression
o internal nodes: operatorso leaves: operands
Leaves are operands (constants or variables) The other nodes (internal nodes) contain operators Will not be a binary tree if some operators are not binary Example: arithmetic expression tree for the infix expression (2 ´ (a - 1) + (3 ´ b))
Note: infix expressions require parentheses ((2 x a) – (1+3) * b) is a different expression tree
Decision Tree Binary tree associated with a decision process
o internal nodes: questions with yes/no answero leaves: decisions
Example: Dining decision
A
B C
F GD E
H I
10
815
412
20
17
´´
2
a 1
3 b
6
Binary Tree ADT The BinaryTree ADT extends the Tree ADT, i.e., it inherits all the methods of the Tree ADT Additional methods:
o position leftChild(p)o position rightChild(p)o position sibling(p)
Update methods may be defined by data structures implementing the BinaryTree ADT
Binary Tree Representations If a complete binary tree with n nodes (depth = log n + 1) is represented sequentially, then for
any node with index i, 1<=i<=n, we have:o parent(i) is at i/2 if i!=1. If i=1, i is at the root and has no parent.o left_child(i) ia at 2i if 2i<=n. If 2i>n, then i has no left child.o right_child(i) ia at 2i+1 if 2i +1 <=n. If 2i +1 >n, then i has no right child.
Array Representation of Binary Tree The binary tree is represented in an array by storing each element at the array position corresponding
to the number assigned to it.
Linked Representation The most popular way to present a binary tree Each element is represented by a node that has two link fields (leftChild and rightChild) plus an
element field Each binary tree node is represented as an object whose data type is binaryTreeNode
typedef struct node *tree_pointer;typedef struct node {
Want a fast meal?
How about coffee?
On expense account?
Starbucks
Spike’s
Al Forno
Café Paragon
Yes
No
Yes
No
Yes
No
7
int data; tree_pointer left_child, right_child;};
Tree traversal Used to print out the data in a tree in a certain order A binary tree is defined recursively: it consists of a root, a left subtree, and a right subtree To traverse (or walk) the binary tree is to visit each node in the binary tree exactly once Tree traversals are naturally recursive Two approaches
o Depth first o Breadth first
Depth firsto We process all of the descendents of a child before going on the next child.
Breadth firsto Each level is completely processed before the next level is started.
Let L, V, and R stand for moving left, visiting the node, and moving right. There are six possible combinations of traversal
o LVR, LRV, VLR, VRL, RVL, RLV Adopt convention that we traverse left before right, only 3 traversals remain LVR, LRV, VLR inorder, postorder, preorder Depth-first traversals
data
left_child right_child
data
left_child right_child
8
Preorder traversal In preorder, the root is visited first Recursively print out all data in the left subtree Recursively print out all data in the right subtree Here’s a preorder traversal to print out all the elements in the binary tree:
public void preorderPrint(BinaryTree bt) { if (bt == null) return; System.out.println(bt.value); preorderPrint(bt.leftChild); preorderPrint(bt.rightChild);}
Inorder traversal In inorder, the root is visited in the middle Here’s an inorder traversal to print out all the elements in the binary tree:
public void inorderPrint(BinaryTree bt) { if (bt == null) return; inorderPrint(bt.leftChild); System.out.println(bt.value); inorderPrint(bt.rightChild);}
Postorder traversal In postorder, the root is visited last Here’s a postorder traversal to print out all the elements in the binary tree:
public void postorderPrint(BinaryTree bt) { if (bt == null) return; postorderPrint(bt.leftChild); postorderPrint(bt.rightChild); System.out.println(bt.value);}
Level order The tree is processed by levels. So first all nodes on level i are processed from left to right before the
first node of level i+1 is visited
Expression trees traversals Infix traversal Postfix traversal Prefix traversal
Example 1: Expression Trees
9
Preorder traversal Node is considered before its children have been considered Root, left, right Prefix expression
o ++a*bc*+*defg Nice feature: unambigous, don’t need parentheses
Postorder traversal Node is considered after its children have been considered Left, right, root Postfix expression
o abc*+de*f+g*+ Nice feature: unambiguous … doesn’t need parentheses
o Often used with scientific calculatorso Simple stack-based evaluation
Inorder traversal Consider left child, consider node, consider right child Left, node, right. Infix expression
o a+b*c+d*e+f*g
The ADT Binary Search Tree A deficiency of the ADT binary tree which is corrected by the ADT binary search tree
o Searching for a particular item Record
o A group of related items, called fields, that are not necessarily of the same data type Field
o A data element within a record A data item in a binary search tree has a specially designated search key
o A search key is the part of a record that identifies it within a collection of records KeyedItem class
o Contains the search key as a data field and a function for accessing the search key Stores keys in the nodes in a way so that searching, insertion and deletion can be done efficiently.
Binary search tree property For every node X, all the keys in its left subtree are smaller than the key value in X, and all the keys in
its right subtree are larger than the key value in X
10
Searching BST If we are searching for 15, then we are done. If we are searching for a key < 15, then we should search in the left subtree. If we are searching for a key > 15, then we should search in the right subtree.
Inorder traversal of BST Print out all the keys in sorted order
11
A binary search tree
Not a binary search tree
Inorder: 2, 3, 4, 6, 7, 9, 13, 15, 17, 18, 20
Binary Tree Construction Suppose that the elements in a binary tree are distinct. Can you construct the binary tree from which a given traversal sequence came? When a traversal sequence has more than one element, the binary tree is not uniquely defined. Therefore, the tree from which the sequence was obtained cannot be reconstructed uniquely.
Searching (Find) Find X: return a pointer to the node that has key X, or NULL if there is no such node
Application of Traversal Sorting a BST Observe the output of the inorder traversal of the BST example. It is sorted As a general rule, if you output the keys (data) of the nodes of a BST using inorder traversal, the data
comes out sorted in increasing order
Binary Search Tree Construction How to build & maintain binary trees?
o Insertiono Deletion
Maintain key property (invariant)o Smaller values in left subtreeo Larger values in right subtree
Binary Search Tree – Insertion Algorithm
1. Perform search for value X2. Search will end at node Y (if X not in tree)3. If X < Y, insert new leaf X as new left subtree for Y4. If X > Y, insert new leaf X as new right subtree for Y
Example Insertion
12
Binary Search Tree – Deletion Algorithm
1. Perform search for value X2. If X is a leaf, delete X3. Else // must delete internal node
a) Replace with largest value Y on left subtree OR smallest value Z on right subtreeb) Delete replacement value (Y or Z) from subtree
Example Deletion (Internal Node)
Insert ( 20 )
5
10
30
2 25 45
10 < 20, right30 > 20, left25 > 20, leftInsert 20 on left
20
Delete ( 25 )
5
10
30
2 25 45
10 < 25, right30 > 25, left25 = 25, delete
5
10
30
2 45
13
Other (Non-Search) Trees Parse trees
o Convert from textual representation to tree representationo Textual program to tree
Used extensively in compilerso Tree representation of data
E.g. HTML data can be represented as a tree called DOM (Document Object Model) tree
XML Like HTML, but used to represent data Tree structured
Graph Data Structures E.g: Airline networks, road networks, electrical circuits Nodes and Edges E.g. representation: class Node
Delete ( 10 ) (Replacing with largest value in left subtree)
5
10
30
2 25 45
5
5
30
2 25 45
2
5
30
2 25 45
Replacing 10 with largest value in left
subtree
Replacing 5 with largest value in left subtree
Deleting leaf
Delete ( 10 ) (Replacing with smallest value in right subtree)
5
10
30
2 25 45
5
25
30
2 25 45
5
25
30
2 45
Replacing 10 with smallest value in right
subtree
Deleting leaf Resulting tree
14
o Stores nameo stores pointers to all adjacent nodes
i,e. edge == pointer To store multiple pointers: use array or linked list
CODEfor each Node:Name
public void preorderTraversal(Visitor v){ if(!isEmpty() && ! v.isDone()){ v.visit(getKey()); getLeft().preorderTraversal(v); getRight().preorderTraversal(v); }}
Visit the node Visit the left subtree, if any. Visit the right subtree, if any.
Preorder(N-L-R)
public void inorderTraversal(Visitor v){ if(!isEmpty() && ! v.isDone()){ getLeft().inorderTraversal(v); v.visit(getKey()); getRight().inorderTraversal(v); }}
Visit the left subtree, if any. Visit the node
Visit the right subtree, if any.
Inorder(L-N-R)
public void postorderTraversal(Visitor v){ if(!isEmpty() && ! v.isDone()){ getLeft().postorderTraversal(v) ; getRight().postorderTraversal(v); v.visit(getKey()); }}
Visit the left subtree, if any. Visit the right subtree, if any. Visit the node
Postorder(L-R-N)
15