数据结构 动态查找与二叉排序树

简介: 数据结构 动态查找与二叉排序树

1. DS二叉排序树之创建和插入


题目描述


给出一个数据序列,建立二叉排序树,并实现插入功能


对二叉排序树进行中序遍历,可以得到有序的数据序列


输入


第一行输入t,表示有t个数据序列


第二行输入n,表示首个序列包含n个数据


第三行输入n个数据,都是自然数且互不相同,数据之间用空格隔开


第四行输入m,表示要插入m个数据


从第五行起,输入m行,每行一个要插入的数据,都是自然数且和前面的数据不等


以此类推输入下一个示例


输出


第一行输出有序的数据序列,对二叉排序树进行中序遍历可以得到


从第二行起,输出插入第m个数据后的有序序列,输出m行


以此类推输出下一个示例的结果


输入样例


1

6

22 33 55 66 11 44

3

77

50

10


输出样例


11 22 33 44 55 66

11 22 33 44 55 66 77

11 22 33 44 50 55 66 77

10 11 22 33 44 50 55 66 77


参考代码

#include<iostream>
using namespace std;
struct Node
{
  int data;
  struct Node* leftTree;
  struct Node* rightTree;
};
class BSTree
{
private:
  Node* head = new Node();
  Node* p = new Node();
public:
  BSTree()
  {
    head->data = -1;
    head->leftTree = head->rightTree = NULL;
  }
  void Create();
  void Insert(Node* p,int e);
  void InOrderTraverse(Node* p);
};
void BSTree::Create()
{
  int t;
  int n,e;
  cin >> t;
  while (t--)
  {
    cin >> n;
    while (n--)
    {
      cin >> e;
      if (head->data == -1)head->data = e;
      else
      {
        Insert(head,e);
      }
    }
    InOrderTraverse(head);
    cout << endl;
    cin >> n;
    while (n--)
    {
      cin >> e;
      Insert(head, e);
      InOrderTraverse(head);
      cout << endl;
    }
  }
}
void BSTree::Insert(Node *p,int e)
{
  if (e > p->data&&p->rightTree==NULL) 
  {
    Node* s = new Node();
    s->data = e;
    s->leftTree = s->rightTree = NULL;
    p->rightTree = s;
  }
  else if (e < p->data && p->leftTree == NULL)
  {
    Node* s = new Node();
    s->data = e;
    s->leftTree = s->rightTree = NULL;
    p->leftTree = s;
  }
  else if (e > p->data && p->rightTree != NULL)
  {
    Insert(p->rightTree, e);
  }
  else if (e < p->data && p->leftTree != NULL)
  {
    Insert(p->leftTree, e);
  }
}
void BSTree::InOrderTraverse(Node *p)
{
  if(p->leftTree!=NULL)InOrderTraverse(p->leftTree);
  cout << p->data << " ";
  if(p->rightTree!=NULL)InOrderTraverse(p->rightTree);
}
int main()
{
  BSTree bst;
  bst.Create();
  return 0;
}


2. DS二叉排序树之查找


题目描述


给出一个数据序列,建立二叉排序树,并实现查找功能


对二叉排序树进行中序遍历,可以得到有序的数据序列


输入

第一行输入t,表示有t个数据序列


第二行输入n,表示首个序列包含n个数据


第三行输入n个数据,都是自然数且互不相同,数据之间用空格隔开


第四行输入m,表示要查找m个数据


从第五行起,输入m行,每行一个要查找的数据,都是自然数


以此类推输入下一个示例


输出


第一行输出有序的数据序列,对二叉排序树进行中序遍历可以得到


从第二行起,输出查找结果,如果查找成功输出查找次数,如果查找失败输出-1


以此类推输出下一个示例的结果


输入样例


1

6

22 33 55 66 11 44

7

11

22

33

44

55

66

77


输出样例


11 22 33 44 55 66

2

1

2

4

3

4

-1


参考代码

