title | date | tags | categories | |
---|---|---|---|---|
BinaryTree基础知识 |
2017-08-17 04:41:50 -0700 |
|
编程基础 |
BinaryTree基础知识 关键点:
每个结点最多有两个子树的有序树,最大度数为2
class Node
{
int key;
Node left, right;
public Node(int item)
{
key = item;
left = right = null;
}
}
class BinaryTree
{
// Root of Binary Tree
Node root;
// Constructors
BinaryTree(int key)
{
root = new Node(key);
}
BinaryTree()
{
root = null;
}
}
A Binary Tree is full if every node has 0 or 2 children;
18
/ \
15 20
/ \
40 50
/ \
30 50
A Binary Tree is complete Binary Tree if all levels are completely filled except possibly the last level and the last level has all keys as left as possible.
18
/ \
15 30
/ \ / \
40 50 100 40
/ \ /
8 7 9
Practical example of Complete Binary Tree is Binary Heap.
A Binary tree is Perfect Binary Tree in which all internal nodes have two children and all leaves are at same level.
18
/ \
15 30
/ \ / \
40 50 100 40
二叉树是一种数据结构,树有左右2个孩子,而二叉搜索树是一种特殊的二叉树
- 左孩子节点比父节点小
- 右孩子节点比父节点大
2
/ \
1 3