Download presentation
Presentation is loading. Please wait.
1
CHAPTER 4 Trees
2
Trees A tree is a nonlinear, hierarchical structure in which data are organized so that items of information are related by branches A finite set of one or more nodes such that There is a specially designated node called the root. The remaining nodes are partitioned into n>=0 disjoint sets T1,...,Tn, where each of these sets is a tree T1,...,Tn are called the subtrees of the root; connected by a directed edge to the root note: this is a recursive definition The condition that T1,...,Tn be disjoint subsets prohibits subtrees from ever being connected
3
General characteristics of trees
Node Degree Children Parent Siblings Ancestors Leaves Path from node n1 to nk a sequence of nodes n1,n2,...,nk such that ni is the parent of ni+1 for 1<= i < k Path length Level Height of a node Height of a tree Depth of a node Depth of a tree Along with arrays and lists, trees are the most frequently encountered data structure in computer algorithms
4
General characteristics of trees
Node - item of information plus the branches to other nodes. Degree - number of subtrees of a node nodes that have degree zero are leaf or terminal nodes; other nodes are nonterminals degree of a tree is the maximum of the degree of the nodes in the tree. (tree above is degree 2) Roots of subtrees of a node X are the children of X X is parent of its children Children of the same parent are siblings Ancestors of a node are all the nodes along the path from the root to that node. Nodes with no children are called leaves Path from node n1 to nk is a sequence of nodes n1,n2,...,nk such that ni is parent of ni+1 for 1<= i < k Path length: number of edges in the path(k-1)
5
General Characteristics of Trees
Exactly one path from root to each node, i.e. no cycles Level of a node is defined by letting root be at level 0. If node is at level i, then its children are at level i +1 Height of a node u is the height of the subtree rooted at u Height of tree is height of the root Depth of any node is length of unique path from the root to that node (root is depth of 0) Depth of tree is equal to depth of deepest leaf (i.e. always the height of the tree)
6
Parse Trees: An example application
Used to represent a grammar in computer languages A typical C++ grammar: <statement> ::= <select-statement> | <expr> <select-statement> ::= if (<expr>) <statement> else <statement> <expr> ::= <relational-expr> | <assign-expr> | identifier <relational-expr> ::= <expr> < <expr> <assign-expr> ::= <expr> = <expr> The C++ statement: if (a<b) max = b else max = a is analyzed by constructing a tree leaf nodes = tokens (symbols) interior nodes = syntactic categories
7
Parse Trees: An Example Application
Compiler constructs parse tree Traversing the tree generates code
8
Left Child-Right Sibling Representation
Each node contains Data firstchild next_sibling Order of children in the tree is not important Usually nodes are chosen based on how the tree is drawn
9
Application Level 1 nodes represent types of vehicles
B = sports vehicle C = family van D = 4-door sedan Level 2 nodes represent colors for each vehicle type B comes in two colors C comes in basic black D comes in three colors
10
Left Child-Right Sibling Tree
Degree-Two Tree Rotate the right-sibling pointers in a left child-right sibling tree clockwise by 45 degrees: The two children of a node are called left and right children Right child of root node is always empty Left child-right child trees are also known as binary trees.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.