#include<iostream>
using namespace std;
struct Node
{
  int data;
  struct Node* leftTree;
  struct Node* rightTree;
};
class BSTree
{
private:
  Node* head = new Node();
  int count = 0;
  //Node* p = new Node();
public:
  BSTree()
  {
    head->data = -1;
    head->leftTree = head->rightTree = NULL;
  }
  void Create();
  void Insert(Node* p, int e);
  void InOrderTraverse(Node* p);
  int Check(Node *p,int e);
};
void BSTree::Create()
{
  int t;
  int n, e;
  cin >> t;
  while (t--)
  {
    cin >> n;
    while (n--)
    {
      cin >> e;
      if (head->data == -1)head->data = e;
      else
      {
        Insert(head, e);
      }
    }
    InOrderTraverse(head);
    cout << endl;
    cin >> n;
    while (n--)
    {
      cin >> e;
      count = 0;
      int flag=Check(head,e);
      if (flag==1)cout << count << endl;
      else cout <<"-1" <<endl;
    }
  }
}
void BSTree::Insert(Node* p, int e)
{
  if (e > p->data && p->rightTree == NULL)
  {
    Node* s = new Node();
    s->data = e;
    s->leftTree = s->rightTree = NULL;
    p->rightTree = s;
  }
  else if (e < p->data && p->leftTree == NULL)
  {
    Node* s = new Node();
    s->data = e;
    s->leftTree = s->rightTree = NULL;
    p->leftTree = s;
  }
  else if (e > p->data && p->rightTree != NULL)
  {
    Insert(p->rightTree, e);
  }
  else if (e < p->data && p->leftTree != NULL)
  {
    Insert(p->leftTree, e);
  }
}
int BSTree::Check(Node* p,int e)
{
  count++;
  if (e > p->data)
  {
    if (p->rightTree == NULL)return -1;
    Check(p->rightTree, e);
  }
  else if (e == p->data)return 1;
  else if (e < p->data)
  {
    if (p->leftTree == NULL)return -1;
    Check(p->leftTree, e);
  }
}
void BSTree::InOrderTraverse(Node* p)
{
  if (p->leftTree != NULL)InOrderTraverse(p->leftTree);
  cout << p->data << " ";
  if (p->rightTree != NULL)InOrderTraverse(p->rightTree);
}
int main()
{
  BSTree bst;
  bst.Create();
  return 0;
}


3. DS二叉排序树之删除


题目描述


给出一个数据序列,建立二叉排序树,并实现删除功能


对二叉排序树进行中序遍历,可以得到有序的数据序列


输入


第一行输入t,表示有t个数据序列


第二行输入n,表示首个序列包含n个数据


第三行输入n个数据,都是自然数且互不相同,数据之间用空格隔开


第四行输入m,表示要删除m个数据


从第五行起,输入m行,每行一个要删除的数据,都是自然数


以此类推输入下一个示例


输出


第一行输出有序的数据序列,对二叉排序树进行中序遍历可以得到


从第二行起,输出删除第m个数据后的有序序列,输出m行


以此类推输出下一个示例的结果


输入样例


1

6

22 33 55 66 11 44

3

66

22

77


输出样例


11 22 33 44 55 66

11 22 33 44 55

11 33 44 55

11 33 44 55


参考代码

