Download presentation
Presentation is loading. Please wait.
Published byNeil Franklin Modified over 8 years ago
2
Tree Representation and Terminology Binary Trees Binary Search Trees Pointer-Based Representation of a Binary Tree Array-Based Representation of a Binary Tree
3
A tree is a collection of nodes and directed edges, satisfying the following properties: –There is one specially designated node called the root, which has no edges pointing to it. –Every node except the root has exactly one edge pointing to it. –There is a unique path (of nodes and edges) from the root to each node.
4
Trees, as defined on the preceding slide, are typically drawn with circles (or rectangles) representing the nodes and arrows representing the edges. The root is typically placed at the top of the diagram, with the rest of the tree below it. Trees : Not Trees : root edge node Leaf nodes
5
If an edge goes from node a to node b, then a is called the parent of b, and b is called a child of a. Children of the same parent are called siblings. If there is a path from a to b, then a is called an ancestor of b, and b is called a descendent of a. A node with all of its descendants is called a subtree. If a node has no children, then it is called a leaf of the tree. If a node has no parent (there will be exactly one of these), then it is the root of the tree.
6
A BC DE F GH I JK A is the root D, E, G, H, J & K are leaves B is the parent of D, E & F D, E & F are siblings and children of B I, J & K are descendants of B A & B are ancestors of I subtree
7
Intuitively, a binary tree is LIKE a tree in which each node has no more than two children. Formally, a binary tree is a set T of nodes such that either: – T is empty, or –T consists of a single node, r, called the root, and two (non- overlapping) binary trees, called the left and right subtrees of r. UNLIKE trees, binary trees may be empty, and they distinguish between left and right subtrees. (These two binary trees are distinct.)
8
Data Left_child Right_child Data null Data null Data null Data null Data Left_child Right_child Data Left_child Right_child Root
9
A binary search tree is a binary tree in which each node, n, has a value satisfying the following properties: –n’s value is > all values in its left subtree, T Left, –n’s value is < all values in its right subtree, T Right, and –T Left and T Right are both binary search trees. John Peter Brenda AmyMaryTom 21 2 3 55 34 13 5 8
10
Intuitively, the level of a node is the number of nodes on a path from the root to the node. Formally, level of node, n: – If n is the root of a tree, then it is at level 1. – Otherwise, its level is 1 greater than the level of its parent. Height of binary tree, T: – If T is empty, its height is 0. – Otherwise, its height is the maximum level of its nodes, or, equivalently, 1 greater than the height of the root’s taller subtree. Namely, height(T) = 1 + max { height( T Left ), height(T Right ) }
11
Intuitively, a binary tree is full if it has no missing nodes. Formally, a binary tree of height h is full if – It is empty (h = 0). – Otherwise, the root’s subtrees are full binary trees of height h – 1. If not empty, each node has 2 children, except the nodes at level h which have no children. Alternatively, the binary tree has all of its leaves at level h.
12
Intuitively, a binary tree of height h is complete if it is full down to level h – 1, and level h is filled from left to right. Formally, a binary tree of height h is complete if – All nodes at level h – 2 and above have 2 children each, – If a node at level h – 1 has children, all nodes to its left at the same level have 2 children each, and – If a node at level h – 1 has 1 child, it is a left child.
13
A binary tree is balanced if the difference in height between any node’s left and right subtree is 1. Note that: –A full binary tree is also complete. –A complete binary tree is not always full. –Full and complete binary trees are also balanced. –Balanced binary trees are not always full or complete.
14
Different operations on a Tree are as follows – Insertion – Deletion – Searching – Traversal Pre-Order In-Order Post- Order Please follow the Power Point Presentations on Insertion, Deletion and Traversal using Binary Search Tree.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.