倾斜二叉树
倾斜二叉树是一种二叉树,其中所有节点都只有一个孩子或没有孩子。
倾斜二叉树的类型
有两种特殊类型的倾斜树:
1. 左偏二叉树:
这些是那些所有节点都有左孩子或根本没有孩子的倾斜二叉树。它是一棵左侧占主导地位的树。所有正确的孩子都保持为空。
下面是一个左倾斜树的例子:
C++
#include
using namespace std;
// A Tree node
struct Node {
int key;
struct Node *left, *right;
};
// Utility function to create a new node
Node* newNode(int key)
{
Node* temp = new Node;
temp->key = key;
temp->left = temp->right = NULL;
return (temp);
}
// Driver code
int main()
{
/*
1
/
2
/
3
*/
Node* root = newNode(1);
root->left = newNode(2);
root->left->left = newNode(3);
return 0;
}
Java
// Java implementation of above approach
import java.util.*;
class GFG
{
// A Tree node
static class Node
{
int key;
Node left, right;
};
// Utility function to create a new node
static Node newNode(int key)
{
Node temp = new Node();
temp.key = key;
temp.left = temp.right = null;
return (temp);
}
// Driver code
public static void main(String args[])
{
/*
1
/
2
/
3
*/
Node root = newNode(1);
root.left = newNode(2);
root.left.left = newNode(3);
}
}
// This code is contributed by Arnab Kundu
Python3
# Python3 implementation of the above approach
# Class that represents an individual
# node in a Binary Tree
class Node:
def __init__(self, key):
self.left = None
self.right = None
self.val = key
# Driver code
""" 1
/
2
/
3 """
root = Node(1)
root.left = Node(2)
root.left.left = Node(2)
# This code is contributed by dhruvsantoshwar
C#
// C# implementation of above approach
using System;
class GFG
{
// A Tree node
public class Node
{
public int key;
public Node left, right;
};
// Utility function to create a new node
static Node newNode(int key)
{
Node temp = new Node();
temp.key = key;
temp.left = temp.right = null;
return (temp);
}
// Driver code
public static void Main()
{
/*
1
/
2
/
3
*/
Node root = newNode(1);
root.left = newNode(2);
root.left.left = newNode(3);
}
}
// This code is contributed by AnkitRai01
Javascript
C++
#include
using namespace std;
// A Tree node
struct Node {
int key;
struct Node *left, *right;
};
// Utility function to create a new node
Node* newNode(int key)
{
Node* temp = new Node;
temp->key = key;
temp->left = temp->right = NULL;
return (temp);
}
// Driver code
int main()
{
/*
1
\
2
\
3
*/
Node* root = newNode(1);
root->right = newNode(2);
root->right->right = newNode(3);
return 0;
}
Java
// Java implementation of above approach
import java.util.*;
class GFG
{
// A Tree node
static class Node
{
int key;
Node left, right;
};
// Utility function to create a new node
static Node newNode(int key)
{
Node temp = new Node();
temp.key = key;
temp.left = temp.right = null;
return (temp);
}
// Driver code
public static void main(String args[])
{
/*
1
\
2
\
3
*/
Node root = newNode(1);
root.right = newNode(2);
root.right.right = newNode(3);
}
}
// This code is contributed by Arnab Kundu
Python3
# Python3 implementation of the above approach
# A Tree node
class Node:
def __init__(self, key):
self.left = None
self.right = None
self.val = key
# Driver code
"""
1
\
2
\
3
"""
root = Node(1)
root.right = Node(2)
root.right.right = Node(3)
# This code is contributed by shivanisinghss2110
C#
// C# implementation of above approach
using System;
class GFG
{
// A Tree node
public class Node
{
public int key;
public Node left, right;
};
// Utility function to create a new node
static Node newNode(int key)
{
Node temp = new Node();
temp.key = key;
temp.left = temp.right = null;
return (temp);
}
// Driver code
public static void Main(String []args)
{
/*
1
\
2
\
3
*/
Node root = newNode(1);
root.right = newNode(2);
root.right.right = newNode(3);
}
}
// This code is contributed by PrinciRaj1992
Javascript
2.右斜二叉树:
这些是那些所有节点都具有右孩子或根本没有孩子的倾斜二叉树。它是一棵右侧占主导地位的树。所有剩下的孩子都保持为空。
以下是右倾斜树的示例:
C++
#include
using namespace std;
// A Tree node
struct Node {
int key;
struct Node *left, *right;
};
// Utility function to create a new node
Node* newNode(int key)
{
Node* temp = new Node;
temp->key = key;
temp->left = temp->right = NULL;
return (temp);
}
// Driver code
int main()
{
/*
1
\
2
\
3
*/
Node* root = newNode(1);
root->right = newNode(2);
root->right->right = newNode(3);
return 0;
}
Java
// Java implementation of above approach
import java.util.*;
class GFG
{
// A Tree node
static class Node
{
int key;
Node left, right;
};
// Utility function to create a new node
static Node newNode(int key)
{
Node temp = new Node();
temp.key = key;
temp.left = temp.right = null;
return (temp);
}
// Driver code
public static void main(String args[])
{
/*
1
\
2
\
3
*/
Node root = newNode(1);
root.right = newNode(2);
root.right.right = newNode(3);
}
}
// This code is contributed by Arnab Kundu
Python3
# Python3 implementation of the above approach
# A Tree node
class Node:
def __init__(self, key):
self.left = None
self.right = None
self.val = key
# Driver code
"""
1
\
2
\
3
"""
root = Node(1)
root.right = Node(2)
root.right.right = Node(3)
# This code is contributed by shivanisinghss2110
C#
// C# implementation of above approach
using System;
class GFG
{
// A Tree node
public class Node
{
public int key;
public Node left, right;
};
// Utility function to create a new node
static Node newNode(int key)
{
Node temp = new Node();
temp.key = key;
temp.left = temp.right = null;
return (temp);
}
// Driver code
public static void Main(String []args)
{
/*
1
\
2
\
3
*/
Node root = newNode(1);
root.right = newNode(2);
root.right.right = newNode(3);
}
}
// This code is contributed by PrinciRaj1992
Javascript