#include<iostream>
using namespace std;
struct Node
{
  int data;
  struct Node* leftTree;
  struct Node* rightTree;
};
class BSTree
{
private:
  Node* head = new Node();
  Node* temp = new Node();
  Node* pre = new Node();
public:
  BSTree()
  {
    head->data = -1;
    head->leftTree = head->rightTree = NULL;
  }
  void Create();
  void Insert(Node* p, int e);
  void InOrderTraverse(Node* p);
  int Check(Node* p,int e);
  int Delete(Node* p, int e);
};
void BSTree::Create()
{
  int t;
  int n, e;
  cin >> t;
  while (t--)
  {
    cin >> n;
    while (n--)
    {
      cin >> e;
      if (head->data == -1)head->data = e;
      else
      {
        Insert(head, e);
      }
    }
    InOrderTraverse(head);
    cout << endl;
    cin >> n;
    while (n--)
    {
      cin >> e;
      int flag = Check(head, e);
      if(flag==1)Delete(temp, e);
      InOrderTraverse(head);
      cout << endl;
    }
  }
}
void BSTree::Insert(Node* p, int e)
{
  if (e > p->data && p->rightTree == NULL)
  {
    Node* s = new Node();
    s->data = e;
    s->leftTree = s->rightTree = NULL;
    p->rightTree = s;
  }
  else if (e < p->data && p->leftTree == NULL)
  {
    Node* s = new Node();
    s->data = e;
    s->leftTree = s->rightTree = NULL;
    p->leftTree = s;
  }
  else if (e > p->data && p->rightTree != NULL)
  {
    Insert(p->rightTree, e);
  }
  else if (e < p->data && p->leftTree != NULL)
  {
    Insert(p->leftTree, e);
  }
}
int BSTree::Check(Node* p, int e)
{
  if (e > p->data)
  {
    if (p->rightTree == NULL)return -1;
    if (p->rightTree->data == e)
    {
      if (p->rightTree->leftTree == NULL && p->rightTree->rightTree == NULL)pre = p;
    }
    Check(p->rightTree, e);
  }
  else if (e == p->data)
  {
    temp = p;
    return 1;
  }
  else if (e < p->data)
  {
    if (p->leftTree == NULL)return -1;
    if (p->leftTree->data == e)
    {
      if (p->leftTree->leftTree == NULL && p->leftTree->rightTree == NULL)pre = p;
    }
    Check(p->leftTree, e);
  }
}
int BSTree::Delete(Node* p, int e)
{
  if (!p->rightTree&&p->leftTree!=NULL)
  {
    Node* q = new Node();
    q = p;
    p = p->leftTree;
    delete q;
  }
  else if (!p->leftTree&&p->rightTree!=NULL)
  {
    Node* q = new Node();
    q = p;
    p = p->leftTree;
    delete q;
  }
  else if (p->leftTree==NULL && p->rightTree==NULL)
  {
    if (pre->leftTree->data == e)pre->leftTree = NULL;
    else pre->rightTree = NULL;
    Node* q = new Node();
    q = p;
    delete q;
  }
  else
  {
    Node* q = new Node();
    Node* s = new Node();
    q = p;
    s = p->leftTree;
    while (s->rightTree)
    {
      q = s;
      s = s->rightTree;
    }
    p->data = s->data;
    if (q != p)q->rightTree = s->leftTree;
    else q->leftTree = s->leftTree;
    delete s;
  }
  return 0;
}
void BSTree::InOrderTraverse(Node* p)
{
  if (p->leftTree != NULL)InOrderTraverse(p->leftTree);
  cout << p->data << " ";
  if (p->rightTree != NULL)InOrderTraverse(p->rightTree);
}
int main()
{
  BSTree bst;
  bst.Create();
  return 0;
}


4. DS查找—二叉树平衡因子


题目描述


二叉树用数组存储,将二叉树的结点数据依次自上而下,自左至右存储到数组中,一般二叉树与完全二叉树对比,比完全二叉树缺少的结点在数组中用0来表示。


计算二叉树每个结点的平衡因子,并按后序遍历的顺序输出结点的平衡因子。


–程序要求–

若使用C++只能include一个头文件iostream;若使用C语言只能include一个头文件stdio

程序中若include多过一个头文件,不看代码,作0分处理

不允许使用第三方对象或函数实现本题的要求


输入


测试次数t


每组测试数据一行,数组元素个数n,后跟n个字符,二叉树的数组存储。


输出


对每组测试数据,按后序遍历的顺序输出树中结点的平衡因子(测试数据没有空树)


输入样例


2

6 ABC00D

24 ABCD0EF0000H00000000000I


输出样例


B 0

D 0

C 1

A -1

D 0

B 1

I 0

H 1

E 2

F 0

C 2

A -2


参考代码

