Upload
rajendranbehappy
View
3
Download
0
Embed Size (px)
DESCRIPTION
Tree Properties
Citation preview
jrfTypewritten textBinary Tree-Basic terminology
Outline
Binary Tree ADT
Tree ADT
What is a Tree?
In computer science, a tree is an abstract model of a hierarchical structure
A tree consists of nodes with a parent-
child relation
Applications:
Organization
charts
File systems
Computers R Us
Sales R&DManufacturing
Laptops DesktopsUS International
Europe Asia Canada
Tree Terminology
Root: node without a parent (A)
Internal node: node with at least one child (A, B, C, F)
External node (a.k.a. leaf ): node without children (E, I, J, K, G, H, D)
Parent node: node immediately above a given node (parent of C is A)
Child node: node(s) immediately below a given node (children of C are G and H)
Ancestors of a node: parent, grandparent, grand-grandparent, etc. (ancestors of G are C, A)
Depth of a node: number of ancestors (I has depth 3)
Height of a tree: maximum depth of any node (tree with just a root has height 0, this tree has height 3)
Descendant of a node: child, grandchild, grand-grandchild, etc.
Subtree: tree consisting of a node and its descendants
A
B DC
G HE F
I J K
subtree
Tree ADT (Abstract Data Structure)
Tree methods:
int size(): returns the number of nodes
boolean isEmpty(): returns true if the tree is empty
Node root(): returns the root of the tree
Node methods:
Node parent(): returns the parent of the node
Node[ ] children(): returns the children of the node
boolean isInternal(): returns true if the node has children
boolean isExternal(): returns true if the node is a leaf
boolean isRoot(): returns true if the node is the root
Binary Tree
In CS16, well be looking mainly at binary trees
A binary tree is a tree with the following property: Each internal node has at most 2
children: a left child and a right child. Even if there is only one child, the child is still specified as left or right.
Recursive definition: A tree consisting of a single node, or
A tree whose root has at most 2 children, each of which is a binary tree
A
B C
F GD E
H I
Binary Tree ADT
In addition to the methods specified in the Tree ADT, binary
tree Nodes have a few additional methods:
Node left(): returns the left child of the node if there is
one, otherwise null
Node right(): returns the right child of the node if there is
one, otherwise null
boolean hasLeft(): returns true if node has a left child
boolean hasRight(): returns true if node has a right child
Perfection
A binary tree is a perfect binary tree if
every level is completely full
Not perfect Perfect!
Completeness
A binary tree is a left-complete binary tree if:
every level is completely full, possibly excluding the
lowest level
all nodes are as far left as possible
Left-complete! Not left-complete