Вы находитесь на странице: 1из 9

Binary Tree-Basic terminology

Outline
Tree ADT
Binary 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 parentchild relation
Applications:
Organization
charts
File systems

Computers R Us

Sales

US

Europe

Manufacturing

International

Asia

Laptops

Canada

Desktops

R&D

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, grandgrandparent, 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, grandgrandchild, etc.
Subtree: tree consisting of a node and its
descendants

subtree
I

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

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

Вам также может понравиться