#include<iostream>
using namespace std;
int getheight(char* bst, int i, int n)
{
  if (bst[i] == '0' || i >= n)
    return 0;
  else
  {
    int l = getheight(bst, 2 * i + 1, n)+1;
    int r = getheight(bst, 2 * i + 2, n)+1;
    return l > r ? l : r;
  }
}
void func(char *bst,int n,int i)
{
  if (i < n)
  {
    if (bst[i] != '0')
    {
      func(bst, n, 2 * i + 1);
      func(bst, n, 2 * i + 2);
      cout << bst[i] << " ";
      int height = getheight(bst, 2 * i + 1, n) - getheight(bst, 2 * i + 2, n);
      cout << height << endl;
    }
  }
}
int main()
{
  int t;
  cin >> t;
  while (t--)
  {
    int n;
    cin >> n;
    char* bst;
    bst = new char[n];
    for (int i = 0;i < n;i++)
      cin >> bst[i];
    func(bst, n, 0);
  }
  return 0;
}


相关文章
|
5月前
|
存储 Go iOS开发
掌握Go语言:探索Go语言指针,解锁高效内存操作与动态数据结构的奥秘(19)
掌握Go语言:探索Go语言指针,解锁高效内存操作与动态数据结构的奥秘(19)
|
11月前
|
存储 算法 编译器
【霍洛维兹数据结构】栈和队列 | 动态循环队列 | 迷宫问题 | 表达式 | 多重栈&多重队列
【霍洛维兹数据结构】栈和队列 | 动态循环队列 | 迷宫问题 | 表达式 | 多重栈&多重队列
79 0
|
4月前
|
存储 NoSQL Redis
Redis系列学习文章分享---第十六篇(Redis原理1篇--Redis数据结构-动态字符串,insert,Dict,ZipList,QuickList,SkipList,RedisObject)
Redis系列学习文章分享---第十六篇(Redis原理1篇--Redis数据结构-动态字符串,insert,Dict,ZipList,QuickList,SkipList,RedisObject)
79 1
|
11天前
|
存储 算法 数据管理
数据结构与算法学习二零:二叉排序树(BST)、平衡二叉树(AVL)
这篇文章通过需求分析、代码实现和测试验证,详细介绍了二叉排序树的创建、遍历和删除操作,以及二叉平衡树(AVL)的自平衡特性和单旋转操作,旨在提高树结构在数据管理中的效率和性能。
17 0
数据结构与算法学习二零:二叉排序树(BST)、平衡二叉树(AVL)
|
1月前
|
存储 C语言
数据结构基础详解(C语言): 树与二叉树的应用_哈夫曼树与哈夫曼曼编码_并查集_二叉排序树_平衡二叉树
本文详细介绍了树与二叉树的应用,涵盖哈夫曼树与哈夫曼编码、并查集以及二叉排序树等内容。首先讲解了哈夫曼树的构造方法及其在数据压缩中的应用;接着介绍了并查集的基本概念、存储结构及优化方法;随后探讨了二叉排序树的定义、查找、插入和删除操作;最后阐述了平衡二叉树的概念及其在保证树平衡状态下的插入和删除操作。通过本文,读者可以全面了解树与二叉树在实际问题中的应用技巧和优化策略。
|
4月前
|
弹性计算 负载均衡 NoSQL
NoSQL数据库如何支持动态数据结构?
【6月更文挑战第11天】NoSQL数据库如何支持动态数据结构?
41 2
|
5月前
|
C++
数据结构(顺序表 动态定义
数据结构(顺序表 动态定义
30 2
|
5月前
|
存储 算法 Java
数据结构之动态内存管理机制(中)
数据结构之动态内存管理机制(中)
54 2
|
5月前
|
存储 算法
数据结构之动态内存管理机制(下)
数据结构之动态内存管理机制
54 1
|
5月前
|
存储 NoSQL 安全
Redis入门到通关之数据结构解析-动态字符串SDS
Redis入门到通关之数据结构解析-动态字符串SDS
66 0